1. Create a New Project named InheritanceDemo . 2. Create a new class called Person and cut and paste the Person.java file from Blackboard into the class. Create a new class called Student and cut and paste the Student.java file from Blackboard into the class. Cut and paste the InheritanceDemo.java file from Blackboard into the InheritanceDemo class. 3. Create a new class called GraduateStudent that inherits from class Student. Add two instance variables, a String called major and a double called gpa. Add two constructors, one with no parameters and one with four. Make sure you call the super constructors for each of them. Write the accessor methods and the mutator methods for major and gpa. Write a reset() method that resets the name, studentNumber, major, and gpa. Finally, write a writeOutput() method that prints out all four values for the GraduateStudent class. 4. Uncomment the commented lines in InheritanceDemo.java. Compile the project and then execute it. What does it output?
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 InheritanceDemo
{
public static void main (String [] args)
{
Student s = new Student ();
s.setName ("Warren Peace");
s.setStudentNumber (1234);
s.writeOutput ();
/* GraduateStudent g = new GraduateStudent("Ariel Bender", 5678,
"Computer Science", 4.0);
g.writeOutput ();
g.reset ("Thin White Duke", 2468, "Music", 3.7);
g.writeOutput ();
g.setName ("Holden Caulfield");
g.setStudentNumber (1357);
g.setMajor ("Philosophy");
g.setGpa (3.0);
g.writeOutput ();
*/
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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);
}
}