Implement a class Address. An address has a house number, a street, an optional
ID: 441399 • Letter: I
Question
Implement a class Address. An address has a house number, a street, an optional apartment number, a city, a state, and a postal code. Supply two constructors: one with an apartment number and one without. Supply a print method that prints the address with the street on one line and the city, state, and zip code on the next line. Please also, create driver.java class to test address.java Please provide both the address.java and the driver.java, you can use pastebin.com to post separate codes. ThanksExplanation / Answer
//PLEASE RATE
//Address.java
public class Address {
private int HouseNumber;
private String Street;
private int AptNumber;
private String City;
private String State;
private int PostalCode;
public Address(int HouseNum,String street,int aptNum, String city,String state, int postalCode){
HouseNumber=HouseNum; Street=street; AptNumber=aptNum;
City=city; State=state; PostalCode=postalCode;
}
public Address(int HouseNum,String street, String city,String state, int postalCode){
HouseNumber=HouseNum; Street=street; AptNumber=-999;
City=city; State=state; PostalCode=postalCode;
}
public void PrintAddress(){
String str=HouseNumber+" "+Street;
if(AptNumber!=-999)
str+=" Apt #"+AptNumber;
str+=" "+City+", "+State+" "+PostalCode;
System.out.println(str);
}
}
//---------------------------------------------------------------------------------
//AddressTester.java
public class AddressTester {
public static void main(String[] args) {
Address address1=new Address(123,"Some Street",5,"Somecity","WA",12345);
Address address2=new Address(34,"Another Street","Anothercity","MA",12345);
System.out.println("address1 and address2 created with constructors...");
System.out.println(" address1: ");
address1.PrintAddress();
System.out.println(" address2: ");
address2.PrintAddress();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.