Consider a class PersonAddress that represents am entry in an address book. It\'
ID: 3672896 • Letter: C
Question
Consider a class PersonAddress that represents am entry in an address book. It's attributes are:
The first name of the person
The last name of the person
The e-mail address of the person
The telephone number of the person
It will have methods to:
Access each attribute
Change the e-mail address
Change the telephone number
Test whether two instances are equal based solely on the name
Write a method heading for each method
Write preconditions and post conditions for each method
Write some Java statements that test the class
Implement the class
Explanation / Answer
given below are the code with comments that help to understand the working of code:
Public class PersonAddress
// class declaration //
{//Instance variable declarations
private String firstName,lastName;
private String email;
private String telephoneNum;
//Method define//
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public String getEmail()
{
return email;
}
public String getTelephoneNum()
{
return telephoneNum;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public void setEmail(String email)
{
this.email = email;
}
public void setTelephoneNum(String telephoneNum)
{
this.telephoneNum = telephoneNum;
}
public void changeEmail(String newMail)
{
if(email.equals(newMail))
{
System.out.println("Email is not Changed:");
}
else
{
email = newMail;
}
}
public void changeTelephone(String newTelep)
{
if(telephoneNum == newTelep)
{
System.out.println("Telephone number is not:");
}
else
{
telephoneNum = newTelep;
}
}
// Checks equality//
public boolean equal(Object obj) {
// same object//
if (this == obj)
return true;
if (obj == null)
return false;
if (obj instanceof PersonAddress)
{
PersonAddress address = (PersonAddress) obj;
if (firstName.equals(address.firstName) && lastName.equals(address.lastName) )
return true;
}
return false;
}
}
class PersonAddressTest
{//test class//
public static void main(String args[])
{
PersonAddress Pa1 = new PersonAddress();
Pa1.setFirstName("Rohan");
Pa1.setLastName("Sharma");
Pa1.setEmail("Rohit@gmail.com");
Pa1. setTelephoneNum("885670708");
PersonAddress Pa2 = new PersonAddress();
Pa2.setFirstName("ms");
Pa2.setLastName("dhoni");
Pa2.setEmail("dhoni@gmail.com");
Pa2. setTelephoneNum("67546504732");
//changing rechord//
System.out.println(Pa1.toString());
Pa1.changeEmail("john.zx@gmail.com");
Pa1.changeTelephone("123828322");
System.out.println(" rechord after updation: "+Pa1.toString());
System.out.println();
System.out.println(Pa2.toString());
Pa2.changeEmail("ms.dhoni@gmail.com");
Pa2.changeTelephone("8765290765");
System.out.println(" rechord after updation : "+Pa2.toString());
System.out.println();
//check two updated rechord are equal or not//
System.out.println("is pa1 and pa2 are Equal ? : "+Pa1.equals(Pa2));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.