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

1. The following interface and classes have to do with the storage of informatio

ID: 3747143 • Letter: 1

Question

1. The following interface and classes have to do with the storage of information that may appear on a mailing label. Design and implement each of the described classes below.

The items in bold are items that are a required part of every address. All other items are optional.

public interface AddrLabelInterface {

String getAttnName();

String getTitle(); // Mr., Mrs., etc.

String getName();

String getNameSuffix(); // e.g., Jr., III

String getProfessionalSuffix(); // O.D. (doctor of optometry)

String getStreet();

String getSuiteNum();

String getCity();

String getState();

String getZipCode();

}

Step 1 Create an AddrLabel class that implements the AddrLabelInterface, and that has an instance variable for each of the items of an address in bold above. (Thus, the class should have a constructor that is passed a value for each of these.) Implement each method in bold to return the corresponding stored value. Implement the remaining methods (e.g., getAttnName()) to just return an empty string.

Step 2 Create a class named FriendAddrLabel declared as a subclass of the AddrLabel class. There is no implementation needed for this class other than a constructor, since FriendAddrLabel objects only contain the information required of all AddrLabel objects (i.e., name, street, city, state and zip). This constructor should pass along these values to the AddrLabel class via a call to its constructor.

Step 3 Create a class named CompanyAddrLabel declared as a subclass of the abstract AddrLabel class. The CompanyAddrLabel class should be designed to store only the AttnName, and override the getAttnName method inherited from the AddrLabel class.

e.g., ATTN: Rebecca Rollins A1 Technology 92 Autumn Drive Keene, NH 03431 2

Step 4 Create a class named ProfessionalAddrLabel declared as a subclass of the abstract AddrLabel class. The ProfessionalAddrLabel class should be designed to store only the ProfessionalSuffix and SuiteNum values, and override the getProfessionalSuffix and getSuiteNum methods inherited from the AddrLabel class.

e.g., Sarah K. Phillips, O.D. 100 Oak Street, Suite 904 Omaha, NE 68007

2. Design and implement a class named LabelGenerator that is constructed to contain an object of type AddrLabelInterface. The class should contain the following method (in addition to any other supporting methods), public String[] getLabelLines() that returns as an array of strings the address lines as they should be printed for the given address label type.

3. Create a simple main program that creates a number of address object types, and stores in an array of type AddrLabelInteface. Using a simple for loop (“enhanced for loop”), display each label one by one in the array by call to method getLabelLines for each. The polymorphism is in the various ways that each AddrLabel label behaves.

Explanation / Answer

/* AddrLabelInterface which contains given declaration of method */

public interface AddrLabelInterface {

String getAttnName();

String getTitle(); // Mr., Mrs., etc.

String getName();

String getNameSuffix(); // e.g., Jr., III

String getProfessionalSuffix(); // O.D. (doctor of optometry)

String getStreet();

String getSuiteNum();

String getCity();

String getState();

String getZipCode();

}

//AddrLabel class that implements the AddrLabelInterface, and that has an instance variable for each of the items

public abstract class AddrLabel implements AddrLabelInterface{

String Title;

String Name;

String NameSuffix;

String ProfessionalSuffix;

String Street;

String SuiteNum;

String City;

String State;

String ZipCode;

String AttnName;

AddrLabel(String Name,String Street,String City,String State,String ZipCode){// constructor for mandatory fields of addsress

this.Name=Name;

this.Street=Street;

this.City=City;

this.State=State;

this.ZipCode=ZipCode;

}

// setting different values according to the costructor values null if not in required constructor

@Override

public String getTitle() {

// TODO Auto-generated method stub

return null;

}

@Override

public String getName() {

// TODO Auto-generated method stub

return Name;

}

@Override

public String getNameSuffix() {

// TODO Auto-generated method stub

return null;

}

@Override

public String getProfessionalSuffix() {

// TODO Auto-generated method stub

return null;

}

@Override

public String getStreet() {

// TODO Auto-generated method stub

return Street;

}

@Override

public String getSuiteNum() {

// TODO Auto-generated method stub

return null;

}

@Override

public String getCity() {

// TODO Auto-generated method stub

return City;

}

@Override

public String getState() {

// TODO Auto-generated method stub

return State;

}

@Override

public String getZipCode() {

// TODO Auto-generated method stub

return ZipCode;

}

@Override

public String getAttnName() {

// TODO Auto-generated method stub

return null;

}

}

//FriendAddrLabel declared as a subclass of the AddrLabel class

class FriendAddrLabel extends AddrLabel {

FriendAddrLabel(String Name, String Street, String City, String State, String ZipCode) {

super(Name, Street, City, State, ZipCode);// super method is required to address its parents class

}

}

// CompanyAddrLabel class declared as a subclass of the abstract AddrLabel class

public class CompanyAddrLabel extends AddrLabel{

// private AddrLabel al;

CompanyAddrLabel(String Name, String Street, String City, String State, String ZipCode) {

super(Name, Street, City, State, ZipCode);// super method is required to address its parents class

// TODO Auto-generated constructor stub

}

String AttnName;

@Override

public String getAttnName() {// overiding getAttnName method of its parent class

// TODO Auto-generated method stub

return AttnName;

}

}

//ProfessionalAddrLabel class declared as a subclass of the abstract AddrLabel class

