Todo
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
}
}
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
}
}
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);
}
}
}
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
publ.
Todopackage hwk6; This class contains the configuration of a t.pdf
1. Todo
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() });
}
}
2. 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
}
}
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
}
3. // 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
}
}
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);
}
}
}
package hwk6;
public class Room {
Window window;
int numOfWindows;
Room(Window window, int numOfWindows) {
this.window = window;
4. 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)
5. @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
}
}
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
}
}
6. 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
}
}
7. Testing
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();
9. 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;
}
10. @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());
}
11. @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());
12. }
@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);
13. 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);
14. 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
15. 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
16. 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());
}
}
please finish todo. 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 in cludes a field to store the
number of units of this tvpe. 3. Room and its subclass LivingRoom, MasterBedroom, and
GuestRoom Each room has some windows of certain size. Different type of rooms have
17. 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 wl fill in the methods for classes described above. The
provided code template has instruction in the comments above the methods that vou 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.
Solution
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.toString());
System.out.println("Window orders are: ");
for(WindowOrder order: orders) {
System.out.println(order.toString());
}
}
}
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() {
18. WindowOrder temp[] = new WindowOrder[3];
temp[0] = new WindowOrder(new Window(6,8),0);
temp[1] = new WindowOrder(new Window(4,6),0);
temp[2] = new WindowOrder(new Window(5,6),0);
for(int i=0;i