Don\'t change the templates, and just fill out the // TODO parts on below of templates, please.
Instruction
Reimplement a program using List and ArrayList . Recall that it is a
little tricky to merge arrays of window orders when the arrays may have dierent
sizes. Using List to represent arrays of window orders is more convenient since
we can add a new window order if its size is not found in the existing orders.
To this end, we have added a new class called TotalOrder , which represents
a list of window orders. It contains methods to add a single window order or
another total order. These methods will ensure that the orders are propertly
merged by window sizes.
What to implement?
You will use in the methods for classes described above. The provided code
template has instruction in the comments above the methods that you will
implement.
Output
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:
225 6 X 8 window
135 4 X 6 window
70 5 X 6 window
Note
TotalOrder:
o add(WindowOrder newOrder): To add the “newOrder” at first check if a similar order is
already stored in the array list. You should check this within a loop structure. If it already exists
you should merge it with the similar window order using a method call to “add” (which is a
method declared in WindowOrder to merge the orders). Otherwise, simply add the “newOrder”
to the end of the array list through calling the “add” method of the array list. The method returns
the current object.
o add(TotalOrder that): this method makes use of the previous one. You should iterate over all of
the window orders listed in “that” and add them to the array list by passing each one of its
elements them as the input argument to the above “add” method.
o times(int num): for every element stored in the arraylist of the class, call “times” method and
pass “num” as the input argument to it.
o toString(): for every element stored in the arraylist, call its toString() method +”\ ”. Store the
result in a String variable and then return it.
- Apartment:
o orderForOneUnit(): create a new “TotalOrder” object. Add the window orders of every room
to the total order. Return the total order.
o totalOrder(): call the previous method to get the total order for one unit. Call the “times”
method of the total order and use number of apartments as input.
- Building:
o order(): declare and create a TotalOrder object. Add the total order of each apartment into the
object you just created and return it after the loop.
Templates
TotalOrder.java
import java.util.ArrayList;
import java.util.List;
// This class represents a collection of window orders using an ArrayList
public class TotalOrder {
List orders = new ArrayList.
Dont change the templates, and just fill out the TODO parts on .pdf
1. Don't change the templates, and just fill out the // TODO parts on below of templates, please.
Instruction
Reimplement a program using List and ArrayList . Recall that it is a
little tricky to merge arrays of window orders when the arrays may have dierent
sizes. Using List to represent arrays of window orders is more convenient since
we can add a new window order if its size is not found in the existing orders.
To this end, we have added a new class called TotalOrder , which represents
a list of window orders. It contains methods to add a single window order or
another total order. These methods will ensure that the orders are propertly
merged by window sizes.
What to implement?
You will use in the methods for classes described above. The provided code
template has instruction in the comments above the methods that you will
implement.
Output
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:
225 6 X 8 window
135 4 X 6 window
70 5 X 6 window
Note
TotalOrder:
o add(WindowOrder newOrder): To add the “newOrder” at first check if a similar order is
already stored in the array list. You should check this within a loop structure. If it already exists
you should merge it with the similar window order using a method call to “add” (which is a
method declared in WindowOrder to merge the orders). Otherwise, simply add the “newOrder”
to the end of the array list through calling the “add” method of the array list. The method returns
the current object.
o add(TotalOrder that): this method makes use of the previous one. You should iterate over all of
the window orders listed in “that” and add them to the array list by passing each one of its
elements them as the input argument to the above “add” method.
2. o times(int num): for every element stored in the arraylist of the class, call “times” method and
pass “num” as the input argument to it.
o toString(): for every element stored in the arraylist, call its toString() method +” ”. Store the
result in a String variable and then return it.
- Apartment:
o orderForOneUnit(): create a new “TotalOrder” object. Add the window orders of every room
to the total order. Return the total order.
o totalOrder(): call the previous method to get the total order for one unit. Call the “times”
method of the total order and use number of apartments as input.
- Building:
o order(): declare and create a TotalOrder object. Add the total order of each apartment into the
object you just created and return it after the loop.
Templates
TotalOrder.java
import java.util.ArrayList;
import java.util.List;
// This class represents a collection of window orders using an ArrayList
public class TotalOrder {
List orders = new ArrayList<>();
// Add a new window order to this list
// Make sure to merge the orders for window of the same size
// Return the current object
TotalOrder add(WindowOrder newOrder) {
// TODO
}
// Add another collection of window order
// Also make sure that the orders for windows of the same size are merged
// Return the current object
TotalOrder add(TotalOrder that) {
// TODO
}
// Multiple all window orders by "num"
TotalOrder times(int num) {
3. // TODO
}
@Override
public String toString() {
// TODO
}
}
Apartment.java
public class Apartment {
int numOfApartments; // the number of apartments of this type
Room[] rooms; // rooms in this type of apartment
Apartment(int numOfApartments, Room[] rooms) {
this.numOfApartments = numOfApartments;
this.rooms = rooms;
}
// Return the window orders for one apartment of this type as TotalOrder object
TotalOrder orderForOneUnit() {
// TODO
}
// Return the window orders for all apartments of this type
TotalOrder totalOrder() {
// TODO
}
public String toString() {
String ret = numOfApartments + " apartments with ";
for(Room room: rooms) {
ret += String.format("(%s)", room);
}
return ret;
}
4. }
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() });
}
}
Building.java
public class Building {
Apartment[] apartments;
public Building(Apartment[] units) {
this.apartments = units;
}
// Return the total order all apartments in this building
TotalOrder order() {
// TODO
}
public String toString() {
String ret = "";
for(Apartment a: apartments) {
ret += a + " ";
}
return ret;
}
}
5. Main.java
public class Main {
public static void main(String[] args) {
Apartment[] apartments = { new OneBedroom(20), new TwoBedroom(15), new
ThreeBedroom(10) };
Building building = new Building(apartments);
TotalOrder orders = building.order();
System.out.println(building);
System.out.println("Window orders are: " + orders);
}
}
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);
}
@Override
public String toString() {
return String.format("%d (%s)", numOfWindows, window);
}
@Override
public boolean equals(Object that){
if(that instanceof Room){
Room r = (Room)that;
6. return this.numOfWindows==r.numOfWindows&&this.window.equals(r.window);
}
else
return false;
}
}
class MasterBedroom extends Room {
MasterBedroom() {
super(new Window(4, 6), 3);
}
@Override
public String toString() {
return String.format("Master bedroom: %s", super.toString());
}
}
class GuestRoom extends Room {
GuestRoom() {
super(new Window(5, 6), 2);
}
@Override
public String toString() {
return String.format("Guest room: %s", super.toString());
}
}
class LivingRoom extends Room {
LivingRoom() {
super(new Window(6, 8), 5);
}
@Override
public String toString() {
return String.format("Living room: %s", super.toString());
}
}
Window.java
public class Window {
private final int width, height;
7. public Window(int width, int height) {
this.width = width;
this.height = height;
}
public String toString() {
return String.format("%d X %d window", width, height);
}
public boolean equals(Object that) {
boolean ret = false;
if(that instanceof Window) {
Window thatWindow = (Window) that;
ret = width == thatWindow.width && height == thatWindow.height;
}
return ret;
}
}
class WindowOrder {
final Window window;
int num;
WindowOrder(Window window, int num) {
this.window = window;
this.num = num;
}
WindowOrder add (WindowOrder order) {
if(window.equals(order.window)) {
this.num += order.num;
}
return this;
}
WindowOrder times(int number) {
this.num *= number;
8. return this;
}
public String toString() {
return String.format("%d %s", num, window);
}
@Override
public boolean equals(Object that) {
if(that instanceof WindowOrder){
WindowOrder w= (WindowOrder)that;
return this.num==w.num &&this.window.equals(w.window);
}
return false;
}
}
Solution
//TotalOrder.java
import java.util.ArrayList;
import java.util.List;
// This class represents a collection of window orders using an ArrayList
//
public class TotalOrder {
List orders = new ArrayList<>();
// Add a new window order to this list
// Make sure to merge the orders for window of the same size
// Return the current object
TotalOrder add(WindowOrder newOrder) {
boolean isWindowFound = false;
for(WindowOrder windowOrder: orders){
if(windowOrder.window.equals(newOrder.window)){
windowOrder.add(newOrder);
9. isWindowFound = true;
break;
}
}
if(!isWindowFound){
orders.add(newOrder);
}
return this;
}
// Add another collection of window order
// Also make sure that the orders for windows of the same size are merged
// Return the current object
TotalOrder add(TotalOrder that) {
for(WindowOrder windowOrder: that.orders){
add(windowOrder);
}
return this;
}
// Multiple all window orders by "num"
TotalOrder times(int num) {
for(WindowOrder windowOrder: orders){
windowOrder.times(num);
}
return this;
}
@Override
public String toString() {
String string = "";
for(WindowOrder windowOrder: orders){
string+=windowOrder+" ";
}
return string;
}
10. }
//Apartment.java
//
public class Apartment {
int numOfApartments; // the number of apartments of this type
Room[] rooms; // rooms in this type of apartment
Apartment(int numOfApartments, Room[] rooms) {
this.numOfApartments = numOfApartments;
this.rooms = rooms;
}
// Return the window orders for one apartment of this type as TotalOrder object
TotalOrder orderForOneUnit() {
TotalOrder totalOrder = new TotalOrder();
for(Room room : rooms){
totalOrder.add(room.order());
}
return totalOrder;
}
// Return the window orders for all apartments of this type
TotalOrder totalOrder() {
TotalOrder totalOrder = orderForOneUnit();
totalOrder.times(numOfApartments);
return totalOrder;
}
public String toString() {
String ret = numOfApartments + " apartments with ";
for(Room room: rooms) {
ret += String.format("(%s)", room);
}
return ret;
}
11. }
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() });
}
}
//Building.java
//
public class Building {
Apartment[] apartments;
public Building(Apartment[] units) {
this.apartments = units;
}
// Return the total order all apartments in this building
TotalOrder order() {
TotalOrder totalOrder = new TotalOrder();
for(Apartment apartment: apartments){
totalOrder.add(apartment.totalOrder());
}
return totalOrder;
}
public String toString() {
String ret = "";
for(Apartment a: apartments) {
12. ret += a + " ";
}
return ret;
}
}
//Main.java
public class Main {
public static void main(String[] args) {
Apartment[] apartments = { new OneBedroom(20), new TwoBedroom(15), new
ThreeBedroom(10) };
Building building = new Building(apartments);
TotalOrder orders = building.order();
System.out.println(building);
System.out.println("Window orders are: " + orders);
}
}
//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);
}
@Override
public String toString() {