A7 ‣ Overriding and Exceptions

Create a directory in your repository named a7 and include all of the files for this assignment in the a7 directory. Place all files within the a7 package.

  1. Write an Interface named Building that represents a physical structure.
    1. The interface should require access methods (getters) for the square footage (double) of the building and for the address (String).
  1. Write a class named CommercialBuilding that implements the Building interface.
    1. The CommercialBuilding class should include a field for the number of units in the building (int) along with an access method for the field.
    2. The class should set all of its fields in the constructor using arguments passed to the constructor.
  1. Write an exception class named IllFormedApartmentBuildingException.
    1. The class should have a constructor that takes a String as an argument and saves the string in a field named reason.
    2. The class has a toString() method that returns the string “Ill formed apartment building: “ concatenated with the reason
  1. Write a class named ApartmentBuilding that is a subclass of CommercialBuilding.
    1. The ApartmentBuilding class has additional field that indicates if there is a penthouse apartment in the building (boolean).
    2. The constructor should set all of the class fields using the values of the arguments passed into the constructor.
    3. The constructor should throw an IllFormedApartmentBuildingException if the address is null.
    4. The class should have access methods to get the values stored in the fields but should not contain modifier methods.
    5. The class should override equals, hashCode, and toString.
  1. Write a driver that does the following:
    1. It creates two objects of type ApartmentBuilding for the following apartment buildings:
      1. The apartment building at “35 Main Street” has 24000 square feet, 16 units, and has a penthouse apartment.
      2. The apartment building at “100 Oak Avenue” has 3000 square feet, 2 units, and does not have a penthouse apartment.
    2. Your driver should catch an IllFormedApartmentBuildingException exception and print the reason string if the exception is thrown.
    3. Add code to your driver to thoroughly test all of the methods in your classes.

 

© 2017 – 2019, Eric. All rights reserved.