Specification for Java classes are as follows : A building has a number of floor
ID: 3860527 • Letter: S
Question
Specification for Java classes are as follows:
A building has a number of floors, and a number of windows.
A house is a building.
A garage is a building. (This isn’t Florida-like … it’s a detached garage.)
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.
1. Describe the inheritance hierarchy. What inherits from what. (just class names – it's small. Include the Object class in your hierarchy).
2. Implement the specification in Java classes.
You must use the following mechanisms correctly:
Inheritance – is a
Composition – has a
Constructor methods
Accessor / mutator methods = getters, setters
Arrays of objects – you can use ArrayList if you know how
Passing arrays of objects to methods – or an ArrayList
Abstract classes and methods
Access modifiers – public, private (you might find a reason to use protected, it’s not all that common)
toString() methods – in each class
the super keyword
method overriding
method overloading
This program also requires you to demonstrate the OO concepts of:
encapsulation
polymorphism
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.
Additional specs:
Your product for the assignment is the House, Building, Room, Garage classes. The class with the main is used to test these classes. Consider that there might be a large project in the future for a real-estate company, and the classes will be put into use in that project. (There won’t be … but imagining the use of the classes will help you to make them more convenient for the user.)
No user input is required – you can hard-code some data in the main method.
Only test methods (main in this case) use 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 (ArrayList) 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. OK bigger hint here. The Building class must have an abstract method signature that imposes on all of its subclasses that they override this method to calculate the floor space.
Work incrementally. Start by making a Room class, and testing it. Then make the Building and Garage classes, test them. Then make the House class, and test it.
The constructor for the house class has an array of rooms as a parameter, i.e. you must have an array of Room[] (or an ArrayList of room objects) to call the constructor for the House
The building class is the only class that stores and manipulates the number of windows, and floors. An abstract class usually has some concrete (non-abstract) methods.
All garage objects have exactly one floor. Garages are not part of a House. Garages do NOT have a room in them. Even though the description of the Room sounds like you might be able to re-use that code in the Garage class, don’t do it, you would have to deal with closets in the Garage, and there aren’t any.
Explanation / Answer
import java.util.*;
//abstract class
abstract class building
{
//protected keyword to access elements in derived class only
protected int NoOfFloors;
protected int NoOfWindows;
//constructor
public building(int floors, int windows)
{
NoOfFloors = floors;
NoOfWindows = windows;
}
//abstract method
abstract double calculateFloorSpace();
}
class house extends building
{
private int noOfBathroom;
private List<room> rooms = new ArrayList<room>();
//constructor
public house(int floors,int windows,int bathrooms)
{
//super call base class constructor
super(floors,windows);
noOfBathroom = bathrooms;
}
//method to find average room size
public double avgRoomsSize()
{
double areaSum=0;
for(room r: rooms)
{
areaSum += (r.getlength() * r.getwidth());
}
return areaSum/rooms.size();
}
//implements base class abstract method (overrinding)
public double calculateFloorSpace()
{
double areaSum=0;
for(room r: rooms)
{
areaSum += (r.getlength() * r.getwidth());
}
return areaSum;
}
//setter for noOfBathroom
public void setnoOfBathroom(int bathrooms)
{
noOfBathroom = bathrooms;
}
//getter for noOfBathroom
public int getnoOfBathroom()
{
return noOfBathroom;
}
//add new room in to roomList
public boolean addRoom( room r ) {
rooms.add( r );
return true;
}
//to String method oveerrinding
public String toString()
{
return "house has "+NoOfFloors +" floor ,"+NoOfWindows +" windows and " +noOfBathroom +" bathroom.";
}
}
class garage extends building
{
//class attributes
private int noOfCars;
private String floorType;
private double length;
private double width;
//constructor
public garage(int floors,int windows,int cars,String type, double l,double w)
{
super(floors,windows);
noOfCars = cars;
floorType = type;
length = l;
width = w;
}
//implements base class abstract method
public double calculateFloorSpace()
{
return length*width;
}
//setter to no of car
public void setNoOfCar(int c)
{
noOfCars = c;
}
//getters to noofcar
public int getNoOfCar()
{
return noOfCars;
}
//setter for floor type
public void setfloorType(String type)
{
floorType = type;
}
//getter for floor type
public String getfloorType()
{
return floorType;
}
public String toString()
{
return "garage has" +NoOfFloors +" floor ,"+NoOfWindows +" windows and capacity of parking: "+noOfCars +" car. floor type: " +floorType + " length: "+length +" width: " +width;
}
}
class room
{
//room class attributes
private double length;
private double width;
private String floorCovering;
private int NoOfClosets;
//setter for length
public void setlength(double l)
{
length = l;
}
//getter for length
public double getlength()
{
return length;
}
//setter for width
public void setwidth(double w)
{
width = w;;
}
//getter for width
public double getwidth()
{
return width;
}
//setter for floorCovering
public void setfloorCovring(String type)
{
floorCovering = type;
}
//getter for floorCovering
public String getfloorCovring()
{
return floorCovering;
}
//setter for NoOfClosets
public void setNoOfClosets(int closetNo)
{
NoOfClosets = closetNo;
}
//getter for NoOfClosets
public int getNoOfClosets()
{
return NoOfClosets;
}
public String toString()
{
return "Rooms has length: "+length+" width: "+width+" floorcovring: "+floorCovering+" number of closet: "+NoOfClosets;
}
}
public class Test{
public static void main(String []args){
//create object house class
building b1 = new house(3,4,10);
//create object of garage class
building b2 = new garage(5,6,9,"cement floor",15, 12);
System.out.println(b1);
System.out.println(b2);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.