Ask Question
23 August, 10:40

An address has a house number, a street, an optional apartment number, a city, a state and a postal code. Define the constructor such that an object can be created in one of two ways: with an apartment number or without. Supply a print method that prints the address with the street on one line and the city, state and postal code on the next line.

+4
Answers (1)
  1. 23 August, 12:30
    0
    public class Address {

    private int houseNumber;

    private String street;

    private int apartmentNumber;

    private String city;

    private String state;

    private String postalCode;

    //First Constructor with an Apartment Number

    public Address (int houseNumber, String street, int 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;

    }

    / / Second Constructor Without Apartment Number

    public Address (int houseNumber, String street, String city, String state, String postalCode) {

    this. houseNumber = houseNumber;

    this. street = street;

    this. city = city;

    this. state = state;

    this. postalCode = postalCode;

    }

    / / Creating Getter Methods

    public int getHouseNumber () {

    return houseNumber;

    }

    public String getStreet () {

    return street;

    }

    public int getApartmentNumber () {

    return apartmentNumber;

    }

    public String getCity () {

    return city;

    }

    public String getState () {

    return state;

    }

    public String getPostalCode () {

    return postalCode;

    }

    //A Print Method to print an Address

    public void printAddress () {

    System. out. println (getStreet () + " Street"); / /First line of the address

    System. out. println (getCity () + ", "+getState () + " State, "+getPostalCode ()); //Second line of the address

    }

    }

    Explanation:

    In Java, A class is created called Address

    The house number, street, apartment number, city, state and postal code are all created as the class member variables (fields)

    Two constructors are created. The first initializes all the fields so an object of the class contains all the fields. The second does not initialize the optional apartment number. This technique is called constructor overloading

    Getter methods are created for all the fields

    A method printAddress () is created that prints as stipulated by the question. Do pay attention to the comments in the code
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “An address has a house number, a street, an optional apartment number, a city, a state and a postal code. Define the constructor such that ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers