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

JAVA help This is a review of inheritance. Write a TitledPerson class, which you

ID: 3806809 • Letter: J

Question

JAVA help

This is a review of inheritance.

Write a TitledPerson class, which you derive from the Person class. The TitledPerson class has one additional String instance variable for a title such as Ms., Mr., or Dr. It has two constructors, a default constructor and one that sets both the name and title. It has a writeOutput method, a reset method, an equals method, an accessor method getTitle that 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.

Explanation / Answer

Person.java

public class Person {
   // Declaring variable
   private String name;

   // Zero argumented constructor
   public Person() {
   }

   // Parameterized constructor
   public Person(String name) {
       this.name = name;
   }

   // Setters and getters
   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

}

____________________

TitledPerson.java

public class TitledPerson extends Person {

   // Declaring instance variables
   private String title;

   // Zero argumented constructor
   public TitledPerson() {
       super();
       this.title = "No Title yet";
   }

   // Parameterized constructor
   public TitledPerson(String name, String title) {
       super(name);
       this.title = title;
   }

   /*
   * This method will modify the old name and titled with used supplied name
   * and title
   */
   public void reset(String newName, String newTitle) {
       setName(newName);
       title = newTitle;
   }

   // Setters and getters
   public String getTitle() {
       return title;
   }

   public void setTitle(String title) {
       this.title = title;
   }

   // This method will display the output
   public void writeOutput() {

       System.out.println("Name :" + title + "" + getName());
   }

   // this method compare two TitledPerson Objects
   public boolean equals(TitledPerson tp) {

       if (title == null) {
           if (tp.title != null)
               return false;
       } else if (!title.equals(tp.title))
           return false;
       return true;
   }

}

_______________________

Test.java

public class Test {

   public static void main(String[] args) {
       TitledPerson tp1 = new TitledPerson("Sanjay", "Mr.");
       System.out.println("---------Titled Person#1---------");
       tp1.writeOutput();

       TitledPerson tp2 = new TitledPerson("Williams", "Dr.");
       System.out.println("---------Titled Person#2---------");
       tp2.writeOutput();

       TitledPerson tp3 = new TitledPerson("Mary", "Ms.");
       System.out.println("---------Titled Person#3---------");
       tp3.writeOutput();

       System.out.println(" ______Comparing Titled Person#1 and Titled Person#2 Objects_____");
       if (tp1.equals(tp2))
           System.out.println("Titled Person#1 and Titled Person#2 are Equal");
       else
           System.out.println("Titled Person#1 and Titled Person#2 are not Equal");

       System.out.println(" _______Reseting Titled Person#2 Obejct_______");
       tp2.reset("Sanjay", "Mr.");

       System.out.println("______Comparing Titled Person#1 and Titled Person#2 Objects_____");
       if (tp1.equals(tp2))
           System.out.println("Titled Person#1 and Titled Person#2 are Equal");
       else
           System.out.println("Titled Person#1 and Titled Person#2 are not Equal");

   }

}

____________________

Output:

---------Titled Person#1---------
Name :Mr.Sanjay
---------Titled Person#2---------
Name :Dr.Williams
---------Titled Person#3---------
Name :Ms.Mary

______Comparing Titled Person#1 and Titled Person#2 Objects_____
Titled Person#1 and Titled Person#2 are not Equal

_______Reseting Titled Person#2 Obejct_______
______Comparing Titled Person#1 and Titled Person#2 Objects_____
Titled Person#1 and Titled Person#2 are Equal

______Thank You