Let's get started with a program that uses the switch conditional!
Open Eclipse
Right-Click on your buffalo.edu.fa23. package
Choose: File -> New -> class
In the Name: textbox type: Grade
Click: Finish
Copy the Grade.java Template (Java) code (below)
Paste that code into your new Grade class in the editor
Change both occurrences of put-your-lastname-here
Replace the blank on the int
line with a grade between 0 and 100
After the } else {
for the 'C' grade, add: } else if (grade > 59) {
letterGrade = 'D';
After the case 'D':
line print out a message for a 'D' grade
and a break; statement.
Click the Run icon
Grade.java Template (Java)
package buffalo.edu.fa23.put-your-lastname-here;
/**
*
* File: Grade.java
*
* Create a Java class named Grade displays a letter grade based on the grade points:
*
* 1) The class has one integer property (grade)
* 2) This class contains a main( ) method which
* a) initializes the value of 'grade'
* b) uses a switch statement to compute the corresponding letter grade
* c) outputs the result and a message to the user's screen.
*
*
/**
*
* @authorput-your-name-here
*
*/public class Grade {
public static void main(String[] arguments) {
intgrade = ____;
charletterGrade;
if (grade > 89) {
letterGrade = 'A';
} else if (grade > 79) {
letterGrade = 'B';
} else if (grade > 69) {
letterGrade = 'C';
// add else if for a 'D' grade (>59) here
} else if (______________________________________) {
} else {
letterGrade = 'F';
}
switch (letterGrade) {
case'A':
System.out.println("You got an A. Great job!");
break;
case'B':
System.out.println("You got a B. Good work!");
break;
case'C':
System.out.println("You got a C. You need to try harder!");
break;
case'D';
break;
___________________;
default:
System.out.println("You got an F. You'll do well in Congress!");
}
}
}