SlideShare a Scribd company logo
1 of 34
Download to read offline
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 room: 2 (5 X 6 window))(Guest room: 2 (5 X 6 window))
//
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 {
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;
}
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() {
// 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 {
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:
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();
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;
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);
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
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
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());
}
@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);
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);
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))";
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(){
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
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;
}
// 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
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());
}
}
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 &&
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;
}
}
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;
}
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 ;
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;
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);
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
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
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());
}
//@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);
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);
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))";
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(){
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());
}
}

More Related Content

Similar to APARTMENT BUILDING HOMEWORK1 What’s the problemSuppose you want.pdf

Runnable interface.34
Runnable interface.34Runnable interface.34
Runnable interface.34
myrajendra
 
C#_hw9_S17.docModify AnimatedBall on Chapter 13. Instead of dr.docx
C#_hw9_S17.docModify AnimatedBall on Chapter 13. Instead of dr.docxC#_hw9_S17.docModify AnimatedBall on Chapter 13. Instead of dr.docx
C#_hw9_S17.docModify AnimatedBall on Chapter 13. Instead of dr.docx
RAHUL126667
 
Use of Apache Commons and Utilities
Use of Apache Commons and UtilitiesUse of Apache Commons and Utilities
Use of Apache Commons and Utilities
Pramod Kumar
 

Similar to APARTMENT BUILDING HOMEWORK1 What’s the problemSuppose you want.pdf (20)

srgoc
srgocsrgoc
srgoc
 
Thread
ThreadThread
Thread
 
Modern Java Workshop
Modern Java WorkshopModern Java Workshop
Modern Java Workshop
 
Getting Started - Console Program and Problem Solving
Getting Started - Console Program and Problem SolvingGetting Started - Console Program and Problem Solving
Getting Started - Console Program and Problem Solving
 
SPF Getting Started - Console Program
SPF Getting Started - Console ProgramSPF Getting Started - Console Program
SPF Getting Started - Console Program
 
csc ppt 15.pptx
csc ppt 15.pptxcsc ppt 15.pptx
csc ppt 15.pptx
 
Functional Programming with C#
Functional Programming with C#Functional Programming with C#
Functional Programming with C#
 
Runnable interface.34
Runnable interface.34Runnable interface.34
Runnable interface.34
 
The Ring programming language version 1.5.4 book - Part 71 of 185
The Ring programming language version 1.5.4 book - Part 71 of 185The Ring programming language version 1.5.4 book - Part 71 of 185
The Ring programming language version 1.5.4 book - Part 71 of 185
 
Design patterns for fun and profit
Design patterns for fun and profitDesign patterns for fun and profit
Design patterns for fun and profit
 
The Ring programming language version 1.3 book - Part 5 of 88
The Ring programming language version 1.3 book - Part 5 of 88The Ring programming language version 1.3 book - Part 5 of 88
The Ring programming language version 1.3 book - Part 5 of 88
 
Introduction to Griffon
Introduction to GriffonIntroduction to Griffon
Introduction to Griffon
 
Ppt on java basics
Ppt on java basicsPpt on java basics
Ppt on java basics
 
Groovy AST Transformations
Groovy AST TransformationsGroovy AST Transformations
Groovy AST Transformations
 
Java Generics
Java GenericsJava Generics
Java Generics
 
The Ring programming language version 1.5.1 book - Part 67 of 180
The Ring programming language version 1.5.1 book - Part 67 of 180The Ring programming language version 1.5.1 book - Part 67 of 180
The Ring programming language version 1.5.1 book - Part 67 of 180
 
C#_hw9_S17.docModify AnimatedBall on Chapter 13. Instead of dr.docx
C#_hw9_S17.docModify AnimatedBall on Chapter 13. Instead of dr.docxC#_hw9_S17.docModify AnimatedBall on Chapter 13. Instead of dr.docx
C#_hw9_S17.docModify AnimatedBall on Chapter 13. Instead of dr.docx
 
25-inheritance-polymorphism.ppt
25-inheritance-polymorphism.ppt25-inheritance-polymorphism.ppt
25-inheritance-polymorphism.ppt
 
Use of Apache Commons and Utilities
Use of Apache Commons and UtilitiesUse of Apache Commons and Utilities
Use of Apache Commons and Utilities
 
Presentation - Windows App Development - II - Mr. Chandan Gupta
Presentation - Windows App Development - II - Mr. Chandan GuptaPresentation - Windows App Development - II - Mr. Chandan Gupta
Presentation - Windows App Development - II - Mr. Chandan Gupta
 

More from arihantcomp1008

Can someone please help me write a case brief for two supreme court .pdf
Can someone please help me write a case brief for two supreme court .pdfCan someone please help me write a case brief for two supreme court .pdf
Can someone please help me write a case brief for two supreme court .pdf
arihantcomp1008
 
Write a A minimum of 2 pages of text describing the hardware compone.pdf
Write a A minimum of 2 pages of text describing the hardware compone.pdfWrite a A minimum of 2 pages of text describing the hardware compone.pdf
Write a A minimum of 2 pages of text describing the hardware compone.pdf
arihantcomp1008
 
Which of the following would be sufficient for the Hardy-Weinberg equ.pdf
Which of the following would be sufficient for the Hardy-Weinberg equ.pdfWhich of the following would be sufficient for the Hardy-Weinberg equ.pdf
Which of the following would be sufficient for the Hardy-Weinberg equ.pdf
arihantcomp1008
 
Why do a lot of scientist feel like god doesnt exist What is th.pdf
Why do a lot of scientist feel like god doesnt exist What is th.pdfWhy do a lot of scientist feel like god doesnt exist What is th.pdf
Why do a lot of scientist feel like god doesnt exist What is th.pdf
arihantcomp1008
 
true false 1, Sampling is always wrong because it is stup.pdf
true false 1, Sampling is always wrong because it is stup.pdftrue false 1, Sampling is always wrong because it is stup.pdf
true false 1, Sampling is always wrong because it is stup.pdf
arihantcomp1008
 
Susie has influenza A. She lives in a residence hall where there hav.pdf
Susie has influenza A. She lives in a residence hall where there hav.pdfSusie has influenza A. She lives in a residence hall where there hav.pdf
Susie has influenza A. She lives in a residence hall where there hav.pdf
arihantcomp1008
 
Section 5 Controlling Risk Given the following categories or areas .pdf
Section 5 Controlling Risk Given the following categories or areas .pdfSection 5 Controlling Risk Given the following categories or areas .pdf
Section 5 Controlling Risk Given the following categories or areas .pdf
arihantcomp1008
 
5)Sonny has the following account balances as of January 1, 2018 bef.pdf
5)Sonny has the following account balances as of January 1, 2018 bef.pdf5)Sonny has the following account balances as of January 1, 2018 bef.pdf
5)Sonny has the following account balances as of January 1, 2018 bef.pdf
arihantcomp1008
 
Question In C Programming In mathematics, a set is a colle...Sav.pdf
Question In C Programming In mathematics, a set is a colle...Sav.pdfQuestion In C Programming In mathematics, a set is a colle...Sav.pdf
Question In C Programming In mathematics, a set is a colle...Sav.pdf
arihantcomp1008
 
Q1. What is the primary reason that Fenders blue butterflies are e.pdf
Q1. What is the primary reason that Fenders blue butterflies are e.pdfQ1. What is the primary reason that Fenders blue butterflies are e.pdf
Q1. What is the primary reason that Fenders blue butterflies are e.pdf
arihantcomp1008
 

More from arihantcomp1008 (20)

Can someone please help me write a case brief for two supreme court .pdf
Can someone please help me write a case brief for two supreme court .pdfCan someone please help me write a case brief for two supreme court .pdf
Can someone please help me write a case brief for two supreme court .pdf
 
