You are given the complete definition of a class called Person below. Write a ch
ID: 643011 • Letter: Y
Question
You are given the complete definition of a class called Person below. Write a child class calledTitledPerson that inherits from Person. The class TitledPerson has one additional String instance variable for a title (e.g., Ms, Mr, Dr, etc.).
Write two constructors: a default constructor and one that sets both the name and the title based on parameters.
Write a toString method that lists both the name and the title of the person.
Write appropriate getters and setters.
Write an equals method. Two TitledPersons are considered
Explanation / Answer
public class TitledPerson extends Person { private String title; public TitledPerson() { super(); title = "no title yet"; } public TitledPerson(String initialName, String initialTitle) { super(initialName); title = initialTitle; } public void reset(String newName, String newTitle) { setName(newName); title = newTitle; } public String getTitle() { return title; } public void setTitle(String newTitle) { title = newTitle; } public void writeOutput() { System.out.println("Name: " + getName()); System.out.println("Title: " + title); } public boolean equals(TitledPerson otherPerson) { return (this.sameName(otherPerson)) && (this.title.equalsIgnoreCase(otherPerson.title)); } } 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 sameName(Person otherPerson) { return (this.name.equalsIgnoreCase(otherPerson.name)); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.