Difference between revisions of "Letter Grade Conversion"

From CompSciWiki
Jump to: navigation, search
(EDIT: Changed "I learned to test variables and evaluate it" to "I learned to test variables and evaluate them".)
(updated solution to include Scanner; and avoids nested ifs (since didn't seem necessary))
Line 3: Line 3:
 
|Problem=
 
|Problem=
 
Create a program that converts a numerical grade to its corresponding letter grade.  The letter grades that need assigning are (Please note that the following ranges are inclusive):   
 
Create a program that converts a numerical grade to its corresponding letter grade.  The letter grades that need assigning are (Please note that the following ranges are inclusive):   
<br>'''A''' : 80-100 (Above Average)  
+
<br>'''A''' : 80-100 (Excellent work)  
<br>'''B''': 70-79, '''C''': 60-69   (Average)  
+
<br>'''B''': 70-79 (Very Good)
<br>'''D''': 50-59, '''F''': 0-49(Below Average)
+
<br>'''C''': 60-69 (Average)  
<br>Your first task is to assign the numerical range for each corresponding letter. Then, you must provide the user which category the grade falls on.  For example, if the user typed in "78" as the input and is considered as a B, you must also output its category as "Average".  Only grades between 0-100(inclusive) are considered valid.   Input must be handled using  JOptionPane.showInputDialog(), and output using System.out.println().  Ensure that necessary validity checks are done in the program to prevent invalid input.  
+
<br>'''D''': 50-59 (Below Average)
 +
'''F''': 0-49 (Below Average)
 +
<br>Prompt the user to input a letter grade (between 0 and 100). Output the letter grade and a brief grade description (such as "Excellent work"). Use Scanner for input and System.out for output. Ensure that necessary validity checks are done in the program to prevent invalid input. Use constant for the letter grade ranges. 
  
 
|SideSectionTitle=...by students
 
|SideSectionTitle=...by students
Line 13: Line 15:
  
 
|Solution=
 
|Solution=
Before attempting to program the solution, you need to first figure out which constants need to be declared.  Remember that declaring constants guarantee that these values will not change.  One obvious set to be instantiated would be the letter grades.  These letter grades can be declared as chars, as they only need one character to be stored.  The next set of constants that need to be declared are the numerical range of each letter grade.  Since declaring the actual range of the letters  will only increase the difficulty of the solution, the minimum of each letter grade will suffice.  The maximum grade that can be earned should be declared as a constant as well, to ensure the validity of the input.  Finally, the categories must be declared as constants since these values do not change.  Notice that all the names of the constants follow programming standards.
+
First, figure out which constants need to be declared.  Remember that declaring constants guarantee that these values will not change.  
 
<pre>
 
<pre>
 
       final int MAX_GRADE = 100;
 
       final int MAX_GRADE = 100;
Line 23: Line 25:
 
         final int MIN_F = 0;
 
         final int MIN_F = 0;
 
          
 
          
        final char LET_A = 'A';
 
        final char LET_B = 'B';
 
        final char LET_C = 'C';
 
        final char LET_D = 'D';
 
        final char LET_F = 'F';
 
 
        final String ABOVE_AVG = "The grade is above average.";
 
        final String AVG = "The grade is average.";
 
        final String BELOW_AVG = "The grade is below average.";
 
 
</pre>
 
</pre>
  
The next step is to declare the variables. You will need a String variable to handle user input, an int variable to convert the String input to a comparable value, and another String variable to handle the result category.
+
Hint: This program uses Scanner for input. Remember to import the Scanner code.  
 
<pre>
 
<pre>
        String input;              //The string that the user inputs will be stored here.
+
import java.util.Scanner;
        int grade;                    //An int is needed to convert the String input for integer comparison.
+
        String result="";         //The String needed for output.
+
 
</pre>
 
</pre>
  
Now that all constants and variables are declared, it is time to code the rest of the program.  For JOptionPane to work on your program, ensure that this piece of code is insterted at the beginning of your program:
 
<pre>
 
import javax.swing.JOptionPane;
 
</pre>
 
 
To generate input from the user, assign JOptionPane.showInputDialog to your input string.  After this is done, the string input must be converted to an int. 
 
<pre>
 
        //The program asks the user for a numerical grade.
 
        input = JOptionPane.showInputDialog("Please enter the numerical grade to be converted: ");
 
       
 
        //Integer.parseInt converts a String to an int.
 
        grade = Integer.parseInt(input);
 
</pre>
 
 
Then, the first layer of if statements need to be created to check the validity of the user input.  The first set of conditions need to ensure that the input is between 0-100(inclusive).  If this condition fails, a message needs to be sent to the user that provide proper feedback on what values are valid.
 
<pre>
 
if(MAX_GRADE >= grade && grade >= MIN_F)
 
{
 
 
}
 
else
 
{
 
      result = "Error: not a valid numerical grade.  Enter a number between 0-100(inclusive)";
 
}
 
</pre>
 
 
The categories and letter grades will be handled inside the first set of conditions.  The next layer of if statements determine which category the current grade belongs to, and inside these will be the layer of if statements that determine the exact letter grade conversion.  Since the minimum of each letter grade is declared, the first condition of each layer of evaluation need to be in descending order. 
 
<pre>
 
            //The second layer of nested if statements determine
 
            //what category the numerical grade is in.  This layer
 
            //is nested inside a validity check to ensure that any
 
            //invalid input will not generate a valid output.
 
            if(grade >= MIN_A)
 
            {
 
                System.out.print(LET_A);
 
                result= ABOVE_AVG;
 
            }
 
            else if(grade >= MIN_C)
 
            {
 
                //The third layer of nested if statements determine
 
                //the letter grade value of the numerical grade.  The
 
                //letter is printed out without the next line so the
 
                //output looks continuous.
 
                if(grade >= MIN_B)
 
                {
 
                    System.out.print(LET_B);
 
                }
 
                else
 
                {
 
                    System.out.print(LET_C);
 
                }
 
                result = AVG;
 
            }
 
            else if(grade >= MIN_F)
 
            {
 
                if(grade >= MIN_D)
 
                {
 
                    System.out.print(LET_D);
 
                }
 
                else
 
                {
 
                    System.out.print(LET_F);
 
                }
 
                result = BELOW_AVG;
 
            }
 
            System.out.print(": ");
 
</pre>
 
 
Since the letter grade is printed within the if statements, the only thing missing is the category.  This can be printed at the end of the program, and output will still look continuous because there was no extral line printed.
 
<pre>
 
System.out.println(result);
 
</pre>
 
 
   
 
   
 
|SolutionCode=import javax.swing.JOptionPane;
 
|SolutionCode=import javax.swing.JOptionPane;
  
public class LetterConversion
+
import java.util.Scanner;
 +
 
 +
public class LetterGrade1
 
{
 
{
    public static void main(String [ ] args)
+
  public static void main(String args [])
 +
  {
 +
    final int MAX_GRADE = 100;
 +
       
 +
    final int MIN_A = 80;
 +
    final int MIN_B = 70;
 +
    final int MIN_C = 60;
 +
    final int MIN_D = 50;
 +
    final int MIN_F = 0;
 +
    double grade;
 +
    String input;
 +
   
 +
    //just put this line in (making new Scanner object) so you can use Scanner
 +
    Scanner kbd = new Scanner (System.in);
 +
   
 +
    //get input using Scanner   
 +
    System.out.println ("Please enter a grade score percentage between 1 - 100: ");
 +
    grade = kbd.nextDouble();
 +
   
 +
    //use if/else ladder
 +
   
 +
    if (grade < MIN_D)
 
     {
 
     {
        //You must first declare the necessary constants needed
+
        System.out.println("You're grade is below average.");
        //to run the program.
+
        System.out.println("F");
       
+
        //MAX_GRADE is important to declare because you need to ensure invalid
+
        //input will not receive a letter grade.
+
        final int MAX_GRADE = 100;
+
       
+
        //Minimum letter grades must be declared to ensure the complete
+
        //range of grades are accounted for.
+
        final int MIN_A = 80;
+
        final int MIN_B = 70;
+
        final int MIN_C = 60;
+
        final int MIN_D = 50;
+
        final int MIN_F = 0;
+
       
+
        //The actual char of each letter grade.  This must be declared
+
        //to prevent from bad programming practice.
+
        final char LET_A = 'A';
+
        final char LET_B = 'B';
+
        final char LET_C = 'C';
+
        final char LET_D = 'D';
+
        final char LET_F = 'F';
+
       
+
        //String constants are also declared for efficiency.
+
        final String ABOVE_AVG = "The grade is above average.";
+
        final String AVG = "The grade is average.";
+
        final String BELOW_AVG = "The grade is below average.";
+
       
+
        String input;            //The string that the user inputs will be stored here.
+
        int grade;              //An int is needed to convert the String input for integer comparison.
+
        String result="";        //The String needed for output.
+
       
+
        //The program asks the user for a numerical grade.
+
        input = JOptionPane.showInputDialog("Please enter the numerical grade to be converted: ");
+
       
+
        //Integer.parseInt converts a String to an int.
+
        grade = Integer.parseInt(input);
+
       
+
        //This first if statement ensures that the grade provided by the
+
        //user is valid.  Note the use of constants here.
+
        if(MAX_GRADE >= grade && grade >= MIN_F)
+
        {
+
            //The second layer of nested if statements determine
+
            //what category the numerical grade is in.  This layer
+
            //is nested inside a validity check to ensure that any
+
            //invalid input will not generate a valid output.
+
            if(grade >= MIN_A)
+
            {
+
                System.out.print(LET_A);
+
                result= ABOVE_AVG;
+
            }
+
            else if(grade >= MIN_C)
+
            {
+
                //The third layer of nested if statements determine
+
                //the letter grade value of the numerical grade.  The
+
                //letter is printed out without the next line so the
+
                //output looks continuous.
+
                if(grade >= MIN_B)
+
                {
+
                    System.out.print(LET_B);
+
                }
+
                else
+
                {
+
                    System.out.print(LET_C);
+
                }
+
                result = AVG;
+
            }
+
            else if(grade >= MIN_F)
+
            {
+
                if(grade >= MIN_D)
+
                {
+
                    System.out.print(LET_D);
+
                }
+
                else
+
                {
+
                    System.out.print(LET_F);
+
                }
+
                result = BELOW_AVG;
+
            }
+
            System.out.print(": ");
+
        }
+
        else
+
        {
+
            result = "Error: not a valid numerical grade.  Enter a number between 0-100(inclusive)";
+
        }
+
       
+
           
+
        System.out.println(result);
+
       
+
 
     }
 
     }
 +
      else if (grade < MIN_C)
 +
      {
 +
        System.out.println("Your grade is below average.");
 +
        System.out.println("D");
 +
      }
 +
      else if (grade < MIN_B)
 +
      {
 +
        System.out.println("Your grade is average.");
 +
        System.out.println("C");
 +
      }
 +
      else if (grade < MIN_A)
 +
      {
 +
        System.out.println("Your grade is average.");
 +
        System.out.println("B");
 +
      }
 +
      else if (grade <= MAX_GRADE)
 +
      {
 +
        System.out.println("Your grade is above average.");
 +
        System.out.println("A");
 +
      }
 +
    else
 +
      System.out.println("That is not a valid grade score. Please rerun the program and try again.");
 +
   
 +
  }
 
}
 
}
}}
+
 
 +
   
 +
    }}

Revision as of 15:35, 22 September 2011

Back to the Program-A-Day homepage

Problem

Create a program that converts a numerical grade to its corresponding letter grade. The letter grades that need assigning are (Please note that the following ranges are inclusive):
A : 80-100 (Excellent work)
B: 70-79 (Very Good)
C: 60-69 (Average)
D: 50-59 (Below Average)

F: 0-49 (Below Average)


Prompt the user to input a letter grade (between 0 and 100). Output the letter grade and a brief grade description (such as "Excellent work"). Use Scanner for input and System.out for output. Ensure that necessary validity checks are done in the program to prevent invalid input. Use constant for the letter grade ranges.

 

...by students

This problem was part of the midterm I wrote when I was in COMP 1010 (which was known as 74.101 when I took it). The problem was in the form of a multiple choice question, and asked what letter grade would be generated in the provided code. I was confident in my answer, but later found out that I was wrong. I made the mistake of assuming what the code would generate, instead of actually mapping out the answer. When doing if statements, I learned to test variables and evaluate them using the provided conditions, instead of assuming that the solution in my head is what the program actually does.

Solution

First, figure out which constants need to be declared. Remember that declaring constants guarantee that these values will not change.

       final int MAX_GRADE = 100;
        
        final int MIN_A = 80;
        final int MIN_B = 70;
        final int MIN_C = 60;
        final int MIN_D = 50;
        final int MIN_F = 0;
        

Hint: This program uses Scanner for input. Remember to import the Scanner code.

import java.util.Scanner;

Code

Solution Code

Back to the Program-A-Day homepage