bsc Logo

Let's get started with a program that uses the switch conditional!

  1. Open Eclipse
  2. Right-Click on your edu.buffalostate.cis425.fa19.exercises. package
  3. Choose: File -> New -> class
  4. In the Name: textbox type: Grade
  5. Click: Finish
  6. Copy the Grade.java Template (Java) code (below)
  7. Paste that code into your new Grade class in the editor
  8. Change both occurrences of put-your-lastname-here
  9. Replace the blank on the
    int
    line with a grade between 0 and 100
  10. After the
    } else {
    for the 'C' grade, add:
    } else if (grade > 59) {
      letterGrade = 'D';
  11. After the
    case 'D':
    line print out a message for a 'D' grade
    and a break; statement.
  12. Click the Run icon

Grade.java Template (Java)

package edu.buffalostate.cis425.fa19.exercises.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.
 *
 *
/**
 *
 * @author put-your-name-here
 *
 */

public class Grade {

  public static void main(String[] arguments) {
    int grade = ____;
    char letterGrade;
    if (grade > 89) {
      letterGrade = 'A';
    } else if (grade > 79) {
      letterGrade = 'B';
    } else if (grade >  69) {
      letterGrade = 'C';
    // add else if and assignment 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': 
        ___________________;
      default:
        System.out.println("You got an F. You'll do well in Congress!");
    }
  }
}

Grade Class Output

You got an F. You'll do well in Congress!