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

Java Specifications: Part 1 Refers to the Building / House / Garage assignment (

ID: 3860996 • Letter: J

Question

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

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<room> rooms = (List<room>) 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);

}

}

Explanation / Answer

Given below are the modified and new files . Please do rate the answer if it helped. Thank you.

house.java

import java.util.ArrayList;
import java.util.List;

class house extends building implements MLSListable {
   private int noOfBathroom;
   @SuppressWarnings("unchecked")
   private List<room> rooms = (List<room>) 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.";
   }

   @Override
   public String getMLSListing() {

       return toString();
   }
}

room.java

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 "Rooms has length: " + length + " width: " + width + " floorcovring: " + floorCovering
               + " number of closet: " + NoOfClosets;
   }

   /*
   * compare 2 rooms . 2 rooms are equal if their length and width are same and same number of closets. Compare first by length
   * and then by width. So when rooms are sortd, they are first sorted by length and when length matches
   * widths are compared, and sorted by widths then . If both length and width match, the rooms are compared
   * by number of closets
   */
   @Override
   public int compareTo(room r) {

       if(length > r.length)
           return 1;
       else if(length < r.length)
           return -1;
       else
       {
           if(width > r.width)
               return 1;
           else if(width < r.width)
               return -1;
           else
           {
               if(NoOfClosets > r.NoOfClosets)
                   return 1;
               else if(NoOfClosets < r.NoOfClosets)
                   return -1;
               else
               {
                   return 0;
               }
           }
       }
   }

   @Override
   public boolean equals(Object obj) {
       if(obj instanceof room && compareTo((room)obj) == 0)
           return true;
       else
           return false;
   }


}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 "Rooms has length: " + length + " width: " + width + " floorcovring: " + floorCovering
               + " number of closet: " + NoOfClosets;
   }

   /*
   * compare 2 rooms . 2 rooms are equal if their length and width are same and same number of closets. Compare first by length
   * and then by width. So when rooms are sortd, they are first sorted by length and when length matches
   * widths are compared, and sorted by widths then . If both length and width match, the rooms are compared
   * by number of closets
   */
   @Override
   public int compareTo(room r) {

       if(length > r.length)
           return 1;
       else if(length < r.length)
           return -1;
       else
       {
           if(width > r.width)
               return 1;
           else if(width < r.width)
               return -1;
           else
           {
               if(NoOfClosets > r.NoOfClosets)
                   return 1;
               else if(NoOfClosets < r.NoOfClosets)
                   return -1;
               else
               {
                   return 0;
               }
           }
       }
   }

   @Override
   public boolean equals(Object obj) {
       if(obj instanceof room && compareTo((room)obj) == 0)
           return true;
       else
           return false;
   }


}

MLSListable.java


public interface MLSListable {
   public String getMLSListing();
}

Test.java

import java.util.Arrays;
import java.util.Random;

public class Test {
   private static void display(room rooms[])
   {
       for(int i = 0; i < rooms.length; i++)
           System.out.println((i+1) + " " + "length: " + rooms[i].getlength()
                   + " width: " + rooms[i].getwidth() +" closets: " + rooms[i].getNoOfClosets());
   }
   public static void display(MLSListable m)
   {
       System.out.println(m.getMLSListing());
   }
   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);

       house h = new house(2, 4, 9);
       display(h);
       garage g = new garage(5, 7, 11, "cement floor", 15, 12);
       room r = new room();
       /*the following 2 lines will not compile since garage and room does not implement MLSListable
and does not match parameter type of display()*/

       //display(g);
       //display(r);
      
      
       //demonstrate sorting of rooms
       room rooms[] = new room[9];//create an array of 10 rooms
       Random random = new Random(System.currentTimeMillis());//use random generator to generate random room details
       for(int i = 0; i < rooms.length; i++)
       {
           rooms[i] = new room();
           rooms[i].setlength(5 + random.nextInt(10));
           rooms[i].setwidth(5 + random.nextInt(10));
           rooms[i].setNoOfClosets(1 + random.nextInt(3));
           rooms[i].setfloorCovring("Granite");
       }
      
       System.out.println("generated rooms. before sorting, the rooms are ...");
       display(rooms);
      
       System.out.println("sorting rooms using Arrays.sort()");
       Arrays.sort(rooms);
      
       System.out.println("after sorting, the rooms are ...");
       display(rooms);
   }
  
}

output

house has 3 floor ,5 windows and 10 bathroom.
garage has5 floor ,7 windows and capacity of parking: 11 car. floor type: cement floor length: 15.0 width: 12.0
house has 2 floor ,4 windows and 9 bathroom.
generated rooms. before sorting, the rooms are ...
1 length: 10.0 width: 10.0 closets: 1
2 length: 14.0 width: 13.0 closets: 2
3 length: 11.0 width: 6.0 closets: 2
4 length: 5.0 width: 5.0 closets: 2
5 length: 10.0 width: 6.0 closets: 2
6 length: 8.0 width: 10.0 closets: 2
7 length: 13.0 width: 14.0 closets: 3
8 length: 6.0 width: 11.0 closets: 3
9 length: 7.0 width: 8.0 closets: 3
sorting rooms using Arrays.sort()
after sorting, the rooms are ...
1 length: 5.0 width: 5.0 closets: 2
2 length: 6.0 width: 11.0 closets: 3
3 length: 7.0 width: 8.0 closets: 3
4 length: 8.0 width: 10.0 closets: 2
5 length: 10.0 width: 6.0 closets: 2
6 length: 10.0 width: 10.0 closets: 1
7 length: 11.0 width: 6.0 closets: 2
8 length: 13.0 width: 14.0 closets: 3
9 length: 14.0 width: 13.0 closets: 2

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote