Question: You are given a specification for some Java classes as follows. A building has a number of floors, and a number of windows. A house
You are given a specification for some Java classes as follows.
A building has a number of floors, and a number of windows.
A house is a building.
A garage is a building.
A room has a length, width, a floor covering, and a number of closets.
You can never create an instance of a building, but every object that is a building must have a method that calculates the floor space, i.e., the Building class is abstract, and has an abstract method.
A house has a number of bathrooms, and an array of Rooms.
A house has a method that returns the average size of the Rooms.
A Garage can hold some number of cars, and it may have a cement floor or a gravel floor. A garage has a length and a width. (Dont use the Room class as a member of the Garage class.)
Object
/ \
Building Room
/ \
House Garage
2. Implement the specification.
You must use the following mechanisms correctly:
Inheritance is a
Composition has a
Constructor methods
Accessor / mutator methods = getters, setters
Arrays of objects
Passing arrays of objects to methods
Abstract classes and methods
Access modifiers public private
o might not need protected, a you should never use
toString() methods
the super keyword
method overriding
interfaces
generics are introduced
polymorphism using inheritance, and interfaces
Include a test class that has a main method. This test program will make instances of your classes and output the text representation of the objects using the toString() methods. There should be enough comments in the output so that it is easy to see which classes are being tested.
In your test class, create an ArrayList
Additional Requirements:
Only methods used for testing will call the System.out.println() method. The purpose of the classes is to store and manipulate data. Input and output is not included in the data storage and manipulation classes.
A house will calculate its floor space by looping through the array of rooms, and accumulating the floor space. Don't worry about the space used by a closet, you can assume that it is included in the size of the room.
Work incrementally. Start by making a Room class, and testing it. Then make the Building and Garage classes, test them. The make the house class, and test it.
The constructor for the house class has an array of rooms as a parameter.
The building class is the only class that stores and manipulates the number of windows, and number of floors.
All garage objects have exactly one floor. Perhaps the Building class should have an overloaded constructor to accommodate this.
Interface
Add an interface to your package. Call the interface MLSListable. That means that a class that implements this interface is a property that can be listed for sale. This interface will have only one method called getMLSListing, and it will return a nicely formatted string about the property for sale.
- The House class will implement this interface.
- In the Test class add a static method that has one parameter with a data type of MLSListable. Demonstrate that you can pass a house to that method, but a Garage, and a Room is not MLSListable so it wont compile.
==========================================================
- There are some interfaces already provided for you in the Java API. Implement the Comparable interface for your Room class. compareTo returns the difference between 2 objects as an int. There are 2 flavors of the Comparable interface. One uses generics and include the type of the objects that can be compared
class Room implements Comparable
There is an advantage to using this version of Comparable. Look up the 2 versions of Comparable, and describe why you would use this newer one.
- Override the equals method in the Room class.
- Notice the relationship between the equals method and the compareTo method. If your code indicates that two room objects are equal, but compareTo returns a non-zero value, there is a contradiction. Similarly, if compareTo indicates that two objects have a difference of 0, the equals method must return true for those 2 objects.
Also notice that a.compareTo(b) == -b.compareTo(a) must always be true.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
