Program Flow  «Prev  Next»

Switch Statement - Exercise

Using Java switch statement

Objective: Use the switch statement to implement a method in the GradePoints application.

Exercise grading

The full credit for this exercise is 10 points. To receive full credit, you willl need to successfully create the source code for the application. You will submit your source code.

Exercise instructions

Create an application named GradePoints that determines the grade points associated with the letter grades A, B, C, D, and F.
For your reference, here are the grade points associated with the letter grades A, B, C, D, and F:
A = 4 points
B = 3 points
C = 2 points
D = 1 point
F = 0 points
In this exercise, you need to create a static method called gradePoints(). The method should accept a char as a parameter representing a letter grade. The method should return an int which represents the grade points corresponding to the letter grade.
Use a switch statement to implement your gradePoints() method. To keep things simple, you can return 0 (zero) for the grade points associated with a character that does not represent a letter grade.
In your main() method, invoke the method gradePoints with each of the characters A, B, C, D, and F and display the results. For example, to display the grade points associated with the letter grade B, you could use the following code:
            
System.out.println("B = " + gradePoints('B') + " points");

What needs to be submitted:

In the text box below, cut and paste the source code for GradePoints. Click Submit to submit your code.