Programming Assignment: Inheritance and Polymorphism


  1. You are about to write an object-oriented program using inheritance and polymorphism to simulate a student tracking system. You'll consider adding the following classes in your design:

    Properties: name (:String) and age (:int). In additon, to keep track of the number of Person being created, you need to additional property that is a class variable (:static int). Methods include a parameterized constructor, a print method, and a method that returns the number of Person objects created.
    Properties: GPA. Methods include a parameterized constructor and a print. If the GPA of a student is 3.5 or above, print out additional message: "Made Dean's honor list."
    Properties: major(:String) and university this student is attending(:University). Methods include a parameterized constructor, a print, and a method that changes the major of this student.
    Properties: school name (:String). Methods include a default constructor, a parameterized constructor and a print method. If the user does not provide any name for the university, use a message as default school name: "The university name has not been provided."
    When you design the program, you need to consider the inheritance relationships among the classes. Use the main method in class TestStudent as a guide to implement the classes. Note that you are NOT allowed to change the main method. In addition, define the member variables (both instance and static) as private and the methods and constructors as public. Once your program works correctly, it should print out the output given at the bottom of this instructions.

    A skeleton code for class TestStudent:

    public class TestStudent {
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		University wesleyan = new University("Texas Wesleyan University, Fort Worth, Texas");
    		University rice = new University("Rice University, Houston, Texas");
    		University cornell = new University("Cornell University, Ithaca, New York");
    		Person bretBirth = new CollegeStudent("Bret Birth", 21, 3.58, "Computer Science", wesleyan);
    		System.out.println("\n---------------------- 1 ---------------------");
    		Person erikaSmart = new CollegeStudent("Erika Smart", 25, 2.3, "Biology", rice);
    		System.out.println("\n---------------------- 2 ---------------------");
    		if (erikaSmart instanceof CollegeStudent)
    			((CollegeStudent)erikaSmart).changeMajor("General Business");
    		System.out.println("\n---------------------- 3 ---------------------");
    		Student joMiller = new CollegeStudent("Jo Miller", 19, 2.85, "Liberal Arts", new University());
    		System.out.println("\n---------------------- 4 ---------------------");
    		CollegeStudent amyZhang = new CollegeStudent("Amy Zhang", 22, 3.95, "Mechanical Engineering", cornell);
    		System.out.println("\n---------------------- 5 ---------------------");
    		System.out.println("\n" + Person.getPersonCount() + " Person objects have been created.");
  2. Sample output:
    // ---------------------- 1 ---------------------
    // Bret Birth is 21 years old. GPA is 3.58.
    // Made Dean's honor list.
    // Major in Computer Science.
    // Attending Texas Wesleyan University
    // ---------------------- 2 ---------------------
    // Erika Smart is 25 years old. GPA is 2.3.
    // Major in Biology.
    // Attending Rice University
    // ---------------------- 3 ---------------------
    // Erika Smart is 25 years old. GPA is 2.3.
    // Major in General Business.
    // Attending Rice University
    // ---------------------- 4 ---------------------
    // Jo Miller is 19 years old. GPA is 2.85.
    // Major in Liberal Arts.
    // Attending The university name has not been provided.
    // ---------------------- 5 ---------------------
    // Amy Zhang is 22 years old. GPA is 3.95.
    // Made Dean's honor list.
    // Major in Mechanical Engineering.
    // Attending Cornell University
    // 4 Person objects have been created.