gcp Logo

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

  1. Login to your Repl.It account.
  2. Click on: Languages
  3. In the Popular section choose: Java: A concurrent, class-based, statically typed object-oriented language.
  4. Click the pencil icon next to your repl name.
  5. In the Default name: input box type: Grade
  6. Delete all the lines of code provided as an example.
  7. Copy the Grade.java Template (Java) code (below)
  8. Paste that code into your new Grade class in the editor
  9. Change the @author put-your-lastname-here
  10. Replace the blank on the
    int
    line with a grade between 0 and 100
  11. After the
    } else {
    for the 'C' grade, add:
    } else if (grade > 59) {
      letterGrade = 'D';
  12. After the
    case 'D':
    line print out a message for a 'D' grade
    and a break; statement.
  13. Click the Run icon

Grade.java Template (Java)

/**
 *
 * 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 Main {

  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 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!");
    }
  }
}

Grade Class Output

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