Difference between revisions of "Letter Grade Conversion"

From CompSciWiki
Jump to: navigation, search
(Added quotations to student quotes)
 
(13 intermediate revisions by 5 users not shown)
Line 2: Line 2:
  
 
|Problem=
 
|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:   
+
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):   
Above Average: A+, A, B+
+
<br>'''A''' : 80-100 (Excellent work)
Average: B, C+, C  
+
<br>'''B''': 70-79 (Very Good)
Below Average: D, F
+
<br>'''C''': 60-69 (Average)
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().
+
<br>'''D''': 50-59 (Below Average)
 +
<br>'''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 (numerical) input.  Use constants for the letter grade ranges.  
  
 +
|SideSectionTitle=...By Students
 +
|SideSection=
 +
"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."
  
|SideSectionTitle=<center>If Statements and Named Constants
+
|Solution=
 +
First, figure out which constants need to be declared.  Remember that declaring constants guarantee that these values will not change.
 +
{{CodeBlock|Code=
 +
      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; 
 +
}}
  
|SideSection=[[Image:Wiki_sign01.jpg|center]]<BR>
+
Hint: This program uses Scanner for input. Remember to import the Scanner code.
 +
{{CodeBlock|Code=
 +
import java.util.Scanner;
 +
}}
  
|Solution=The solution...
+
 +
|SolutionCode=import javax.swing.JOptionPane;
  
|SolutionCode=
+
import java.util.Scanner;
import javax.swing.JOptionPane;
+
  
<pre>public class LetterConversion
+
public class LetterGrade1
 
{
 
{
    public static void main(String [ ] args)
+
  public static void main(String args [])
    {
+
  {
        final int MIN_A = 80;
+
    final int MAX_GRADE = 100;
        final int MIN_B = 70;
+
        final int MIN_C = 60;
+
        final int MIN_D = 50;
+
        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 = "Above Average";
+
        final String AVG = "Average";
+
        final String BELOW_AVG = "Below Average";
+
 
          
 
          
 +
    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)
 +
    {
 +
        System.out.println("You're grade is below average.");
 +
        System.out.println("F");
 
     }
 
     }
 +
      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.");
 +
   
 +
  }
 
}
 
}
</pre>
+
//thanks to Josh Simard for the code
  
}}
+
   
 +
    }}

Latest revision as of 15:20, 8 December 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 (numerical) input. Use constants 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