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

Chapter 10 defined a class personType to store the name of a person. The member

ID: 3855971 • Letter: C

Question

Chapter 10 defined a class personType to store the name of a person. The member functions that we included merely print the name and set the name of a person. Redefine the class personType so that, in addition to what the existing class does, you can: a) Set the first name only. b) Set the last name only. c) Store and set the middle name. d) Check whether a given first name is the same as the first name of this person. e) Check whether a given last name is the same as the last name of this person. Write the definitions of the member functions to implement the operations for this class. Also, write a program to test various operations on this class. You can download the class files from the blackboard ("Lab Assignments/Lab 8/Ch10_PersonType.zip").

Explanation / Answer

class personType

{

private String firstName,middleName,lastName;

  

public void setName(String firstName,String lastName,String middleName)

{

this.firstName = firstName;

this.middleName = middleName;

this.lastName = lastName;

}

public void printName()

{

System.out.println(firstName +" "+middleName+" "+ lastName);

}

public void setFirstName(String firstName)

{

this.firstName = firstName;   

}

public void setLastName(String lastName)

{

this.lastName = lastName;

}

public void setMiddleName(String middleName)

{

this.middleName = middleName;

}

public boolean sameFirstName(String firstName)

{

if(this.firstName == firstName)

return true;

else

return false;

  

}

public boolean sameLastName(String lastName)

{

if(this.lastName == lastName)

return true;

else

return false;

  

}

}

class TestPersonType

{

public static void main (String[] args)

{

personType person = new personType();

person.setFirstName("Mathew");

person.setMiddleName("Jack");

person.setLastName("Louis");

System.out.println(" First name is same as Chris : "+person.sameFirstName("Chris"));

System.out.println(" Last name is same as Louis :"+person.sameLastName("Louis"));

}

}

Output:

First name is same as Chris : false

Last name is same as Louis :true

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote