Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

i have to override the Equals method in the ROOM CLASS. I listed the following code in PYTHON. Please no generic pictures of a computer

i have to override the Equals method in the ROOM CLASS. I listed the following code in PYTHON. Please no generic pictures of a computer monitor. Thx.

The BUILDING class is directly under the Room CLASS

This is the problem in java

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 alength 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

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. 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.

This is the code the problem is i keep getting "No enclosing instance of type Test is accessible. Must qualify the allocation with an enclosing instance of type Test (e.g. x.new A() where x is an instance of Test)." on new Room(2, 2, "Carpet", 2); right at the beginning. I dont know how to fix this....any recommendations?

package test;

//Test.java;

import java.util.ArrayList;

public class Test {

public static void main(String[] args) { // TODO Auto-generated method stub ArrayList buildingsArray = new ArrayList(); Room squareRoom = new Room(2, 2, "Carpet", 2); Room blueRoom = new Room(2, 3, "Marble", 1); Room redRoom = new Room(3, 2, "Wood", 1); Room greenRoom = new Room(3, 3, "Marble", 2); Room diningRoom = new Room(4, 5, "Wood", 3); Room livingRoom = new Room(5, 5, "Carpet", 4); Room[] roomSet1 = { squareRoom, blueRoom, redRoom }; Room[] roomSet2 = { diningRoom, livingRoom }; Room[] roomSet3 = { blueRoom, greenRoom, redRoom }; Room[] roomSet4 = { livingRoom, redRoom, squareRoom }; House house1 = new House(roomSet1, 4, 6, 2); House house2 = new House(roomSet2, 2, 4, 1); House house3 = new House(roomSet3, 3, 2, 2); House house4 = new House(roomSet4, 4, 4, 3); Garage garage1 = new Garage(1, 5, 5, 2, "Cement"); Garage garage2 = new Garage(1, 6, 6, 3, "Gravel"); Garage garage3 = new Garage(2, 6, 7, 4, "Cement"); Garage garage4 = new Garage(2, 5, 6, 3, "Gravel"); buildingsArray.add(house1); buildingsArray.add(house2); buildingsArray.add(house3); buildingsArray.add(house4); buildingsArray.add(garage1); buildingsArray.add(garage2); buildingsArray.add(garage3); buildingsArray.add(garage4); for (Building aBuilding : buildingsArray) { if (aBuilding instanceof House) { housePassMethod((House)aBuilding); } else { System.out.println(aBuilding.toString()); } } System.out.println("Red Room compare to Blue Room: " + blueRoom.compareTo(redRoom));

if (blueRoom.equals(redRoom)) { System.out.println("Blue Room is equal to Red Room in size."); } else { System.out.println("Blue Room is not equal to Red Room in size."); } System.out.println("Green Room compared to Blue Room: " + greenRoom.compareTo(blueRoom));

if (greenRoom.equals(blueRoom)) { System.out.println("Green Room is equal to Blue Room in size."); } else { System.out.println("Green Room is not equal to Blue Room in size."); }

} public static void housePassMethod(MLSListable house) { System.out.println(house.getMLSListing()); }

//Building.java------->The BUILDING class

public abstract class Building {

private int numberOfFloors; private int numberOfWindows;

public int getNumberOfFloors() { return numberOfFloors; }

public void setNumberOfFloors(int numberOfFloors) { this.numberOfFloors = numberOfFloors; }

public int getNumberOfWindows() { return numberOfWindows; }

public void setNumberOfWindows(int numberOfWindows) { this.numberOfWindows = numberOfWindows; }

public String toString() { String result = "I am a Building!"; return result; }

}

//Room.java------------>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; 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 = " \t\tLength: " + 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; } 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; }

}

//Garage.java

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 getNumberOfCars() { return numberOfCars; }

public void setNumberOfCars(int numberOfCars) { 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.java

public class House extends Building implements MLSListable {

private Room[] rooms; private int numberOfBathrooms;

public House(Room[] rooms, int numberOfBathrooms, int numberOfWindows, int numberOfFloors) {

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; }

public int getAvgRoomSize() { int result; 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(); } }

//MLSListable.java

public interface MLSListable {

public String getMLSListing(); }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

50 Tips And Tricks For MongoDB Developers Get The Most Out Of Your Database

Authors: Kristina Chodorow

1st Edition

1449304613, 978-1449304614

More Books

Students also viewed these Databases questions