A4 • Exceptions, inheritance

The goals of this assignment are to

  • gain experience creating classes and subclasses
  • instantiate new objects within a driver
  • use the exception mechanism to signal an abnormal event has occurred

Getting Started

Create a directory named a4 in your repository and add the following files to this directory.


Create a make file that allows you to use make to compile the program, use make run to run your program, and use make clean to remove all .class files.


Write an exception class named IllFormedCommercialBuildingException.

  • The class should have a constructor that takes a String as an argument and saves the string in a field named reason.
  • The class overrides the toString() method to returns the string “Ill formed commercial building: “ concatenated with the reason.


Write a class named CommercialBuilding.

  • The class should include fields for the number of units in the building (int) and the street address (String)
  • The class should set all of its fields in the constructor using arguments passed to the constructor.  If the number of units is less than one, an exception should be thrown with an appropriate reason.  If the String holding the address is null or empty, an exception should be thrown with an appropriate reason.


Write a class named ApartmentBuilding that is a subclass of CommercialBuilding.

  • The ApartmentBuilding class has additional field that indicates if there is a penthouse apartment in the building (boolean).
  • The constructor should set all of the class fields (including those in the superclass) using the values of the arguments passed into the constructor.
  • The constructor should re-throw any exception that it might catch from the superclass by including a throws clause.  Do not however try to catch the exception in the subclass’ constructor.  That is not allowed.


Write a class named ApartmentBuildingInventory that does the following:

  • The driver maintains an ArrayList of ApartmentBuilding objects.
  • The driver creates an infinite loop that within it does the following:
    • A menu is displayed to the user with 3 options:
      1. Add apartment building
      2. Print inventory
      3. Exit
    • If the user chooses 1, the program prompts the user to enter the necessary data to create an ApartmentBuilding object.  The program then reads in the data from the keyboard and attempts to create an ApartmentBuilding object.  If successful, the program adds the object to the inventory ArrayList.  If an exception is caught, the program prints to the screen the reason for the error.
    • If the user chooses 2, the program prints the fields of each ApartmentBuilding object to the screen, with commas between the fields, and one building on each line.
    • If the user chooses 3, the program terminates.

Compile and Test

Compile your code regularly.  When complete test your program to verify that both types

Submitting Your Work

When complete, remove all class files from your a4 directory and push your change to GitHub.

About the author