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. (Don’t 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. Add a couple of houses and garages to your list. Loop
through the list, using the enhanced for loop. Print out the objects using the toString methods.
(The toString methods should return a String object containing all of the instance variables’
values.)
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. T.
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
You are given a specification for some Java classes as follows. A.pdf
1. 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. (Don’t 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
2. 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. Add a couple of houses and garages to your list. Loop
through the list, using the enhanced for loop. Print out the objects using the toString methods.
(The toString methods should return a String object containing all of the instance variables’
values.)
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
3. MLSListable. Demonstrate that you can pass a house to that method, but a Garage, and a Room
is not MLSListable so it won’t 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.
Solution
The Room Class:
public class Room implements Comparable {
private int length;
private int width;
private String floorCovering;
private int numberOfClosets;
public Room(int length, int width, String floorCovering, int numberOfClosets) {
this.length = length;
this.width = width;
4. this.floorCovering = floorCovering;
this.numberOfClosets = numberOfClosets;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public String getFloorCovering() {
return floorCovering;
}
public void setFloorCovering(String floorCovering) {
this.floorCovering = floorCovering;
}
public int getNumberOfClosets() {
return numberOfClosets;
}
public void setNumberOfClosets(int numberOfClosets) {
this.numberOfClosets = numberOfClosets;
}
public String toString() {
String result = " ttLength: " + this.getLength() + " Width: " + this.getWidth() + " Closets:
"
+ this.getNumberOfClosets() + " Floor Covering: " + this.getFloorCovering();
return result;
}
public int compareTo(Room arg0) {
if (this.getLength() * this.getWidth() < arg0.getLength() * arg0.getWidth()) {
return -1;
5. } else if (this.getLength() * this.getWidth() > arg0.getLength() * arg0.getWidth()) {
return 1;
} else {
return 0;
}
}
public boolean equals(Object obj) {
boolean result = false;
if (!(obj instanceof Room)) {
return false;
}
if (this.getLength() * this.getWidth() == ((Room) obj).getLength() * ((Room) obj).getWidth()) {
result = true;
}
return result;
}
}
Building Class:
// The class is abstract so that it can't be instantiated
public abstract class Building {
// Number of floors in the building
private int numberOfFloors;
// Number of Windows in the building
private int numberOfWindows;
// The required Abstract Method
public abstract int floor_space();
// Getter and Setters for the class
public int getNumberOfFloors() {
return numberOfFloors;
}
public void setNumberOfFloors(int numberOfFloors) {
this.numberOfFloors = numberOfFloors;
6. }
public int getNumberOfWindows() {
return numberOfWindows;
}
public void setNumberOfWindows(int numberOfWindows) {
this.numberOfWindows = numberOfWindows;
}
// String Representation for the class
public String toString() {
String result = "I am a Building!";
return result;
}
}
Garage Class:
public class Garage extends Building {
private int numberOfCars;
private String floorCovering;
private int length;
private int width;
public Garage(int numberOfWindows, int length, int width, int numberOfCars, String
floorCovering) {
super.setNumberOfFloors(1);
super.setNumberOfWindows(numberOfWindows);
this.length = length;
this.width = width;
this.numberOfCars = numberOfCars;
this.floorCovering = floorCovering;
}
public int floor_space(){
return this.length*this.width;
}
public int getNumberOfCars() {
return numberOfCars;
}
public void setNumberOfCars(int numberOfCars) {
7. this.numberOfCars = numberOfCars;
}
public String getFloorCovering() {
return floorCovering;
}
public void setFloorCovering(String floorCovering) {
this.floorCovering = floorCovering;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public String toString() {
String result = "I am a Garage!" + " tLength: " + this.getLength() + " tWidth: " +
this.getWidth()
+ " tFloor Covering: " + this.getFloorCovering() + " tNumber of Cars: " +
this.getNumberOfCars()
+ " ";
return result;
}
}
House Class:
public class House extends Building implements MLSListable {
private Room[] rooms;
private int numberOfBathrooms;
public House(Room[] rooms, int numberOfBathrooms, int numberOfWindows, int
numberOfFloors) {
8. super.setNumberOfFloors(numberOfFloors);
super.setNumberOfWindows(numberOfWindows);
this.numberOfBathrooms = numberOfBathrooms;
this.rooms = rooms;
}
public String genRoomInfo() {
String result = "";
for (int i = 0; i < rooms.length; i++) {
result += rooms[i].toString();
}
return result;
}
public Room[] getRooms() {
return rooms;
}
public void setRooms(Room[] rooms) {
this.rooms = rooms;
}
public int getNumberOfBathrooms() {
return numberOfBathrooms;
}
public void setNumberOfBathrooms(int numberOfBathrooms) {
this.numberOfBathrooms = numberOfBathrooms;
}
// Abstract MEthod from the Building Class
public int floor_space(){
int result;
int sum = 0;
for (int i = 0; i < this.rooms.length; i++) {
sum += rooms[i].getLength() * rooms[i].getWidth();
}
return sum;
}
public int getAvgRoomSize() {
int result;
9. int sum = 0;
for (int i = 0; i < this.rooms.length; i++) {
sum += rooms[i].getLength() * rooms[i].getWidth();
}
result = sum / this.rooms.length;
return result;
}
public String toString() {
String result = "I am a House!" + " tAverage Room Size: " + this.getAvgRoomSize() + "
tBathrooms: "
+ this.getNumberOfBathrooms() + " tFloors: " + this.getNumberOfFloors() + "
tWindows: "
+ this.getNumberOfWindows() + " tRooms: " + this.rooms.length + this.genRoomInfo() + "
";
return result;
}
public String getMLSListing() {
return "MLSListing - " + this.toString();
}
}
Then Finally the Test Class:
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList buildingsArray = new ArrayList();
Room room1 = new Room(10, 30, "Marble", 1);
Room room2 = new Room(30, 50, "Marble", 1);
Room room3 = new Room(40, 20, "Wood", 2);
Room room4 = new Room(10, 50, "Wood", 4);
10. Room[] rooms11 = { room1, room2 };
Room[] rooms22 = { room3, room4 };
House house1 = new House(rooms1, 4, 3, 2);
House house2 = new House(rooms2, 1, 2, 1);
Garage garage1 = new Garage(1, 5, 5, 2, "Cement");
Garage garage2 = new Garage(1, 6, 6, 3, "Gravel");
buildingsArray.add(house1);
buildingsArray.add(house2);
buildingsArray.add(garage1);
buildingsArray.add(garage2);
for (Building aBuilding : buildingsArray) {
if (aBuilding instanceof House) {
housePassMethod((House)aBuilding);
} else {
System.out.println(aBuilding.toString());
}
}
System.out.println("Room1 compared to Room2: " + room1.compareTo(room2));
if (room1.equals(room2)) {
System.out.println("Room1 is equal to Room2 in size.");
} else {
System.out.println("Sizes differ.");
}
}
public static void housePassMethod(MLSListable house) {