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

JAVA homework! The class below describes a Person. In the next questions, you wi

ID: 3675709 • Letter: J

Question

JAVA homework!

The class below describes a Person. In the next questions, you will write a class called TitledPerson. A TitledPerson has an additional String variable (e.g., Ms., Mr., Dr., etc).

For full credit, follow good principles of class design, encapsulation, and inheritance.

I recommend writing the class in your development environment to make sure it compiles and then copying the code into the appropriate question

public class Person {

private String name;

private final static String DEFAULT_NAME = "N/A";

public Person() {

this(DEFAULT_NAME);

}
public Person(String name) {

this.name = name;

}public void setName(String name) {

this.name = name;

}public String getName() {

return name;

}
public String toString() {

return “Name: ” + name;

}
public boolean equals(Object obj) {

if(obj instanceof Person) {

Person otherPerson = (Person) obj;

return name.equalsIgnoreCase(otherPerson.getName());

} else {

return false;

}

}
}

Question 1

Write the class header and the instance data variables for the TitledPerson class.

A TitledPerson is a person that is further described by a Title.

Question 2

Write two constructors for the TitledPerson class:

a default constructor that takes no parameters

a constructor that takes in both the name and title as parameters

Question 3

Write accessor and mutator (getter and setter) methods. Include validity checking where appropriate.

Question 4

Write a toString method to return a text representation of the titled person that includes both the name and title of the person.

QUestion 5

Write an equals method. Two TitledPerson objects are considered the same(logically equivalent) if they have the same name (ignoring case) and title (ignoring case).

Question 6

Write code that would go inside a driver program with a main method.

Create an array to hold 5 Person objects

Fill the array with some Person and some TitledPerson objects

Iterate the array and print a text representation of each object

Iterate the array and count how many "Dr" titles are in the array

Use polymorphism to do this!

Explanation / Answer

main.java


   import java.util.Scanner;


   public class main {
       String newTitlePerson;
      
       public static void main(String[] args){
              
               Scanner input = new Scanner(System.in);
              
              
           System.out.println("Enter your intital name");
           String newInitialName = input.nextLine();
           //PolyShape p = new PolyShape(numofSideinput);
           System.out.println("Enter your title");
           String newTitle = input.nextLine();
       TitledPerson t = new TitledPerson(newInitialName, newTitle);
           System.out.println(t);
          
          
           System.out.println("Enter your intital name");
           String newInitialName1 = input.nextLine();
           //PolyShape p = new PolyShape(numofSideinput);
           System.out.println("Enter your title");
           String newTitle1 = input.nextLine();
       TitledPerson t1 = new TitledPerson(newInitialName1, newTitle1);
           System.out.println(t1);
      
       if (t1.equals(t))
            {
                System.out.print("The names are equal. ");
            }
            else
                System.out.print("The names are not equal. ");
       }
   }
              

TitledPerson.java

public class TitledPerson extends Person{
   private String title;
   private String fullName;
  
   public TitledPerson(){
       super();
       title = "N/A";
   }
   public TitledPerson(String initialName, String title){
       super(initialName);
       this.title = title;
       setFullName();//everytime i call the class, it automatically set the full name

   }
   public String getTitle(){
       return title;
   }
   public void setTitle(String title){
       this.title = title;
   }
   public String getFullName(){
       return fullName;
   }
   public void setFullName(){
       fullName = title +" /t"+ getName();
   }
  
   public String toString(){  
      
       return title +"." + " " + super.toString() + " " + "Full Name is " + fullName;
      
   }
   @Override
   public boolean equals(Object obj){
       /*if (obj instanceof TitledPerson){
           TitledPerson otherTitledPerson = (TitledPerson) obj;
           return (super.equals(obj) && title.equalsIgnoreCase(otherTitledPerson.getTitle()));
       }*/
       TitledPerson otherTitledPerson = (TitledPerson) obj;

       if(fullName.equals(otherTitledPerson.fullName))
           return true;
       else{
           return false;
       }
  
   }
  
}

Person.java


public class Person {
private String name;

public Person() {
name = "N/A";
}
public Person(String initialName) {
name = initialName;
}
public void setName(String newName) {
name = newName;
}
public String getName() {
return name;
}
public String toString() {
return "Name:" + name;
}
public boolean equals(Object obj) {
if(obj instanceof Person) {
Person otherPerson = (Person) obj;
return name.equalsIgnoreCase(otherPerson.getName());
} else {
return false;
}
}
}

output

Enter your intital name                                                                                                                                     
Ramchandran                                                                                                                                                 
Enter your title                                                                                                                                            
Mr                                                                                                                                                          
Mr.     Name:Ramchandran                                                                                                                                    
Full Name is Mr /tRamchandran                                                                                                                               
Enter your intital name                                                                                                                                     
kavitha                                                                                                                                                     
Enter your title                                                                                                                                            
Ms                                                                                                                                                          
Ms.     Name:kavitha                                                                                                                                        
Full Name is Ms /tkavitha                                                                                                                                   
The names are not equal.