1. Create a New Project named InheritanceDemo . 2. Create a new class called Per
ID: 3557652 • Letter: 1
Question
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?
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Explanation / Answer
Program Code:
public class Items//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 ();
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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);
}
}
class GraduateStudent extends Student
{
String major;
double gpa;
GraduateStudent()
{
major="";
gpa=0.0;
}
GraduateStudent(String major,double gpa,String initialName, double d)
{
String name=super.getName();
int studentNumer=super.getStudentNumber();
this.major=major;
this.gpa=gpa;
}
public void setMajor (String major)
{
this.major=major;
}
public String getMajor ()
{
return major;
}
public void setGpa (double gpa)
{
this.gpa=gpa;
}
public double getGpa()
{
return gpa;
}
public void reset (String newName, int newStudentNumber,String major,double gpa)
{
super.setName(newName);
super.setStudentNumber(newStudentNumber);
setMajor(major);
setGpa(gpa);
}
public void writeOutput ()
{
System.out.println ("Name: " + super.getName ());
System.out.println ("Student Number: " + super.getStudentNumber());
System.out.println("Major: "+major);
System.out.println("GPA: "+gpa);
}
}
Output:
Name: Warren Peace
Student Number: 1234
Name: No name yet
Student Number: 0
Major: Ariel Bender
GPA: 5678.0
Name: Thin White Duke
Student Number: 2468
Major: Music
GPA: 3.7
Name: Holden Caulfield
Student Number: 1357
Major: Philosophy
GPA: 3.0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.