Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1 Give a complete definition of a class called TitledPerson, which you derive fr

ID: 3915068 • Letter: 1

Question

1 Give a complete definition of a class called TitledPerson, which you derive from the class Person. The class TitledPerson has one additional String instance variable for a title, such as Ms., Mr., or The Honorable. The class TitledPerson has two constructors: a default constructor and one that sets both the name and the title. It has a writeOutput method, a reset method, an equals method, an accessor method getTitlethat returns the title, and a mutator method setTitle that changes the person's title. For two titled people to be equal, they must have the same name and the same title. You may want to use the class Student as a model. 2. Add a constructor to the class Student that sets the student's name to a given argument string and sets the student's number to zero. Your constructor should invoke another constructor in Student to accomplish this. 3 Rewrite the definition of the method writeOutput for the class Undergraduate, using getName and getStudentNumber instead of super.writeOutput. 4. Rewrite the definition of the method reset for the class Undergraduate in, using setName and setStudentNumber instead of the overloaded reset method name. 5. Can an object be referenced by variables of several different data types? 6. What is the type or types of the variable(s) that can reference the object created in the following statement? Undergraduate ug new Undergraduate); 7. Describe two uses for the keyword super. 8. What is the difference between this and super when these words are used within a constructor definition as the names of methods that are called?

Explanation / Answer

1)

public class TtitledPerson extends Person{

private String title;

public TitledPerson(){

}

public TitledPerson(String name,String title){

super(name);

this.title=title;

}

public boolean equals(TitledPerson tp1){

if(tp1.getName().equalsIgnoreCase(this.getName()) && tp1.getTitle().equalsIgnoreCase(this.getTitle()){

return true;

}

else{

return false;

}

}

public String getTitle(){

return this.title;

}

public void setTitle(String title){

this.title=tile;

}

public String toString(){

return "Name is "+this.getName()+"title is"+this.title;

}

}

2)

public class Student(){

private String name;

private int studentNumber;

public Student(String name,int no){

this.studentNumber=no;

}

public Student(String name){

this(name,0);//calls above constructor to initialize name and no as 0

}

}

3,4)

public class UnderGraduate(){

public String getName(){

return name;

}

public int getStudentNumber(){

return studentNumber;

}

public void setName(String name){

this.name=name;

}

public void setStudentNumber(String no){

this.studentNumber=no;

}

}

5)no an object can only be refernced by variables of its class type and base class type .

6)undergraduate and student class type variables can be used as reference variables.

7)super() keyword can be used to call parent class constructor as well as methods(public and protected only)

8)this is used to call constructor or method belonging to same class whereas super is used to call parent class methods and constructors.