In: Computer Science
Implement a class Address. An address has a house number, a street, an optional apartment number, a city, a state, and a postal code (all of these are strings). Create the getters and setters.
Supply two constructors that accept all the address components except one includes an apartment number and one without (refer to the tester class below to see the proper order).
Supply a print method that returns void and prints to the console the address with the street on one line and the city, state, and zip code on the next line.
Use my addressTester file attached here to verify it works correctly. You shouldn't make any changes to the addressTester file. Make changes to your class so that it works with my addressTester.java file. If your class file doesn't work with my tester class, you will receive no points.
Clarification: All components of the Address should print - however, an apartment number should only print if one is provided. Your method should not print null if no apartment number is provided.
Given Tester
public class AddressTester {
//provided by instructor
public static void main(String[] args) {
Address a1 = new
Address("123","Main Street","Los Angeles", "CA","90001");
System.out.print(Address);
System.out.println("----------------");
Address condo = new Address("5643",
"Hideaway Mountain", "3C","Nashville", "TN", "37115");
condo.print();
}
}
code:
[11:44 am, 30/03/2020] Chinna: public class Address {
private String houseNumber;
private String street;
private String apartmentNumber;
private String city;
private String state;
private String postalCode;
public Address(String houseNumber, String street, String city,
String state, String postalCode) {
this.houseNumber = houseNumber;
this.street = street;
this.city = city;
this.state = state;
this.postalCode = postalCode;
}
public Address(String houseNumber, String street, String
apartmentNumber, String city, String state, String postalCode)
{
this.houseNumber = houseNumber;
this.street = street;
this.apartmentNumber = apartmentNumber;
this.city = city;
this.state = state;
this.postalCode = postalCode;
}
public String getHouseNumber() {
return houseNumber;
}
public void setHouseNumber(String houseNumber) {
this.houseNumber = houseNumber;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getApartmentNumber() {
return apartmentNumber;
}
public void setApartmentNumber(String apartmentNumber) {
this.apartmentNumber = apartmentNumber;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public void print() {
System.out.println("Street: " + street);
System.out.println("City: " + city + ", State: " + state + ", ZIP:
" + postalCode);
}
}
output:
note:
ask any doubts
give me up vote:)