Source of TestPeople.java


  1: package inheritance;


  4: import java.util.Scanner;

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

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

 23:         // Overriding

 25:     }

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

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

 47:     private static void testStudentClass() {
 48:         // create Persons and do peron-y things with them
 49:         System.out.println("Creating Students Alexander and Djenaba");
 50:         Student alex = new Student("Alexander");
 51:         Student djen = new Student("Djenaba");
 52:         System.out.println("Students have A-Numbers:");
 53:         System.out.println("\talex.A_NUMBER is " + alex.A_NUMBER);
 54:         System.out.println("\tdjen.A_NUMBER is " + djen.A_NUMBER);
 55:         pause();

 57:         // do some grade stuff
 58:         System.out.println("Students have grades:");
 59:         alex.setGrade(80);
 60:         djen.setGrade(85);
 61:         System.out.println("\talex.getGrade() is " + alex.getGrade());
 62:         System.out.println("\tdjen.getGrade() is " + djen.getGrade());
 63:         pause();

 65:         // do some Person stuff
 66:         System.out.println("Students are Persons, and Persons have names:");
 67:         System.out.println("\talex.getName() is " + alex.getName());
 68:         System.out.println("\tdjen.getName() is " + djen.getName());
 69:         pause();
 70:     }

 72:     private static void testUndergradClass() {
 73:         // create Persons and do peron-y things with them
 74:         System.out.println("Creating Undergrads Ellen and Frank");
 75:         Undergrad ellen = new Undergrad("Ellen", 3);
 76:         Undergrad frank = new Undergrad("Frank");
 77:         System.out.println("Undergrads have years:");
 78:         System.out.println("\tellen.getYear() is " + ellen.getYear());
 79:         System.out.println("\tfrank.getYear() is " + frank.getYear());
 80:         pause();

 82:         // do some grade stuff
 83:         System.out.println("Undergrads are Students, and so:");
 84:         ellen.setGrade(70);
 85:         frank.setGrade(75);
 86:         System.out.println("\tellen.A_NUMBER is " + ellen.A_NUMBER);
 87:         System.out.println("\tfrank.A_NUMBER is " + frank.A_NUMBER);
 88:         System.out.println("\tellen.getGrade() is " + ellen.getGrade());
 89:         System.out.println("\tfrank.getGrade() is " + frank.getGrade());
 90:         pause();

 92:         // do some Person stuff
 93:         System.out.println("Undergrads are Students, "
 94:                 + "and Students are Persons, so:");
 95:         System.out.println("\tellen.getName() is " + ellen.getName());
 96:         System.out.println("\tfrank.getName() is " + frank.getName());
 97:         pause();
 98:     }

100:     private static void testGradStudentClass() {
101:         // create Persons and do peron-y things with them
102:         System.out.println("Creating GradStudents geety and harry");
103:         GradStudent geety = new GradStudent("Geety", "BSc");
104:         GradStudent harry = new GradStudent("Harry", "BSc, MMath");
105:         System.out.println("GradStudents have previous degrees:");
106:         System.out.println("\tgeety.getPreviousDegree() is " 
107:                 + geety.getPreviousDegree());
108:         System.out.println("\tharry.getPreviousDegree() is " 
109:                 + harry.getPreviousDegree());
110:         pause();

112:         // do some grade stuff
113:         System.out.println("Undergrads are Students are Persons....");
114:         geety.setGrade(90);
115:         harry.setGrade(95);
116:         System.out.println("\tgeety.A_NUMBER is " + geety.A_NUMBER);
117:         System.out.println("\tharry.A_NUMBER is " + harry.A_NUMBER);
118:         System.out.println("\tgeety.getGrade() is " + geety.getGrade());
119:         System.out.println("\tharry.getGrade() is " + harry.getGrade());
120:         System.out.println("\tgeety.getName() is " + geety.getName());
121:         System.out.println("\tharry.getName() is " + harry.getName());
122:         pause();
123:     }

125:     public static void pause() {
126:         Scanner kbd = new Scanner(System.in);
127:         System.out.print("\nPress enter...");
128:         kbd.nextLine();
129:         System.out.println("\n\n\n\n");
130:     }

132: }