Ask 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 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.

+3
Answers (1)
  1. 3 February, 19:13
    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?
Find 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 the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers