Question
Modify the student class to include a private variable for graduation year. Modify the person class to include a private variable for the birthdate. Modify
Modify the student class to include a private variable for graduation year.
Modify the person class to include a private variable for the birthdate.
Modify the undergraduate class to include a private variable the major.
Modify the polymorphism class to ask the user the following:
How many people?
Read the names?
Ask is the person a student? Read the student ID.
if the person is a student, you have to ask : is the person undergraduate student or not? if yes, read the level.
Print out your final people array.
public class PolymorphismDemo { public static void main(String[] args) { Person[] people = new Person[4]; people[0] = new Undergraduate("Cotty, Manny", 4910, 1); people[1] = new Undergraduate("Kick, Anita", 9931, 2); people[2] = new Student("DeBanque, Robin", 8812); people[3] = new Undergraduate("Bugg, June", 9901, 4); for (Person p : people) { p.writeOutput(); System.out.println(); } } }
public class UndergraduateDemo { public static void main(String[] args) { Undergraduate ug1 = new Undergraduate("James Bond", 007, 1); ug1.writeOutput(); ug1.reset("Sam Spade", 1940, 2); System.out.println("ug1 is:"); ug1.writeOutput(); Undergraduate ug2 = new Undergraduate("James Bond", 007, 1); System.out.println(" ug2 is:"); ug2.writeOutput(); if (ug1.equals(ug2)) System.out.println("Same students."); else System.out.println("Not the same students."); //hasSameName inherited from Student, which inherited it from Person. if (ug1.hasSameName(ug2)) System.out.println("Same names."); else System.out.println("Not the same names."); Undergraduate ug3 = new Undergraduate("James Bond", 007, 1); System.out.println(" ug3 is:"); ug3.writeOutput(); if (ug3.equals(ug2)) System.out.println("Same students."); else System.out.println("Not the same students."); } }
public class Undergraduate extends Student { private int level; //1 for freshman, 2 for sophomore, //3 for junior, or 4 for senior. public Undergraduate( ) { super( ); level = 1; } public Undergraduate(String initialName, int initialStudentNumber, int initialLevel) { super(initialName, initialStudentNumber); setLevel(initialLevel); //Checks 1 <= initialLevel <= 4 } public void reset(String newName, int newStudentNumber, int newLevel) { reset(newName, newStudentNumber); //Student s reset setLevel(newLevel); //Checks 1 <= newLevel <= 4 } public int getLevel( ) { return level; } public void setLevel(int newLevel) { if ((1 <= newLevel) && (newLevel <= 4)) level = newLevel; else { System.out.println("Illegal level!"); System.exit(0); } } public void writeOutput( ) { super.writeOutput( ); System.out.println("Student Level: " + level); } public boolean equals(Undergraduate otherUndergraduate) { return equals((Student)otherUndergraduate) && (this.level == otherUndergraduate.level); } /* // Alternate version public boolean equals(Undergraduate otherUndergraduate) { return super.equals(otherUndergraduate) && (this.level == otherUndergraduate.level); } */
}
public class Person { private String name; public Person( ) { name = "No name yet"; } public Person(String initialName) { name = initialName; } public void setName(String newName) { name = newName; } public String getName( ) { return name; } public void writeOutput( ) { System.out.println("Name: " + name); } public boolean hasSameName(Person otherPerson) { return this.name.equalsIgnoreCase(otherPerson.name); } }
public class Student extends Person { private int studentNumber; public Student( ) { super( ); studentNumber = 0;//Indicating no number yet } public Student(String initialName, int initialStudentNumber) { super(initialName); studentNumber = initialStudentNumber; } public void reset(String newName, int newStudentNumber) { setName(newName); studentNumber = newStudentNumber; } public int getStudentNumber( ) { return studentNumber; } public void setStudentNumber(int newStudentNumber) { studentNumber = newStudentNumber; } public void writeOutput( ) { System.out.println("Name: " + getName( )); System.out.println("Student Number: " + studentNumber); } public boolean equals(Student otherStudent) { return this.hasSameName(otherStudent) && (this.studentNumber == otherStudent.studentNumber); } public String toString( ) { return "Name: " + getName( ) + " Student number: " + studentNumber; } /* //For Optional Section public boolean equals(Object otherObject) { if (otherObject == null) return false; else if (!(otherObject instanceof Student)) return false;
else { Student otherStudent = (Student)otherObject; return (this.sameName(otherStudent) && (this.studentNumber == otherStudent.studentNumber)); } } */
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started