Program 2 Write a class called FullName that represents a persons full name. It
ID: 3912551 • Letter: P
Question
Program 2 Write a class called FullName that represents a persons full name. It must have separate fields for title (e.g., Mr., Mrs., Ms.), first name, middle name, and last name. Override the toString) method to return a nicely formatted name. Create as many methods as you think necessary. Write a class called MailingAddress that represents a mailing address. It must have separate fields for a FullName object, street address, city, province and postal code. Other than FullName all other fields are Strings. Override the toString0 method to return a nicely formatted address. Create as many methods as you think necessary. Write a class called ShippingLabel that consists of ship-from and ship-to MailingAddress objects. Write a single method that prints the label to the console. Use these statements in the method: ShippingLabel label new Shippinglabel... your parameter list. System.out.println(label) Write a simple test program in the main method of ShippingLabel to test the above classesExplanation / Answer
class FullName
{
private String title,firstName,middleName,lastName;
public FullName(String title,String firstName,String middleName,String lastName)//constructor
{
this.title = title;
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
}
public String toString()
{
return " "+title+" "+firstName+" "+middleName+" "+lastName;
}
}
class MailingAddress
{
private FullName name;
private String streetAddress,city,province,postalCode;
//constructor
public MailingAddress(FullName name,String streetAddress,String city,String province,String postalCode)
{
this.name = name;
this.streetAddress = streetAddress;
this.city = city;
this.province = province;
this.postalCode = postalCode;
}
public String toString()
{
return ""+name +" "+streetAddress+" "+city+" "+province+" "+postalCode;
}
}
class ShippingLabel
{
private MailingAddress shipFrom,shipTo;
//constructor
public ShippingLabel(MailingAddress shipFrom,MailingAddress shipTo)
{
this.shipFrom = shipFrom;
this.shipTo = shipTo;
}
public String toString()
{
return " Ship From : "+shipFrom+" Ship To : "+shipTo;
}
}
class Test
{
public static void main (String[] args)
{
FullName name = new FullName("Mr.","Andrew","Paul","Johnson");
FullName name1 = new FullName("Mrs.","Catherine","J.","James");
MailingAddress shipTo = new MailingAddress(name," 456 downstreet","hillside","New Jersey","7687-788");
MailingAddress shipFrom = new MailingAddress(name1,"879 new Street","NY","NY","879-7878");
ShippingLabel label = new ShippingLabel(shipTo,shipFrom);
System.out.println(label);
}
}
Output:
hip From :
Mr. Andrew Paul Johnson 456 downstreet hillside New Jersey 7687-788
Ship To :
Mrs. Catherine J. James 879 new Street NY NY 879-7878
Do ask if any doubt. Please upvote.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.