Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA!!!! Based on these codes: //Building.Java import java.util.*; //abstract cl

ID: 3860188 • Letter: J

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 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 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) won’t 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 – that’s 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.)

Explanation / Answer

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg.july;

/**
*
* @author Sam
*/
//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 implements MLSListable{

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

    @Override
    public String getMLSListing() {
        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 implements Comparable<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;
    }

    @Override
    public int compareTo(room o) {
        return Double.compare(length, o.length)*5 +
                Double.compare(width, o.width)*7 +
                Integer.compare(NoOfClosets, o.NoOfClosets)*11 +
                floorCovering.compareTo(o.floorCovering) * 13;
              
    }

  
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final room other = (room) obj;
        if (compareTo(other) != 0)
            return false;
        return true;
    }
  
}


interface MLSListable {
    String getMLSListing();
}

//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Test.java
class Test {

    public static void main(String[] args) {

//create object house class
        building b1 = new house(5, 16, 10);
        house h1 = new house(5, 50, 1);
//create object of garage class
        building b2 = new garage(2, 4, 10, "cement floor", 20, 20);
        garage g1 = new garage(2, 4, 10, "cement floor", 20, 20);
        System.out.println(b1);
        System.out.println(b2);
        //testMLSListable(b1); //ERROR
        //testMLSListable(b2); //ERROR
        testMLSListable(h1);
        //testMLSListable(g1); //ERROR
      
        int i;
        room[] rooms = new room[4];
        i = 0;
        rooms[i] = new room();
        rooms[i].setNoOfClosets(5);
        rooms[i].setfloorCovring("Cement");
        rooms[i].setlength(1);
        rooms[i].setwidth(2);
        i = 1;
        rooms[i] = new room();
        rooms[i].setNoOfClosets(8);
        rooms[i].setfloorCovring("Glass");
        rooms[i].setlength(10);
        rooms[i].setwidth(15);
        i = 2;
        rooms[i] = new room();
        rooms[i].setNoOfClosets(0);
        rooms[i].setfloorCovring("Tile");
        rooms[i].setlength(1);
        rooms[i].setwidth(20);
        i = 3;
        rooms[i] = new room();
        rooms[i].setNoOfClosets(5);
        rooms[i].setfloorCovring("**NO FLOOR**");
        rooms[i].setlength(1);
        rooms[i].setwidth(2);
        System.out.println("*********Before Sorting*********");
        for (i = 0; i< 4 ; i++)
            System.out.println(rooms[i]);
        Arrays.sort(rooms);
      
        System.out.println("*********After Sorting*********");
        for (i = 0; i< 4 ; i++)
            System.out.println(rooms[i]);
    }
  
    private static void testMLSListable(MLSListable object) {
        System.out.println(object.getMLSListing());
    }
}

Here you go!! All answers are included in here!