Back to the Program-A-Day homepage
Problem
Create a program that converts a numerical grade to its corresponding letter grade. Your first task is to assign the numerical range for each corresponding letter. The letter grades that need assigning are:
Above Average: A+, A, B+
Average: B, C+, C
Below Average: D, F
Your second task is to 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 "Above Average". Input must be handled using JOptionPane.showInputDialog(), and output using System.out.println().
|
|
If Statements and Named Constants
|
Solution
The solution...
|
Code
Solution Code
import javax.swing.JOptionPane;
import javax.swing.JOptionPane;
public class LetterConversion
{
public static void main(String [ ] args)
{
//You must first declare the necessary constants needed
//to run the program.
//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 COLON = ": ";
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=0; //An int is needed to convert the String input for integer comparison.
String result=""; //The String of items 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(COLON);
}
else
{
result += "Error: not a valid numerical grade.";
}
System.out.println(result);
}
}
|
Back to the Program-A-Day homepage