Question: JAVA!!!! Based on these codes: //Building.Java import java.util.*; //abstract class abstract class building { //protected keyword to access elements in derived class only protected int

JAVA!!!! Based on these codes:

//Building.Java 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 rooms = new ArrayList(); //constructor public house(int floors,int windows,int bathrooms) { //super call base class constructor super(floors,windows); noOfBathroom = bathrooms; } //method to find the 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 the noOfBathroom public void setnoOfBathroom(int bathrooms) { noOfBathroom = bathrooms; } //getter for the noOfBathroom public int getnoOfBathroom() { return noOfBathroom; } //add new room in the roomList public boolean addRoom( room r ) { rooms.add( r ); return true; } //to String method oveerrinding public String toString() { return "A house has "+NoOfFloors +" floors ,"+NoOfWindows +" Windows, and " +noOfBathroom +" Bathrooms."; } }

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 number of cars public void setNoOfCar(int c) { noOfCars = c; } //getters to the 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 "The Garage has " +NoOfFloors +" Floors ,"+NoOfWindows +" Windows, and a capacity of parking for "+noOfCars +" cars. The floor type is " +floorType + " with a length of "+length +" and a width of " +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 "The Rooms have a Length: "+length+" Width: "+width+" Floorcovering: "+floorCovering+" Number of closet: "+NoOfClosets; } }

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

//Test.java public class Test{

public static void main(String []args){ //create object house class building b1 = new house(5,16,10); //create object of garage class building b2 = new garage(2,4,10,"cement floor",20, 20); System.out.println(b1); System.out.println(b2); } }

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Part 1 Refers to the Above Code

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

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!