Write a class called FullName that represents a persons full name. It must have
ID: 3927798 • Letter: W
Question
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 toString() 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 classes.
Explanation / Answer
public class FullName {
String title, firstName, middleName, lastName;
public FullName(String title, String firstName, String middleName,
String lastName) {
super();
this.title = title;
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
}
/**
* @return the title
*/
public String getTitle() {
return title;
}
/**
* @param title
* the title to set
*/
public void setTitle(String title) {
this.title = title;
}
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName
* the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return the middleName
*/
public String getMiddleName() {
return middleName;
}
/**
* @param middleName
* the middleName to set
*/
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName
* the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "FullName [title=" + title + ", firstName=" + firstName
+ ", middleName=" + middleName + ", lastName=" + lastName + "]";
}
}
public class MailingAddress {
FullName fullName;
String streetAddress, city, province, postalCode;
public MailingAddress(FullName fullName, String streetAddress, String city,
String province, String postalCode) {
super();
this.fullName = fullName;
this.streetAddress = streetAddress;
this.city = city;
this.province = province;
this.postalCode = postalCode;
}
/**
* @return the fullName
*/
public FullName getFullName() {
return fullName;
}
/**
* @param fullName
* the fullName to set
*/
public void setFullName(FullName fullName) {
this.fullName = fullName;
}
/**
* @return the streetAddress
*/
public String getStreetAddress() {
return streetAddress;
}
/**
* @param streetAddress
* the streetAddress to set
*/
public void setStreetAddress(String streetAddress) {
this.streetAddress = streetAddress;
}
/**
* @return the city
*/
public String getCity() {
return city;
}
/**
* @param city
* the city to set
*/
public void setCity(String city) {
this.city = city;
}
/**
* @return the province
*/
public String getProvince() {
return province;
}
/**
* @param province
* the province to set
*/
public void setProvince(String province) {
this.province = province;
}
/**
* @return the postalCode
*/
public String getPostalCode() {
return postalCode;
}
/**
* @param postalCode
* the postalCode to set
*/
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "MailingAddress [fullName=" + fullName + ", streetAddress="
+ streetAddress + ", city=" + city + ", province=" + province
+ ", postalCode=" + postalCode + "]";
}
}
public class ShippingLabel {
MailingAddress shipFrom, shipTo;
public ShippingLabel(MailingAddress shipFrom, MailingAddress shipTo) {
super();
this.shipFrom = shipFrom;
this.shipTo = shipTo;
}
/**
* @return the shipFrom
*/
public MailingAddress getShipFrom() {
return shipFrom;
}
/**
* @param shipFrom
* the shipFrom to set
*/
public void setShipFrom(MailingAddress shipFrom) {
this.shipFrom = shipFrom;
}
/**
* @return the shipTo
*/
public MailingAddress getShipTo() {
return shipTo;
}
/**
* @param shipTo
* the shipTo to set
*/
public void setShipTo(MailingAddress shipTo) {
this.shipTo = shipTo;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "shipFrom=" + shipFrom + ", shipTo=" + shipTo;
}
}
public class ShippingLabelTest {
public static void main(String[] args) {
FullName toName = new FullName("Mr", "Raj", "Kumar", "P");
FullName fromName = new FullName("Mr", "Pavan", "Kumar", "K");
MailingAddress addressTo = new MailingAddress(toName, "nearT", "VSP",
"Prov", "530018");
MailingAddress addressFrom = new MailingAddress(fromName, "nearF",
"VSP", "Prov", "530008");
ShippingLabel label = new ShippingLabel(addressFrom, addressTo);
System.out.println(label);
}
}
OUTPUT:
shipFrom=MailingAddress [fullName=FullName [title=Mr, firstName=Pavan, middleName=Kumar, lastName=K], streetAddress=nearF, city=VSP, province=Prov, postalCode=530008], shipTo=MailingAddress [fullName=FullName [title=Mr, firstName=Raj, middleName=Kumar, lastName=P], streetAddress=nearT, city=VSP, province=Prov, postalCode=530018]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.