This is my code from HW7: public class House { private Room[] myHome ; public Ho
ID: 3604846 • Letter: T
Question
This is my code from HW7:
public class House {
private Room[] myHome;
public House(int rms) {
myHome = new Room[rms];
}
public void showHouse() {
System.out.println("This " + getNumBedrooms() + " bedroom house has " + myHome.length + " rooms; total area is " + getSqFeet() + " sq feet");
for (int incr = 0; incr < myHome.length; incr++) {
System.out.println(" " + myHome[incr]);
}
}
public double getSqFeet() {
double area = 0;
for (int incr = 0; incr < myHome.length; incr++) {
area += myHome[incr].getSqFeet();
}
return area;
}
public double getNumBedrooms() {
double brs = 0;
for (int incr = 0; incr < myHome.length; incr++) {
if ( (myHome[incr].getClass() == Bedroom.class)
|| (myHome[incr].getClass().getSuperclass() == Bedroom.class) ){
brs++;
}
}
return brs;
}
public static void main(String[] args) {
House h = new House(8);
int rooms = 0;
h.myHome[rooms++] = new Kitchen(12, 16);
h.myHome[rooms++] = new LivingRoom(16, 20, true);
h.myHome[rooms++] = new MasterBedroom(17, 17);
h.myHome[rooms++] = new Bedroom(13, 13);
h.myHome[rooms++] = new Bedroom(0, 0);
h.myHome[rooms++] = new Bathroom(9, 9, "three-quarter");
h.myHome[rooms++] = new Bathroom(8, 11, "full");
h.myHome[rooms++] = new Den(14, 15.5, false, true);
h.myHome[4].setSize(12.5, 14.5);
h.showHouse();
System.out.println(); // blank line
House tinyHouse = new House(3);
rooms = 0;
tinyHouse.myHome[rooms++] = new Kitchen(5, 6);
tinyHouse.myHome[rooms++] = new MasterBedroom(10, 6);
tinyHouse.myHome[rooms++] = new Bathroom(5, 6, "three-quarter");
tinyHouse.showHouse();
}
}
public abstract class Room {
protected static final double HUGESIZE = 5280; // no room is more than a mile wide or long
protected double width, length;
public Room() {
this(0, 0);
}
public Room(double wd, double ln) {
this.setSize(wd, ln);
}
public boolean setSize(double wd, double ln) {
if ( (wd >= 0) && (wd < HUGESIZE) && (ln >= 0) && (ln < HUGESIZE)) {
width = wd;
length = ln;
return true; // indicates success
}
return false; // indicates failure
}
public double getSqFeet() { return width*length; }
public double getWidth() { return width; }
public double getLength() { return length; }
public String toString() {
return "WD x LN: [" + width + " x " + length + "] ";
}
}
public class Bathroom extends Room {
private String bathType;
public Bathroom() {
super();
bathType = "full";
}
public Bathroom(double w, double l, String type) {
super(w, l);
bathType = type;
}
public String toString() {
return "Bathroom: " + super.toString() + bathType;
}
}
public class Bedroom extends Room {
public Bedroom() {
super();
}
public Bedroom(double w, double l) {
super(w, l);
}
public String toString() {
return "Bedroom: " + super.toString();
}
}
public class Kitchen extends Room {
public Kitchen() {
super();
}
public Kitchen(double w, double l) {
super(w, l);
}
public String toString() {
return "Kitchen: " + super.toString();
}
}
public abstract class SittingRoom extends Room {
private boolean hasCeilingFan;
public SittingRoom() {
super();
hasCeilingFan = false;
}
public SittingRoom(double w, double l, boolean f) {
super(w, l);
hasCeilingFan = f;
}
public String toString() {
return super.toString() + (hasCeilingFan ? "w/fan " : "");
}
}
public class Den extends SittingRoom {
boolean hasFireplace;
public Den() {
super();
hasFireplace = true; // for now, all Dens have fireplaces
}
public Den(double w, double l, boolean fan, boolean fp) {
super(w, l, fan);
hasFireplace = fp;
}
public String toString() {
return "Den: " + super.toString() + (hasFireplace ? "w/fireplace" : "");
}
}
public class LivingRoom extends SittingRoom {
public LivingRoom() {
super();
}
public LivingRoom(double w, double l, boolean fan) {
super(w, l, fan);
}
public String toString() {
return "LivingRoom: " + super.toString();
}
}
public class MasterBedroom extends Bedroom {
public MasterBedroom() {
super();
}
public MasterBedroom(double w, double l) {
super(w, l);
}
public String toString() {
return "Master " + super.toString();
}
}
In this assignment, you will add suitable equality operators as required to the class hierarchy that you developed in HW7. As discussed in class, testing the equality of objects of class type is tricky and requires a good equals() method in the class definition. With an inheritance hierarchy like this, you need to decide if every class needs its own equals method or if we can inherit a suitable method from the superclass in some cases. For this assignment, you are to add a suitable equals method for the classes in the House assignment. You should not add any more code to the classes than you must. To test your resulting classes, here is a method that will test useful s and write the results. It's an ugly routine and there are better ways to do this, but I wanted it to be as simple to read as possible.Explanation / Answer
The classes which need equals method are:
Room.java
Bathroom.java
SittingRoom.java
Den.java
No other classes have been modified.
The other classes donot require equals() method as they can get the equals() method from their parent classes.
A class Test.java is created which has CompareObjects() method to test as given in the question.
All the classes have been given below with the Test.java class and the output screens.
I hope this helps. All the best. :)
***************************************************************************************
public class Bathroom extends Room {
private String bathType;
public Bathroom() {
super();
bathType = "full";
}
public Bathroom(double w, double l, String type) {
super(w, l);
bathType = type;
}
public String toString() {
return "Bathroom: " + super.toString() + bathType;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
Bathroom other = (Bathroom) obj;
if (bathType == null) {
if (other.bathType != null)
return false;
} else if (!bathType.equals(other.bathType))
return false;
return true;
}
}
public class Bedroom extends Room {
public Bedroom() {
super();
}
public Bedroom(double w, double l) {
super(w, l);
}
public String toString() {
return "Bedroom: " + super.toString();
}
}
public class Den extends SittingRoom {
boolean hasFireplace;
public Den() {
super();
hasFireplace = true; // for now, all Dens have fireplaces
}
public Den(double w, double l, boolean fan, boolean fp) {
super(w, l, fan);
hasFireplace = fp;
}
public String toString() {
return "Den: " + super.toString() + (hasFireplace ? "w/fireplace" : "");
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
Den other = (Den) obj;
if (hasFireplace != other.hasFireplace)
return false;
return true;
}
}
public class House {
private Room[] myHome;
public House(int rms) {
myHome = new Room[rms];
}
public void showHouse() {
System.out.println("This " + getNumBedrooms() + " bedroom house has " + myHome.length + " rooms; total area is "
+ getSqFeet() + " sq feet");
for (int incr = 0; incr < myHome.length; incr++) {
System.out.println(" " + myHome[incr]);
}
}
public double getSqFeet() {
double area = 0;
for (int incr = 0; incr < myHome.length; incr++) {
area += myHome[incr].getSqFeet();
}
return area;
}
public double getNumBedrooms() {
double brs = 0;
for (int incr = 0; incr < myHome.length; incr++) {
if ((myHome[incr].getClass() == Bedroom.class)
|| (myHome[incr].getClass().getSuperclass() == Bedroom.class)) {
brs++;
}
}
return brs;
}
public static void main(String[] args) {
House h = new House(8);
int rooms = 0;
h.myHome[rooms++] = new Kitchen(12, 16);
h.myHome[rooms++] = new LivingRoom(16, 20, true);
h.myHome[rooms++] = new MasterBedroom(17, 17);
h.myHome[rooms++] = new Bedroom(13, 13);
h.myHome[rooms++] = new Bedroom(0, 0);
h.myHome[rooms++] = new Bathroom(9, 9, "three-quarter");
h.myHome[rooms++] = new Bathroom(8, 11, "full");
h.myHome[rooms++] = new Den(14, 15.5, false, true);
h.myHome[4].setSize(12.5, 14.5);
h.showHouse();
System.out.println(); // blank line
House tinyHouse = new House(3);
rooms = 0;
tinyHouse.myHome[rooms++] = new Kitchen(5, 6);
tinyHouse.myHome[rooms++] = new MasterBedroom(10, 6);
tinyHouse.myHome[rooms++] = new Bathroom(5, 6, "three-quarter");
tinyHouse.showHouse();
}
}
public class Kitchen extends Room {
public Kitchen() {
super();
}
public Kitchen(double w, double l) {
super(w, l);
}
public String toString() {
return "Kitchen: " + super.toString();
}
}
public class LivingRoom extends SittingRoom {
public LivingRoom() {
super();
}
public LivingRoom(double w, double l, boolean fan) {
super(w, l, fan);
}
public String toString() {
return "LivingRoom: " + super.toString();
}
}
public class MasterBedroom extends Bedroom {
public MasterBedroom() {
super();
}
public MasterBedroom(double w, double l) {
super(w, l);
}
public String toString() {
return "Master " + super.toString();
}
}
public abstract class Room {
protected static final double HUGESIZE = 5280; // no room is more than a mile wide or long
protected double width, length;
public Room() {
this(0, 0);
}
public Room(double wd, double ln) {
this.setSize(wd, ln);
}
public boolean setSize(double wd, double ln) {
if ((wd >= 0) && (wd < HUGESIZE) && (ln >= 0) && (ln < HUGESIZE)) {
width = wd;
length = ln;
return true; // indicates success
}
return false; // indicates failure
}
public double getSqFeet() {
return width * length;
}
public double getWidth() {
return width;
}
public double getLength() {
return length;
}
public String toString() {
return "WD x LN: [" + width + " x " + length + "] ";
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Room other = (Room) obj;
if (Double.doubleToLongBits(length) != Double.doubleToLongBits(other.length))
return false;
if (Double.doubleToLongBits(width) != Double.doubleToLongBits(other.width))
return false;
return true;
}
}
public abstract class SittingRoom extends Room {
private boolean hasCeilingFan;
public SittingRoom() {
super();
hasCeilingFan = false;
}
public SittingRoom(double w, double l, boolean f) {
super(w, l);
hasCeilingFan = f;
}
public String toString() {
return super.toString() + (hasCeilingFan ? "w/fan " : "");
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
SittingRoom other = (SittingRoom) obj;
if (hasCeilingFan != other.hasCeilingFan)
return false;
return true;
}
}
public class Test {
public static void main(String[] args) {
CompareObjects();
}
private static void CompareObjects() {
// TODO Auto-generated method stub
LivingRoom lrs[] = { new LivingRoom(12, 12, true), new LivingRoom(12, 12, true),new LivingRoom(12,12,false),new LivingRoom(15,15,true)};
System.out.println("lrs[0]==lrs[1]: "+lrs[0].equals(lrs[1]));
System.out.println("lrs[0]==lrs[2]: "+lrs[0].equals(lrs[2]));
System.out.println("lrs[0]==lrs[3]: "+lrs[0].equals(lrs[3]));
Bathroom bths[] = { new Bathroom(12, 12, "full"), new Bathroom(12, 12, "full"),new Bathroom(12,12,"half"),new Bathroom(15,15,"full")};
System.out.println("bths[0]==bths[1]: "+bths[0].equals(bths[1]));
System.out.println("bths[0]==bths[2]: "+bths[0].equals(bths[2]));
System.out.println("bths[0]==bths[3]: "+bths[0].equals(bths[3]));
Kitchen kts[] = { new Kitchen(12, 12), new Kitchen(12, 12),new Kitchen(12,14),new Kitchen(15,15)};
System.out.println("kts[0]==kts[1]: "+kts[0].equals(kts[1]));
System.out.println("kts[0]==kts[2]: "+kts[0].equals(kts[2]));
System.out.println("kts[0]==kts[3]: "+kts[0].equals(kts[3]));
MasterBedroom mbrs[] = { new MasterBedroom(12, 12), new MasterBedroom(12, 12),new MasterBedroom(12,14),new MasterBedroom(15,15)};
System.out.println("mbrs[0]==mbrs[1]: "+mbrs[0].equals(mbrs[1]));
System.out.println("mbrs[0]==mbrs[2]: "+mbrs[0].equals(mbrs[2]));
System.out.println("mbrs[0]==mbrs[3]: "+mbrs[0].equals(mbrs[3]));
Den ds[] = { new Den(12, 12, false,false), new Den(12, 12, false,false),new Den(12,12,false,true),new Den(15,15,false,false)};
System.out.println("ds[0]==ds[1]: "+ds[0].equals(ds[1]));
System.out.println("ds[0]==ds[2]: "+ds[0].equals(ds[2]));
System.out.println("ds[0]==ds[3]: "+ds[0].equals(ds[3]));
System.out.println("kts[0]==lrs[1]: "+kts[0].equals(lrs[0]));
System.out.println("bths[1]==kts[1]: "+bths[1].equals(kts[1]));
System.out.println("lrs[0]==ds[1]: "+lrs[0].equals(ds[0]));
}
}
****************************************************************************************
Output screens:
******************************************************************************************************************************
I hope this helps you.
If you find my answer helpful,
Kindly rate the answer.
All the best :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.