FileDocCategorySizeDatePackage
GradeTester.javaAPI DocExample2181Sat Apr 24 08:22:04 BST 2004com.oreilly.tiger.ch03

GradeTester

public class GradeTester extends Object

Fields Summary
private Student
student1
private Student
student2
private Student
student3
Constructors Summary
public GradeTester()

 
    student1 = new Student("Brett", "McLaughlin");
    student2 = new Student("Ben", "Rochester");
    student3 = new Student("Dennis", "Erwin");
  
Methods Summary
public voidlistGradeValues(java.io.PrintStream out)

    Grade[] gradeValues = Grade.values();

    // for loop
    for (int i=0; i<gradeValues.length; i++) {
      out.println("Allowed value: '" + gradeValues[i] + "'");
    }

    // for/in loop
    for (Grade g : gradeValues ) {
      out.println("Allowed value: '" + g + "'");
    }
  
public static voidmain(java.lang.String[] args)

    try {
      GradeTester tester = new GradeTester();
  
      tester.testGradeAssignment(System.out);
      tester.listGradeValues(System.out);
      tester.testSwitchStatement(System.out);
    } catch (Exception e) {
      e.printStackTrace();
    }
  
public voidtestGradeAssignment(java.io.PrintStream out)

    student1.assignGrade(Grade.B);
    student2.assignGrade(Grade.INCOMPLETE);
    student3.assignGrade(Grade.A);
  
public voidtestSwitchStatement(java.io.PrintStream out)

    StringBuffer outputText = new StringBuffer(student1.getFullName());

    switch (student1.getGrade()) {
      case A: 
        outputText.append(" excelled with a grade of A");
        break;   
      case B: // fall through to C
      case C: 
        outputText.append(" passed with a grade of ")
                  .append(student1.getGrade().toString());
        break;
      case D: // fall through to F
      case F:
        outputText.append(" failed with a grade of ")
                  .append(student1.getGrade().toString());
        break;
      case INCOMPLETE:
        outputText.append(" did not complete the class.");
        break;
      default:
        outputText.append(" has a grade of ")
                  .append(student1.getGrade().toString());
        break;
    }

    out.println(outputText.toString());