Analysis of data collected in a study of educational levels (Did not .pdf
Analysis of data collected in a study of educational levels (Did not .pdfAnalysis of data collected in a study of educational levels (Did not .pdf
Analysis of data collected in a study of educational levels (Did not .pdf
 
As individuals age, they are likely to becomeMore alike and less .pdf
As individuals age, they are likely to becomeMore alike and less .pdfAs individuals age, they are likely to becomeMore alike and less .pdf
As individuals age, they are likely to becomeMore alike and less .pdf
 
Why does Ernst Mayrs biological species concept NOT apply to Archae.pdf
Why does Ernst Mayrs biological species concept NOT apply to Archae.pdfWhy does Ernst Mayrs biological species concept NOT apply to Archae.pdf
Why does Ernst Mayrs biological species concept NOT apply to Archae.pdf
 
Write a A minimum of 2 pages of text describing the hardware compone.pdf
Write a A minimum of 2 pages of text describing the hardware compone.pdfWrite a A minimum of 2 pages of text describing the hardware compone.pdf
Write a A minimum of 2 pages of text describing the hardware compone.pdf
 
Which of the following would be sufficient for the Hardy-Weinberg equ.pdf
Which of the following would be sufficient for the Hardy-Weinberg equ.pdfWhich of the following would be sufficient for the Hardy-Weinberg equ.pdf
Which of the following would be sufficient for the Hardy-Weinberg equ.pdf
 
Which of the following is NOT a measure of precision 8 O (A) standar.pdf
Which of the following is NOT a measure of precision 8 O (A) standar.pdfWhich of the following is NOT a measure of precision 8 O (A) standar.pdf
Which of the following is NOT a measure of precision 8 O (A) standar.pdf
 
Write C++ variable declarations to create one int variable called num.pdf
Write C++ variable declarations to create one int variable called num.pdfWrite C++ variable declarations to create one int variable called num.pdf
Write C++ variable declarations to create one int variable called num.pdf
 
Why do a lot of scientist feel like god doesnt exist What is th.pdf
Why do a lot of scientist feel like god doesnt exist What is th.pdfWhy do a lot of scientist feel like god doesnt exist What is th.pdf
Why do a lot of scientist feel like god doesnt exist What is th.pdf
 
What is the United States Comparative Advantage....please be specifi.pdf
What is the United States Comparative Advantage....please be specifi.pdfWhat is the United States Comparative Advantage....please be specifi.pdf
What is the United States Comparative Advantage....please be specifi.pdf
 
true false 1, Sampling is always wrong because it is stup.pdf
true false 1, Sampling is always wrong because it is stup.pdftrue false 1, Sampling is always wrong because it is stup.pdf
true false 1, Sampling is always wrong because it is stup.pdf
 
The rearrangement of alleles by the process of crossing over is call.pdf
The rearrangement of alleles by the process of crossing over is call.pdfThe rearrangement of alleles by the process of crossing over is call.pdf
The rearrangement of alleles by the process of crossing over is call.pdf
 
Susie has influenza A. She lives in a residence hall where there hav.pdf
Susie has influenza A. She lives in a residence hall where there hav.pdfSusie has influenza A. She lives in a residence hall where there hav.pdf
Susie has influenza A. She lives in a residence hall where there hav.pdf
 
Section 5 Controlling Risk Given the following categories or areas .pdf
Section 5 Controlling Risk Given the following categories or areas .pdfSection 5 Controlling Risk Given the following categories or areas .pdf
Section 5 Controlling Risk Given the following categories or areas .pdf
 
5)Sonny has the following account balances as of January 1, 2018 bef.pdf
5)Sonny has the following account balances as of January 1, 2018 bef.pdf5)Sonny has the following account balances as of January 1, 2018 bef.pdf
5)Sonny has the following account balances as of January 1, 2018 bef.pdf
 
Question In C Programming In mathematics, a set is a colle...Sav.pdf
Question In C Programming In mathematics, a set is a colle...Sav.pdfQuestion In C Programming In mathematics, a set is a colle...Sav.pdf
Question In C Programming In mathematics, a set is a colle...Sav.pdf
 
Q1. What is the primary reason that Fenders blue butterflies are e.pdf
Q1. What is the primary reason that Fenders blue butterflies are e.pdfQ1. What is the primary reason that Fenders blue butterflies are e.pdf
Q1. What is the primary reason that Fenders blue butterflies are e.pdf
 
Normality assumption is very important. A researcher is interested i.pdf
Normality assumption is very important. A researcher is interested i.pdfNormality assumption is very important. A researcher is interested i.pdf
Normality assumption is very important. A researcher is interested i.pdf
 
Punnett square Father and mother are heterozygous and each are .pdf
Punnett square Father and mother are heterozygous and each are .pdfPunnett square Father and mother are heterozygous and each are .pdf
Punnett square Father and mother are heterozygous and each are .pdf
 
Property Values Is this rooted tree Leaf vertices Intenal vertices A.pdf
Property Values Is this rooted tree Leaf vertices Intenal vertices A.pdfProperty Values Is this rooted tree Leaf vertices Intenal vertices A.pdf
Property Values Is this rooted tree Leaf vertices Intenal vertices A.pdf
 

Recently uploaded

QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
httgc7rh9c
 

Recently uploaded (20)

REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Economic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food AdditivesEconomic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food Additives
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
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();
  • 9. 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; 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;
  • 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);
  • 27. 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 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");
  • 28. 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 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(){
  • 29. 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()); } //@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(){
  • 30. 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); 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);
  • 31. 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); 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
  • 32. 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))"; 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());
  • 33. } //@Test public void R_TestMasterRoomToString(){ String expected= "Master bedroom: 3 (4 X 6 window)"; assertEquals(expected,masterbedroom.toString()); } //@Test public void S_TestGuestRoomToString(){ 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)) ";