Source of TestStudent.java


  2: import java.util.Scanner;

  4: /**
  5:  * Check the Student class by creating some objects and manipulating them.
  6:  *
  7:  * @author Mark Young (A00000000)
  8:  */
  9: public class TestStudent {

 11:     public static final Scanner KBD = new Scanner(System.in);

 13:     public static void main(String[] args) {
 14:         // create a couple of students
 15:         Student s1 = new Student("A00000001", "Dent, Stu.", 95);
 16:         Student s2 = new Student("A00000002", "Tudiaunt, A.", 80);

 18:         // show info for first two students
 19:         System.out.println("\n"
 20:                 + "One student's info:"
 21:                 + "\n\tName:   " + s1.getName()
 22:                 + "\n\tNumber: " + s1.getANumber()
 23:                 + "\n\tGrade:  " + s1.getPctGrade());
 24:         System.out.println("\n"
 25:                 + "Another student's info:"
 26:                 + "\n\tName:   " + s2.getName()
 27:                 + "\n\tNumber: " + s2.getANumber()
 28:                 + "\n\tGrade:  " + s2.getPctGrade());
 29:         pause();

 31:         // Change some info
 32:         s1.setName("Dent, Stuart");
 33:         s2.setPctGrade(90);

 35:         // show revised information
 36:         System.out.println("Revised information for the first student:");
 37:         s1.printStudentRecord();
 38:         System.out.println("Revised information for the second student:");
 39:         s2.printStudentRecord();
 40:         pause();

 42:         // prepare to get more student information
 43:         Student stu = null;     // so no complaint about "not initialized"
 44:         String num, name;
 45:         int pct;

 47:         // read student numbers until user signals to quit
 48:         System.out.print("Enter student's A-# (or Q to quit): ");
 49:         num = KBD.nextLine();
 50:         while (!num.startsWith("Q")) {

 52:             // read student info
 53:             System.out.print("Enter student name: ");
 54:             name = KBD.nextLine();
 55:             System.out.print("Enter percent grade: ");
 56:             pct = KBD.nextInt();
 57:             KBD.nextLine();

 59:             // create and print student information
 60:             stu = new Student(num, name, pct);
 61:             System.out.println("\n"
 62:                     + stu.getName() + " (" + stu.getANumber() + ") "
 63:                     + "got " + stu.getPctGrade() + "%");

 65:             // get next student’s number
 66:             System.out.print("Enter student's A-# (or Q to quit): ");
 67:             num = KBD.nextLine();
 68:         }
 69:         pause();

 71:         // report last student read
 72:         System.out.println("The last student you entered was " + stu.aNumber);
 73:         stu.printStudentRecord();
 74:         pause();
 75:     }

 77:     private static void pause() {
 78:         System.out.print("\nPress Enter...");
 79:         KBD.nextLine();
 80:         System.out.println();
 81:     }

 83: }