Question: Java Specifications: Part 1 Refers to the Building / House / Garage assignment (This assignment is at the bottom) - Add an interface to your

Java Specifications:

Part 1 Refers to the Building / House / Garage assignment (This assignment is at the bottom)

- Add an interface to your previous assignment. 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, but Garage will not.

- 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 (which do not implement MLSListable) wont compile if you try to pass them.

==========================================================

Part 2

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

- 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 contract that comes along with the Comparable interface.

Part 3

Side note to convince you that this is useful: The compareTo interface is probably the most commonly used interface in all of Java. Suppose you have a bunch of objects from a class you created. An array of Rooms for example. Whenever you have a collection of things you will eventually want to sort them. When sorting, there must be a definition of which comes before the other thats what sorting means. You define this natural ordering of objects, by implementing the Comparable interface, and the equals method.

You never have to write a sorting, or searching algorithm again. If you want to sort your array of Rooms, you can now use the

Arrays.sort(myRooms);

Exercise:

Create an array of Rooms with at least 4 room objects.

Output it to the console.

Sort the array.

Output the sorted array.

(If your array was already sorted by coincidence, go back and make an array that will change when it is sorted.)

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();

}

House Class

import java.util.List;

class house extends building {

private int noOfBathroom;

@SuppressWarnings("unchecked")

private List rooms = (List) new ArrayList();

// 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 (overriding)

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 overriding

public String toString() {

return "house has " + NoOfFloors + " floor ," + NoOfWindows + " windows and " + noOfBathroom + " bathroom.";

}

}

Garage Class

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;

}

}

Room Class

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;

}

}

Test Class

public class Test {

public static void main(String[] args) {

// create object house class

building b1 = new house(3, 5, 10);

// create object of garage class

building b2 = new garage(5, 7, 11, "cement floor", 15, 12);

System.out.println(b1);

System.out.println(b2);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!