Source of TestPeople.java


  2: import java.util.Scanner;

  4: /**
  5:  * A program to demonstrat various bits of inheritance.
  6:  *
  7:  * @author Mark Young (A00000000)
  8:  */
  9: public class TestPeople {

 11:     public static void main(String[] args) {
 12:         // simple inheritance
 13:         introduceSelf();
 14:         testPersonClass();
 15:         testStudentClass();
 16:         testUndergradClass();
 17:         testGradStudentClass();
 18:         
 19:         // public and private methods

 21:         // Overriding

 23:     }

 25:     /**
 26:      * Introduce this program
 27:      */
 28:     private static void introduceSelf() {
 29:         System.out.println("\n\n"
 30:                 + "Testing Inheritance using Person and Student\n"
 31:                 + "--------------------------------------------\n\n"
 32:                 + "Inheritance is when one class 'extends' another.\n"
 33:                 + "In this example Student extends Person.");
 34:         pause();
 35:     }

 37:     /**
 38:      * Show the Person class and its methods.
 39:      */
 40:     private static void testPersonClass() {
 41:         // create Persons and do peron-y things with them
 42:         System.out.println("Creating Persons Bob and Cathy");
 43:         Person bob = new Person("Bob");
 44:         Person cathy = new Person("Cathy");
 45:         System.out.println("Persons have names:");
 46:         System.out.println("\tbob.getName() returns " + bob.getName());
 47:         System.out.println("\tcathy.getName() returns " + cathy.getName());
 48:         pause();
 49:     }

 51:     /**
 52:      * Show the Student class and its methods -- including inhertied methods.
 53:      */
 54:     private static void testStudentClass() {
 55:         // create Persons and do peron-y things with them
 56:         System.out.println("Creating Students Alexander and Djenaba");
 57:         Student alex = new Student("Alexander");
 58:         Student djen = new Student("Djenaba");
 59:         System.out.println("Students have A-Numbers:");
 60:         System.out.println("\talex.A_NUMBER is " + alex.A_NUMBER);
 61:         System.out.println("\tdjen.A_NUMBER is " + djen.A_NUMBER);
 62:         pause();

 64:         // do some grade stuff
 65:         System.out.println("Students have grades:");
 66:         alex.setGrade(80);
 67:         djen.setGrade(85);
 68:         System.out.println("\talex.getGrade() is " + alex.getGrade());
 69:         System.out.println("\tdjen.getGrade() is " + djen.getGrade());
 70:         pause();

 72:         // do some Person stuff
 73:         System.out.println("Students are Persons, and Persons have names:");
 74:         System.out.println("\talex.getName() is " + alex.getName());
 75:         System.out.println("\tdjen.getName() is " + djen.getName());
 76:         pause();
 77:     }

 79:     /**
 80:      * Show the Undergrad class and its methods -- including inhertied methods.
 81:      */
 82:     private static void testUndergradClass() {
 83:         // create Persons and do peron-y things with them
 84:         System.out.println("Creating Undergrads Ellen and Frank");
 85:         Undergrad ellen = new Undergrad("Ellen", 3);
 86:         Undergrad frank = new Undergrad("Frank");
 87:         System.out.println("Undergrads have years:");
 88:         System.out.println("\tellen.getYear() is " + ellen.getYear());
 89:         System.out.println("\tfrank.getYear() is " + frank.getYear());
 90:         pause();

 92:         // do some grade stuff
 93:         System.out.println("Undergrads are Students, and so:");
 94:         ellen.setGrade(70);
 95:         frank.setGrade(75);
 96:         System.out.println("\tellen.A_NUMBER is " + ellen.A_NUMBER);
 97:         System.out.println("\tfrank.A_NUMBER is " + frank.A_NUMBER);
 98:         System.out.println("\tellen.getGrade() is " + ellen.getGrade());
 99:         System.out.println("\tfrank.getGrade() is " + frank.getGrade());
100:         pause();

102:         // do some Person stuff
103:         System.out.println("Undergrads are Students, "
104:                 + "and Students are Persons, so:");
105:         System.out.println("\tellen.getName() is " + ellen.getName());
106:         System.out.println("\tfrank.getName() is " + frank.getName());
107:         pause();
108:     }

110:     /**
111:      * Show the GradeStudent class and its methods -- including inhertied 
112:      * methods.
113:      */
114:     private static void testGradStudentClass() {
115:         // create Persons and do peron-y things with them
116:         System.out.println("Creating GradStudents geety and harry");
117:         GradStudent geety = new GradStudent("Geety", "BSc");
118:         GradStudent harry = new GradStudent("Harry", "BSc, MMath");
119:         System.out.println("GradStudents have previous degrees:");
120:         System.out.println("\tgeety.getPreviousDegree() is " 
121:                 + geety.getPreviousDegree());
122:         System.out.println("\tharry.getPreviousDegree() is " 
123:                 + harry.getPreviousDegree());
124:         pause();

126:         // do some grade stuff
127:         System.out.println("Undergrads are Students are Persons....");
128:         geety.setGrade(90);
129:         harry.setGrade(95);
130:         System.out.println("\tgeety.A_NUMBER is " + geety.A_NUMBER);
131:         System.out.println("\tharry.A_NUMBER is " + harry.A_NUMBER);
132:         System.out.println("\tgeety.getGrade() is " + geety.getGrade());
133:         System.out.println("\tharry.getGrade() is " + harry.getGrade());
134:         System.out.println("\tgeety.getName() is " + geety.getName());
135:         System.out.println("\tharry.getName() is " + harry.getName());
136:         pause();
137:     }

139:     /**
140:      * Prompt the user and wait for them to press the enter key.
141:      */
142:     public static void pause() {
143:         Scanner kbd = new Scanner(System.in);
144:         System.out.print("\nPress enter...");
145:         kbd.nextLine();
146:         System.out.println("\n\n\n\n");
147:     }

149: }