public class ProfessionalAddrLabel extends AddrLabel {

ProfessionalAddrLabel(String Name, String Street, String City, String State, String ZipCode) {

super(Name, Street, City, State, ZipCode); // super method is required to address its parents class

// TODO Auto-generated constructor stub

}

String ProfessionalSuffix;

String SuiteNum;

public String getProfessionalSuffix() {// overiding getProfessionalSuffix method of its parent class

// TODO Auto-generated method stub

return ProfessionalSuffix;

}

public String getSuiteNum() {//// overiding getSuiteNum method of its parent class

// TODO Auto-generated method stub

return SuiteNum;

}

}

//LabelGenerator Class

public class LabelGenerator {

//As AddrLabelInterface is an interface its direct object cannot be created

//In order to create an object of AddrLabelInterface we must instatinate each of its implentation to get the value required

AddrLabel al= new FriendAddrLabel("name","street","city","street","zip");//for setting of the interface object

//AddrLabel com11 = new CompanyAddrLabel("name","street","city","street","zip");

CompanyAddrLabel cl= new CompanyAddrLabel("name","street","city","street","zip");//it must use same parameters as al object to

//cl.AttnName="AttnName";//correctly identify the al label objects

String AttnName=cl.getAttnName();// getting attnName from the cl object

ProfessionalAddrLabel pl = new ProfessionalAddrLabel("name","street","city","street","zip");////it must use same parameters as al object to

String ProfessionalSuffix=pl.getProfessionalSuffix();////correctly identify the al label objects

String SuiteNum =pl.getSuiteNum();// getting the attributes of pl object

AddrLabelInterface alooo= new AddrLabelInterface() {// setting the value of AddrLabelInterface by using its instances

@Override

public String getZipCode() {

// TODO Auto-generated method stub

return al.ZipCode;

}

@Override

public String getTitle() {

// TODO Auto-generated method stub

return al.getTitle();

}

@Override

public String getSuiteNum() {

// TODO Auto-generated method stub

return SuiteNum;

}

@Override

public String getStreet() {

// TODO Auto-generated method stub

return al.getCity();

}

@Override

public String getState() {

// TODO Auto-generated method stub

return al.getState();

}

@Override

public String getProfessionalSuffix() {

// TODO Auto-generated method stub

return ProfessionalSuffix;

}

@Override

public String getNameSuffix() {

// TODO Auto-generated method stub

return al.getNameSuffix();

}

@Override

public String getName() {

// TODO Auto-generated method stub

return al.getName();

}

@Override

public String getCity() {

// TODO Auto-generated method stub

return al.getCity();

}

@Override

public String getAttnName() {

// TODO Auto-generated method stub

return AttnName;

}

};

public String[] getLabelLines(){//Geting label lines from AddrLabelInterface object

String[] altoReturn= new String[777];

for(int i=0;i<10;i++){

altoReturn[i]=alooo.getName();

altoReturn[i+1]=alooo.getStreet();

altoReturn[i+2]=alooo.getCity();

altoReturn[i+3]=alooo.getState();

altoReturn[i+4]= alooo.getZipCode();

}

return altoReturn;

}

}

//Main Class

import java.util.ArrayList;

public class MainA {

public static void main(String[] args) {

//As AddrLabelInterface is an interface its direct object cannot be created

//In order to create an object of AddrLabelInterface we must instatinate each of its implentation to get the value required

AddrLabel al= new FriendAddrLabel("name","street","city","street","zip");//for setting of the interface object

//AddrLabel com11 = new CompanyAddrLabel("name","street","city","street","zip");

CompanyAddrLabel cl= new CompanyAddrLabel("name","street","city","street","zip");//it must use same parameters as al object to

//cl.AttnName="AttnName";//correctly identify the al label objects

String AttnName=cl.getAttnName();// getting attnName from the cl object

ProfessionalAddrLabel pl = new ProfessionalAddrLabel("name","street","city","street","zip");////it must use same parameters as al object to

String ProfessionalSuffix=pl.getProfessionalSuffix();////correctly identify the al label objects

String SuiteNum =pl.getSuiteNum();// getting the attributes of pl object

AddrLabelInterface alooo= new AddrLabelInterface() {// setting the value of AddrLabelInterface by using its instances

@Override

public String getZipCode() {

// TODO Auto-generated method stub

return al.ZipCode;

}

@Override

public String getTitle() {

// TODO Auto-generated method stub

return al.getTitle();

}

@Override

public String getSuiteNum() {

// TODO Auto-generated method stub

return SuiteNum;

}

@Override

public String getStreet() {

// TODO Auto-generated method stub

return al.getCity();

}

@Override

public String getState() {

// TODO Auto-generated method stub

return al.getState();

}

@Override

public String getProfessionalSuffix() {

// TODO Auto-generated method stub

return ProfessionalSuffix;

}

@Override

public String getNameSuffix() {

// TODO Auto-generated method stub

return al.getNameSuffix();

}

@Override

public String getName() {

// TODO Auto-generated method stub

return al.getName();

}

@Override

public String getCity() {

// TODO Auto-generated method stub

return al.getCity();

}

@Override

public String getAttnName() {

// TODO Auto-generated method stub

return AttnName;

}

};

ArrayList<AddrLabelInterface> aLi = new ArrayList<>();

LabelGenerator lg= new LabelGenerator();//printing the arrayList of containing AddrLabelInterface using getLabelLines method

for(int i=0;i<aLi.size();i++){

System.out.println(i+1+" "+lg.getLabelLines());

}

}

}