Problem Statement: Numbers on a scale of 1 to 100 are randomly given the grade as the input from the user and then match the input grade of the user against the given cases to generate the desired output.
Real life Example
Consider a number on a scale of 1 to 100 are written on a piece of paper slips. Now person starts picking slips one by one and starts organizing them in such a manner that he starts putting slips in 10 different boxes prior deciding making 10 different pots in such a manner slips numbering 1-10 in ‘pot A’, 11-20 in ‘pot B’ and so on till labeling and inserting slips till all the slips are organized in labeled pots. He did this in order to easily figure out numbers written on slips.Â
So in technical terms In these ways machine get
- Take input from the user
- Match the grade using equal to the operator (==)
- Generate the equivalent description as the output
There are several methods starting from brute force(naive) to most optimal. Few are discussed below :
- Â Using the If-Else Method
- Switch case Method
The grades are assigned using the followingÂ
Approach 1: If-Else Statement
Below is the implementation of the above approach
Java
// Java Program to Read Grade & Displaying Equivalent DescÂ
// Importing Classes/Filesimport java.util.*;Â
public class GFG {Â
    // Main Driver Method    public static void main(String[] args)    {Â
        // User is supposed to enter grade among them        System.out.println(            "Enter Grade varying from S,A,B,C,D");        String grade = "A";Â
        // Checking whether grade == "S" or not        if (grade == "S") {            System.out.println(                "Student has scored between 90 to 100");        }        // Checking whether grade == "A" or not        else if (grade == "A") {            System.out.println(                "Student has scored between 80 to 90");        }        // Checking whether grade == "B" or not        else if (grade == "B") {            System.out.println(                "Student has scored between 70 to 80");        }        // Checking whether grade == "C" or not        else if (grade == "C") {            System.out.println(                "Student has scored between 60 to 70");        }        // Checking whether grade == "D" or not        else if (grade == "D") {            System.out.println(                "Student has scored between 50 to 60");        }        else {Â
            // Printing message-user pressed some other key            System.out.println(                "The grade you entered is not valid!");        }    }} |
Enter Grade varying from S,A,B,C,D Student has scored between 80 to 90
Approach 2: Switch Case
Below is the implementation of the above approachÂ
Java
// Java Program to Read a Grade & Display the EquivalentÂ
// Importing Classes/Filesimport java.util.*;Â
public class GFG {Â
    // Main driver method    public static void main(String args[])    {Â
        // Random grade taken for consideration        String grade = "W";Â
        // Using Switch-Case.        switch (grade) {            // Checking whether grade == "S" or not.Â
        case "S":            System.out.println(                "Student has scored between 90 to 100");            break;Â
            // Checking whether grade == "A" or not.        case "A":            System.out.println(                "Student has scored between 80 to 90");            break;Â
            // Checking whether grade == "B" or not.        case "B":            System.out.println(                "Student has scored between 70 to 80");            break;Â
            // Checking whether grade == "C" or not.        case "C":            System.out.println(                "Student has scored between 60 to 70");            break;Â
            // Checking whether grade == "D" or not.        case "D":            System.out.println(                "Student has scored between 50 to 60");            break;Â
        default:            System.out.println(                "The grade you entered is not valid!");        }    }} |
The grade you entered is not valid!
Time complexity: O(1) because constant operations are done
Auxiliary space: O(1)
Â
Â

