APARTMENT BUILDING HOMEWORK
1 What’s the problem?
Suppose you want to model an apartment building so that you can calculate the number of
windows (and their sizes) that are in the building. We are modelling the building with the
following classes:
1. Building, which contains several types of apartments.
2. Apartment and its subclasses OneBedroom, TwoBedroom, and ThreeBedroom. An instance of
this class is to represent a type of apartments, which includes a field to store the number of units
of this type.
3. Room and its subclass LivingRoom, MasterBedroom, and GuestRoom. Each room has some
windows of certain size. Different type of rooms have windows of different sizes.
4. Window and WindowOrder Window object has just the width and height of the window.
Window order object has a window object and the number of window for that order.
2 What to implement?
You will fill in the methods for classes described above. The provided code template has
instruction in the comments above the methods that you will implement.
3 What is provided? We provide a driver class Hwk6.java, a test class Test.java, and a bunch of
template classes. Note that each file may contain multiple classes. I usually group related classes
in the same file. For example, the file Apartment.java contains not only Apartment class but also
its subclasses.
4 What does output look like? If you run the driver class Hwk6.java you will see output that
looks like the followings.
20 apartments with (Living room: 5 (6 X 8 window)) (Master bedroom: 3 (4 X 6 window))
15 apartments with (Living room: 5 (6 X 8 window)) (Master bedroom: 3 (4 X 6 window))
(Guest room: 2 (5 X 6 window))
10 apartments with (Living room: 5 (6 X 8 window)) (Master bedroom: 3 (4 X 6 window))
(Guest room: 2 (5 X 6 window)) (Guest room: 2 (5 X 6 window))
Window orders are:
300 6 X 8 window
180 4 X 6 window
120 5 X 6 window
PROVIDED MAIN CLASS
package hwk6;
public class Hwk6 {
public static void main(String[] args) {
Apartment[] apartments = { new OneBedroom(20), new TwoBedroom(15), new
ThreeBedroom(10) };
Building building = new Building(apartments);
WindowOrder[] orders = building.order();
System.out.println(building);
System.out.println(\"Window orders are: \");
for(WindowOrder order: orders) {
System.out.println(order);
}
}
}
Building Template:
package hwk6;
public class Building {
Apartment[] apartments;
public Building(Apartment[] apartments) {
this.apartments= apartments;
}
// Return an array of window orders for all apartments in the building
// Ensure that the orders for windows of the same sizes are merged.
WindowOrder[] order() {
// TODO
}
// return a string to represent all types of apartments in the building such as:
// 20 apartments with (Living room: 5 (6 X 8 window))(Master bedroom: 3 (4 X 6 window))
// 15 apartments with (Living room: 5 (6 X 8 window))(Master bedroom: 3 (4 X 6
window))(Guest room: 2 (5 X 6 window))
// 10 apartments with (Living room: 5 (6 X 8 window))(Master bedroom: 3 (4 X 6
window))(Guest.
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
APARTMENT BUILDING HOMEWORK1 What’s the problemSuppose you want.pdf
1. APARTMENT BUILDING HOMEWORK
1 What’s the problem?
Suppose you want to model an apartment building so that you can calculate the number of
windows (and their sizes) that are in the building. We are modelling the building with the
following classes:
1. Building, which contains several types of apartments.
2. Apartment and its subclasses OneBedroom, TwoBedroom, and ThreeBedroom. An instance of
this class is to represent a type of apartments, which includes a field to store the number of units
of this type.
3. Room and its subclass LivingRoom, MasterBedroom, and GuestRoom. Each room has some
windows of certain size. Different type of rooms have windows of different sizes.
4. Window and WindowOrder Window object has just the width and height of the window.
Window order object has a window object and the number of window for that order.
2 What to implement?
You will fill in the methods for classes described above. The provided code template has
instruction in the comments above the methods that you will implement.
3 What is provided? We provide a driver class Hwk6.java, a test class Test.java, and a bunch of
template classes. Note that each file may contain multiple classes. I usually group related classes
in the same file. For example, the file Apartment.java contains not only Apartment class but also
its subclasses.
4 What does output look like? If you run the driver class Hwk6.java you will see output that
looks like the followings.
20 apartments with (Living room: 5 (6 X 8 window)) (Master bedroom: 3 (4 X 6 window))
15 apartments with (Living room: 5 (6 X 8 window)) (Master bedroom: 3 (4 X 6 window))
(Guest room: 2 (5 X 6 window))
10 apartments with (Living room: 5 (6 X 8 window)) (Master bedroom: 3 (4 X 6 window))
(Guest room: 2 (5 X 6 window)) (Guest room: 2 (5 X 6 window))
Window orders are:
300 6 X 8 window
180 4 X 6 window
120 5 X 6 window
PROVIDED MAIN CLASS
package hwk6;
public class Hwk6 {
public static void main(String[] args) {
2. Apartment[] apartments = { new OneBedroom(20), new TwoBedroom(15), new
ThreeBedroom(10) };
Building building = new Building(apartments);
WindowOrder[] orders = building.order();
System.out.println(building);
System.out.println("Window orders are: ");
for(WindowOrder order: orders) {
System.out.println(order);
}
}
}
Building Template:
package hwk6;
public class Building {
Apartment[] apartments;
public Building(Apartment[] apartments) {
this.apartments= apartments;
}
// Return an array of window orders for all apartments in the building
// Ensure that the orders for windows of the same sizes are merged.
WindowOrder[] order() {
// TODO
}
// return a string to represent all types of apartments in the building such as:
// 20 apartments with (Living room: 5 (6 X 8 window))(Master bedroom: 3 (4 X 6 window))
// 15 apartments with (Living room: 5 (6 X 8 window))(Master bedroom: 3 (4 X 6
window))(Guest room: 2 (5 X 6 window))
// 10 apartments with (Living room: 5 (6 X 8 window))(Master bedroom: 3 (4 X 6
window))(Guest room: 2 (5 X 6 window))(Guest room: 2 (5 X 6 window))
3. //
public String toString() {
// TODO
}
}
Apartment Template:
package hwk6;
// This class contains the configuration of a type of apartment
public class Apartment {
int numOfUnits; // the number of apartments of this type
Room[] rooms; // rooms in this type of apartment
Apartment(int numOfUnits, Room[] rooms) {
this.numOfUnits = numOfUnits;
this.rooms = rooms;
}
// return an array of window orders for one unit of this type of apartment
WindowOrder[] orderForOneUnit() {
// TODO
}
// return an array of window orders for all units of this type of apartment
WindowOrder[] totalOrder() {
// TODO
}
// return text like:
//
// 15 apartments with (Living room: 5 (6 X 8 window)) (Master bedroom: 3 (4 X 6 window))
(Guest room: 2 (5 X 6 window))
public String toString() {
// TODO
}
}
class OneBedroom extends Apartment {
4. OneBedroom(int numOfUnits) {
super(numOfUnits, new Room[] { new LivingRoom(), new MasterBedroom() });
}
}
class TwoBedroom extends Apartment {
TwoBedroom(int numOfUnits) {
super(numOfUnits, new Room[] { new LivingRoom(), new MasterBedroom(), new
GuestRoom() });
}
}
class ThreeBedroom extends Apartment {
ThreeBedroom(int numOfUnits) {
super(numOfUnits, new Room[] { new LivingRoom(), new MasterBedroom(), new
GuestRoom(), new GuestRoom() });
}
// return an array of window orders for all units of this type of apartment
//
// Notice we have two guest rooms and they have the same size of windows.
// override the inherited method to merge the order for the two guest rooms since their
windows have the same size
@Override
WindowOrder[] orderForOneUnit() {
// TODO
}
}
Room Template:
package hwk6;
public class Room {
Window window;
int numOfWindows;
Room(Window window, int numOfWindows) {
this.window = window;
this.numOfWindows = numOfWindows;
}
5. WindowOrder order() {
return new WindowOrder(window, numOfWindows);
}
// Print text like: 5 (6 X 8 window)
@Override
public String toString() {
// TODO
}
// Two rooms are equal if they contain the same number of windows of the same size
@Override
public boolean equals(Object that) {
// TODO
}
}
class MasterBedroom extends Room {
MasterBedroom() {
super(new Window(4, 6), 3);
}
// Call parent's toString method
//
// return text like: Master bedroom: 3 (4 X 6 window)
@Override
public String toString() {
// TODO
}
}
class GuestRoom extends Room {
GuestRoom() {
super(new Window(5, 6), 2);
}
// Call parent's toString method
//
// return text like: Guest room: 2 (5 X 6 window)
@Override
public String toString() {
6. // TODO
}
}
class LivingRoom extends Room {
LivingRoom() {
super(new Window(6, 8), 5);
}
// Call parent's toString method
//
// return text like: Living room: 5 (6 X 8 window)
@Override
public String toString() {
// TODO
}
}
Window Template:
package hwk6;
public class Window {
private final int width, height;
public Window(int width, int height) {
this.width = width;
this.height = height;
}
// print text like: 4 X 6 window
public String toString() {
// TODO
}
// compare window objects by their dimensions
public boolean equals(Object that) {
// TODO
}
}
class WindowOrder {
7. final Window window; // window description (its width and height)
int num; // number of windows for this order
WindowOrder(Window window, int num) {
this.window = window;
this.num = num;
}
// add the num field of the parameter to the num field of this object
//
// BUT
//
// do the merging only of two windows have the same size
// do nothing if the size does not match
//
// return the current object
WindowOrder add (WindowOrder order) {
// TODO
}
// update the num field of this object by multiplying it with the parameter
// and then return the current object
WindowOrder times(int number) {
// TODO
}
// print text like: 20 4 X 6 window
@Override
public String toString() {
// TODO
}
// Two orders are equal if they contain the same number of windows of the same size.
@Override
public boolean equals(Object that) {
// TODO
}
}
Provided Testing class:
8. package hwk6;
import org.junit.*;
import static org.junit.Assert.*;
import java.lang.reflect.Field;
import org.junit.FixMethodOrder;
import org.junit.runners.MethodSorters;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class Testing {
Window window;
WindowOrder windoworder;
Room room;
Room masterbedroom;
Room guestroom;
Room livingroom;
Apartment apartment;
Apartment onebedroom;
Apartment twobedrom;
Apartment threebedroom;
Building building;
Room []rooms ;
Apartment [] apartments;
@Before
public void setUp()
{
this.window= new Window(10, 20);
windoworder = new WindowOrder(window, 100);
this.room= new Room(window, 5);
this.masterbedroom= new MasterBedroom();
this.guestroom= new GuestRoom();
this.livingroom= new LivingRoom();
10. }
private Object getField( Object instance, String name ) throws Exception
{
Class c = instance.getClass();
Field f = c.getDeclaredField( name );
f.setAccessible( true );
return f.get( instance );
}
@Test
public void AA_TestWindowConstructor() throws Exception{
assertEquals(this.window.getClass().getSimpleName().toString(), "Window");
assertEquals(10,(int)getField(window,"width"));
assertEquals(20,getField(window,"height"));
}
@Test
public void AB_TestWindowEquals() throws Exception{
assertTrue(this.window.equals(new Window(10,20)));
assertTrue(this.window.equals(this.window));
assertFalse(this.window.equals(new Window(1,20)));
}
@Test
public void BA_TestWindowOrderConstructor(){
assertEquals(this.windoworder.getClass().getSimpleName().toString(), "WindowOrder");
assertEquals(this.window,windoworder.window);
assertEquals(100,windoworder.num);
}
@Test
public void BB_Testwindoworderadd(){
WindowOrder w=windoworder.add(new WindowOrder(new Window(10,20), 1000));
assertEquals(windoworder,w);
assertEquals(1100,windoworder.num);
windoworder=null;
}
@Test
11. public void BC_Testwindoworderadd(){
WindowOrder w= windoworder.add(new WindowOrder(new Window(20,20), 1000));
assertEquals(windoworder,w);
assertEquals(100,windoworder.num);
}
@Test
public void BD_Testwindoworderadd(){
WindowOrder w= windoworder.add(windoworder);
assertEquals(windoworder,w);
assertEquals(200,windoworder.num);
}
@Test
public void BE_Testwindowordertimes(){
WindowOrder w= windoworder.times(0);
assertEquals(windoworder,w);
assertEquals(0,windoworder.num);
}
@Test
public void BF_Testwindowordertimes(){
WindowOrder w= windoworder.times(3);
assertEquals(windoworder,w);
assertEquals(300,windoworder.num);
}
@Test
public void CA_TestRoomConstructor(){
assertEquals(this.room.getClass().getSimpleName().toString(), "Room");
assertEquals(5,room.numOfWindows);
assertEquals(window,room.window);
}
@Test
public void CB_TestRoomOrder(){
assertEquals( new WindowOrder(window, 5),room.order());
assertEquals( new WindowOrder(new Window(4, 6), 3),masterbedroom.order());
}
@Test
12. public void D_TestMasterBedRoomConstructor(){
assertEquals(this.masterbedroom.getClass().getSimpleName().toString(),
"MasterBedroom");
assertEquals(3,masterbedroom.numOfWindows);
assertEquals(new Window(4, 6),masterbedroom.window);
}
@Test
public void E_TestGuestRoomConstructor(){
assertEquals(this.guestroom.getClass().getSimpleName().toString(), "GuestRoom");
assertEquals(2,guestroom.numOfWindows);
assertEquals(new Window(5, 6),guestroom.window);
}
@Test
public void F_TestLivingRoomConstructor(){
assertEquals(this.livingroom.getClass().getSimpleName().toString(), "LivingRoom");
assertEquals(5,livingroom.numOfWindows);
assertEquals(new Window(6, 8),livingroom.window);
}
@Test
public void GA_TestApartmentConstructor(){
assertEquals(this.apartment.getClass().getSimpleName().toString(), "Apartment");
assertEquals(30,apartment.numOfUnits);
assertArrayEquals(rooms,apartment.rooms);
}
@Test
public void GB_TestApartmentTotalOrder(){
WindowOrder[] wo = new WindowOrder [5];
wo[0] = new WindowOrder(new Window(4, 6), 90);
wo[1] = new WindowOrder(new Window(5, 6), 60);
wo[2] = new WindowOrder(new Window(6, 8), 150);
wo[3] = new WindowOrder(new Window(4, 6), 90);
wo[4] = new WindowOrder(new Window(6, 8), 150);
assertArrayEquals(wo ,apartment.totalOrder());
}
13. @Test
public void HA_TestOneBedroomConstructor(){
assertEquals(this.onebedroom.getClass().getSimpleName().toString(), "OneBedroom");
assertEquals(10,onebedroom.numOfUnits);
assertArrayEquals(new Room[] { new LivingRoom(), new MasterBedroom()
},onebedroom.rooms);
}
@Test
public void HB_TestOneBedroomorDerForOneUnit(){
WindowOrder[] wo = new WindowOrder [2];
wo[0] = new WindowOrder(new Window(6, 8), 5);
wo[1] = new WindowOrder(new Window(4, 6), 3);
assertArrayEquals(wo ,onebedroom.orderForOneUnit());
}
@Test
public void HC_TestOneBedroomTotalOrder(){
WindowOrder[] wo = new WindowOrder [2];
wo[0] = new WindowOrder(new Window(6, 8), 50);
wo[1] = new WindowOrder(new Window(4, 6), 30);
assertArrayEquals(wo ,onebedroom.totalOrder());
}
@Test
public void IA_TestTwoBedroomConstructor(){
assertEquals(this.twobedrom.getClass().getSimpleName().toString(), "TwoBedroom");
assertEquals(5,twobedrom.numOfUnits);
assertArrayEquals(new Room[] { new LivingRoom(), new MasterBedroom(), new
GuestRoom() },twobedrom.rooms);
}
@Test
public void IB_TestTwoBedroomOrderForOneUnit(){
WindowOrder[] wo = new WindowOrder [3];
wo[0] = new WindowOrder(new Window(6, 8), 5);
wo[1] = new WindowOrder(new Window(4, 6), 3);
14. wo[2] = new WindowOrder(new Window(5, 6), 2);
assertArrayEquals(wo ,twobedrom.orderForOneUnit());
}
@Test
public void IC_TestTwoBedroomTotalOrder(){
WindowOrder[] wo = new WindowOrder [3];
wo[0] = new WindowOrder(new Window(6, 8), 25);
wo[1] = new WindowOrder(new Window(4, 6), 15);
wo[2] = new WindowOrder(new Window(5, 6), 10);
assertArrayEquals(wo ,twobedrom.totalOrder());
}
@Test
public void JA_TestThreeBedroomConstructor(){
assertEquals(this.threebedroom.getClass().getSimpleName().toString(), "ThreeBedroom");
assertEquals(15,threebedroom.numOfUnits);
assertArrayEquals(new Room[] { new LivingRoom(), new MasterBedroom(), new
GuestRoom(), new GuestRoom() },threebedroom.rooms);
}
@Test
public void JB_TestThreeBedroomOrderForOneUnit(){
WindowOrder[] wo = new WindowOrder [3];
wo[0] = new WindowOrder(new Window(6, 8), 5);
wo[1] = new WindowOrder(new Window(4, 6), 3);
wo[2] = new WindowOrder(new Window(5, 6), 4);
assertArrayEquals(wo ,threebedroom.orderForOneUnit());
}
@Test
public void JC_TestThreeBedroomTotalOrder(){
WindowOrder[] wo = new WindowOrder [3];
wo[0] = new WindowOrder(new Window(6, 8), 75);
wo[1] = new WindowOrder(new Window(4, 6), 45);
wo[2] = new WindowOrder(new Window(5, 6), 60);
15. assertArrayEquals(wo ,threebedroom.totalOrder());
}
@Test
public void KA_TestBuildingConstructor(){
assertEquals(this.building.getClass().getSimpleName().toString(), "Building");
assertArrayEquals(apartments,building.apartments);
}
@Test
public void KB_TestBuildingOrderr(){
WindowOrder[] wo = new WindowOrder [3];
wo[0] = new WindowOrder(new Window(6, 8), 275);
wo[1] = new WindowOrder(new Window(4, 6), 165);
wo[2] = new WindowOrder(new Window(5, 6), 80);
assertArrayEquals(wo,building.order());
}
@Test
public void L_TestWindowToString(){
String expected= "10 X 20 window";
assertEquals(expected,window.toString());
}
@Test
public void M_TestWindowOrderToString(){
String expected= "100 10 X 20 window";
assertEquals(expected,windoworder.toString());
}
@Test
public void N_TestApartmentToString(){
String expected= "30 apartments with (Master bedroom: 3 (4 X 6 window))(Guest room: 2 (5
X 6 window))(Living room: 5 (6 X 8 window))(Master bedroom: 3 (4 X 6 window))(Living
room: 5 (6 X 8 window))";
16. assertEquals(expected,apartment.toString());
}
@Test
public void O_TestoneBedRoomToString(){
String expected= "10 apartments with (Living room: 5 (6 X 8 window))(Master bedroom: 3
(4 X 6 window))";
assertEquals(expected,onebedroom.toString());
}
@Test
public void P_TestTwoBedRoomToString(){
String expected= "5 apartments with (Living room: 5 (6 X 8 window))(Master bedroom: 3 (4
X 6 window))(Guest room: 2 (5 X 6 window))";
assertEquals(expected,twobedrom.toString());
}
@Test
public void Q_TestThreeBedRoomToString(){
String expected= "15 apartments with (Living room: 5 (6 X 8 window))(Master bedroom: 3
(4 X 6 window))(Guest room: 2 (5 X 6 window))(Guest room: 2 (5 X 6 window))";
assertEquals(expected,threebedroom.toString());
}
@Test
public void R_TestMasterRoomToString(){
String expected= "Master bedroom: 3 (4 X 6 window)";
assertEquals(expected,masterbedroom.toString());
}
@Test
public void S_TestGuestRoomToString(){
17. String expected= "Guest room: 2 (5 X 6 window)";
assertEquals(expected,guestroom.toString());
}
@Test
public void T_TestLivingRoomToString(){
String expected= "Living room: 5 (6 X 8 window)";
assertEquals(expected,livingroom.toString());
}
@Test
public void UTestBuildingToString(){
String expected= "10 apartments with (Living room: 5 (6 X 8 window))(Master bedroom: 3
(4 X 6 window)) "+
"5 apartments with (Living room: 5 (6 X 8 window))(Master bedroom: 3 (4 X 6
window))(Guest room: 2 (5 X 6 window)) "+
"15 apartments with (Living room: 5 (6 X 8 window))(Master bedroom: 3 (4 X 6
window))(Guest room: 2 (5 X 6 window))(Guest room: 2 (5 X 6 window)) "+
"10 apartments with (Living room: 5 (6 X 8 window))(Master bedroom: 3 (4 X 6 window)) "+
"10 apartments with (Living room: 5 (6 X 8 window))(Master bedroom: 3 (4 X 6 window)) "+
"5 apartments with (Living room: 5 (6 X 8 window))(Master bedroom: 3 (4 X 6
window))(Guest room: 2 (5 X 6 window)) ";
assertEquals(expected,building.toString());
}
}
Solution
Hwk6.java
public class Hwk6 {
public static void main(String[] args) {
Apartment[] apartments = {new OneBedroom(20), new TwoBedroom(15), new
18. ThreeBedroom(10)};
Building building = new Building(apartments);
WindowOrder[] orders = building.order();
System.out.println(building);
System.out.println("Window orders are: ");
for (WindowOrder order : orders) {
System.out.println(order);
}
}
}
Apartment.java
// This class contains the configuration of a type of apartment
public class Apartment {
int numOfUnits; // the number of apartments of this type
Room[] rooms; // rooms in this type of apartment
Apartment(int numOfUnits, Room[] rooms) {
this.numOfUnits = numOfUnits;
this.rooms = rooms;
}
// return an array of window orders for one unit of this type of apartment
WindowOrder[] orderForOneUnit() {
WindowOrder[] windowOrders = new WindowOrder[rooms.length];
for (int i = 0; i < rooms.length; i++) {
windowOrders[i] = rooms[i].order();
}
return windowOrders;
}
// return an array of window orders for all units of this type of apartment
WindowOrder[] totalOrder() {
WindowOrder[] windowOrders = orderForOneUnit();
for (WindowOrder order : windowOrders) {
order.times(numOfUnits);
}
return windowOrders;
19. }
// return text like:
public String toString() {
String[] roomsArr = new String[rooms.length];
for (int i = 0; i < roomsArr.length; i++) {
roomsArr[i] = rooms[i].toString();
}
return numOfUnits + " apartments with (" + String.join(")(", roomsArr) + ")";
}
}
class OneBedroom extends Apartment {
OneBedroom(int numOfUnits) {
super(numOfUnits, new Room[]{new LivingRoom(), new MasterBedroom()});
}
}
class TwoBedroom extends Apartment {
TwoBedroom(int numOfUnits) {
super(numOfUnits, new Room[]{new LivingRoom(), new MasterBedroom(), new
GuestRoom()});
}
}
class ThreeBedroom extends Apartment {
ThreeBedroom(int numOfUnits) {
super(numOfUnits, new Room[]{new LivingRoom(), new MasterBedroom(), new
GuestRoom(), new GuestRoom()});
}
// return an array of window orders for all units of this type of apartment
WindowOrder[] orderForOneUnit() {
Room[] roomsArr = rooms;
return new WindowOrder[]{roomsArr[0].order(), roomsArr[1].order(),
roomsArr[2].order().add(roomsArr[3].order())
};
}
}
Room.java
20. public class Room {
Window window;
int numOfWindows;
Room(Window window, int numOfWindows) {
this.window = window;
this.numOfWindows = numOfWindows;
}
WindowOrder order() {
return new WindowOrder(window, numOfWindows);
}
// Print text like: 5 (6 X 8 window)
public String toString() {
return String.format("%d (%s)", numOfWindows, window);
}
// Two rooms are equal if they contain the same number of windows of the same size
public boolean equals(Object that) {
if (that instanceof Room) {
Room ob = (Room) that;
return this.window.equals(ob.window) &&
this.numOfWindows == ob.numOfWindows;
}
return false;
}
}
class MasterBedroom extends Room {
MasterBedroom() {
super(new Window(4, 6), 3);
}
// Call parent's toString method
//
// return text like: Master bedroom: 3 (4 X 6 window)
@Override
public String toString() {
return String.format("Master bedroom: %s", super.toString());
}
21. }
class GuestRoom extends Room {
GuestRoom() {
super(new Window(5, 6), 2);
}
// Call parent's toString method
public String toString() {
return String.format("Guest room: %s", super.toString());
}
}
class LivingRoom extends Room {
LivingRoom() {
super(new Window(6, 8), 5);
}
// Call parent's toString method
public String toString() {
return String.format("Living room: %s", super.toString());
}
}
Window.java
public class Window {
private final int width, height;
public Window(int width, int height) {
this.width = width;
this.height = height;
}
// print text like: 4 X 6 window
public String toString() {
return String.format("%d X %d window", this.width, this.height);
}
// compare window objects by their dimensions
public boolean equals(Object that) {
if (that instanceof Window) {
Window ob = (Window) that;
return this.width == ob.width &&
22. this.height == ob.height;
}
return false;
}
}
class WindowOrder {
final Window window; // window description (its width and height)
int num; // number of windows for this order
WindowOrder(Window window, int num) {
this.window = window;
this.num = num;
}
// return the current object
WindowOrder add(WindowOrder order) {
if (window.equals(order.window)) {
num += order.num;
}
return this;
}
// update the num field of this object by multiplying it with the parameter
// and then return the current object
WindowOrder times(int number) {
this.num *= number;
return this;
}
// print text like: 20 4 X 6 window
public String toString() {
return String.format("%d %s", num, window);
}
// Two orders are equal if they contain the same number of windows of the same size.
public boolean equals(Object that) {
if (that instanceof WindowOrder) {
WindowOrder ob = (WindowOrder) that;
return window.equals(ob.window) && num == ob.num;
}
return false;
23. }
}
Building.java
public class Building {
Apartment[] apartments;
public Building(Apartment[] apartments) {
this.apartments = apartments;
}
// Return an array of window orders for all apartments in the building
WindowOrder[] order() {
WindowOrder[] windowOrders = apartments[0].totalOrder();
for (int i = 1; i < apartments.length; i++) {
WindowOrder[] liveWindowOrder = apartments[i].totalOrder();
for (WindowOrder j : liveWindowOrder) {
boolean mergeOrder = false;
for (WindowOrder k : windowOrders) {
if (k.window.equals(j.window)) {
k.add(j);
mergeOrder = true;
break;
}
}
if (!mergeOrder) {
WindowOrder[] newWindowOrders = new WindowOrder[windowOrders.length +
1];
for (int l = 0; l < windowOrders.length; l++) {
newWindowOrders[l] = windowOrders[l];
}
newWindowOrders[windowOrders.length] = j;
windowOrders = newWindowOrders;
}
}
}
return windowOrders;
24. }
public String toString() {
String[] roomsArr = new String[apartments.length];
for (int i = 0; i < apartments.length; i++) {
roomsArr[i] = apartments[i].toString();
}
return String.join(" ", roomsArr) + " ";
}
}
Testing.java
import org.junit.*;
import static org.junit.Assert.*;
import java.lang.reflect.Field;
import org.junit.FixMethodOrder;
import org.junit.runners.MethodSorters;
//@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class Testing {
Window window;
WindowOrder windoworder;
Room room;
Room masterbedroom;
Room guestroom;
Room livingroom;
Apartment apartment;
Apartment onebedroom;
Apartment twobedrom;
Apartment threebedroom;
Building building;
Room []rooms ;
25. Apartment [] apartments;
//@Before
public void setUp()
{
this.window= new Window(10, 20);
windoworder = new WindowOrder(window, 100);
this.room= new Room(window, 5);
this.masterbedroom= new MasterBedroom();
this.guestroom= new GuestRoom();
this.livingroom= new LivingRoom();
rooms =new Room[5];
rooms[0]=masterbedroom;
rooms[1]=guestroom;
rooms[2]=livingroom;
rooms[3]=masterbedroom;
rooms[4]=livingroom;
this.apartment= new Apartment(30, rooms);
this.onebedroom= new OneBedroom(10);
this.twobedrom=new TwoBedroom(5);
this.threebedroom=new ThreeBedroom(15);
apartments = new Apartment[6];
apartments[0] = onebedroom;
apartments[1] = twobedrom;
apartments[2] = threebedroom;
apartments[3] = onebedroom;
apartments[4] = onebedroom;
apartments[5] = twobedrom;
this.building= new Building(apartments);
}
//@After
public void tearDown()
{
this.window= null;
26. this.windoworder=null;
this.room= null;
this.masterbedroom= null;
this.guestroom= null;
this.livingroom= null;
this.apartment= null;
this.onebedroom= null;
this.twobedrom=null;
this.threebedroom=null;
this.building=null;
}
private Object getField( Object instance, String name ) throws Exception
{
Class c = instance.getClass();
Field f = c.getDeclaredField( name );
f.setAccessible( true );
return f.get( instance );
}
//@Test
public void AA_TestWindowConstructor() throws Exception{
assertEquals(this.window.getClass().getSimpleName().toString(), "Window");
assertEquals(10,(int)getField(window,"width"));
assertEquals(20,getField(window,"height"));
}
//@Test
public void AB_TestWindowEquals() throws Exception{
assertTrue(this.window.equals(new Window(10,20)));
assertTrue(this.window.equals(this.window));
assertFalse(this.window.equals(new Window(1,20)));
}
//@Test
public void BA_TestWindowOrderConstructor(){
assertEquals(this.windoworder.getClass().getSimpleName().toString(), "WindowOrder");
assertEquals(this.window,windoworder.window);