SlideShare a Scribd company logo
1 of 15
Download to read offline
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<>();
// 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) {
// 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;
}
}
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;
}
}
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;
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;
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;
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);
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;
}
}
//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;
}
}
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) {
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() {
return String.format("%d (%s)", numOfWindows, window);
}
@Override
public boolean equals(Object that){
if(that instanceof Room){
Room r = (Room)that;
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;
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;
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;
}
}

More Related Content

Similar to Dont change the templates, and just fill out the TODO parts on .pdf

Java Code for Sample Projects Methods
Java Code for Sample Projects MethodsJava Code for Sample Projects Methods
Java Code for Sample Projects Methodsjwjablonski
 
Flink Batch Processing and Iterations
Flink Batch Processing and IterationsFlink Batch Processing and Iterations
Flink Batch Processing and IterationsSameer Wadkar
 
Transaction is a monad
Transaction is a  monadTransaction is a  monad
Transaction is a monadJarek Ratajski
 
XpUg Coding Dojo: KataYahtzee in Ocp way
XpUg Coding Dojo: KataYahtzee in Ocp wayXpUg Coding Dojo: KataYahtzee in Ocp way
XpUg Coding Dojo: KataYahtzee in Ocp wayGiordano Scalzo
 
Use the following data set that compares age to average years lef.docx
Use the following data set that compares age to average years lef.docxUse the following data set that compares age to average years lef.docx
Use the following data set that compares age to average years lef.docxdickonsondorris
 
Below is the assignment description and the file I have written..pdf
Below is the assignment description and the file I have written..pdfBelow is the assignment description and the file I have written..pdf
Below is the assignment description and the file I have written..pdfinfo673628
 
1 MVC – Ajax and Modal Views AJAX stands for Asynch.docx
1  MVC – Ajax and Modal Views AJAX stands for Asynch.docx1  MVC – Ajax and Modal Views AJAX stands for Asynch.docx
1 MVC – Ajax and Modal Views AJAX stands for Asynch.docxhoney725342
 
Csphtp1 07
Csphtp1 07Csphtp1 07
Csphtp1 07HUST
 
Everything needs to be according to the instructions- thank you! SUPPO.pdf
Everything needs to be according to the instructions- thank you! SUPPO.pdfEverything needs to be according to the instructions- thank you! SUPPO.pdf
Everything needs to be according to the instructions- thank you! SUPPO.pdffirstchoiceajmer
 
Please find my solution.Please let me know in case of any issue..pdf
Please find my solution.Please let me know in case of any issue..pdfPlease find my solution.Please let me know in case of any issue..pdf
Please find my solution.Please let me know in case of any issue..pdfpremkhatri99
 
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdfPROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdfclimatecontrolsv
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data StructureSriram Raj
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data StructureZidny Nafan
 

Similar to Dont change the templates, and just fill out the TODO parts on .pdf (20)

JAVA Write a class called F.pdf
JAVA  Write a class called F.pdfJAVA  Write a class called F.pdf
JAVA Write a class called F.pdf
 
Collections
CollectionsCollections
Collections
 
Java Code for Sample Projects Methods
Java Code for Sample Projects MethodsJava Code for Sample Projects Methods
Java Code for Sample Projects Methods
 
Flink Batch Processing and Iterations
Flink Batch Processing and IterationsFlink Batch Processing and Iterations
Flink Batch Processing and Iterations
 
Transaction is a monad
Transaction is a  monadTransaction is a  monad
Transaction is a monad
 
XpUg Coding Dojo: KataYahtzee in Ocp way
XpUg Coding Dojo: KataYahtzee in Ocp wayXpUg Coding Dojo: KataYahtzee in Ocp way
XpUg Coding Dojo: KataYahtzee in Ocp way
 
Thread
ThreadThread
Thread
 
Java Class Design
Java Class DesignJava Class Design
Java Class Design
 
Use the following data set that compares age to average years lef.docx
Use the following data set that compares age to average years lef.docxUse the following data set that compares age to average years lef.docx
Use the following data set that compares age to average years lef.docx
 
Below is the assignment description and the file I have written..pdf
Below is the assignment description and the file I have written..pdfBelow is the assignment description and the file I have written..pdf
Below is the assignment description and the file I have written..pdf
 
Lec2
Lec2Lec2
Lec2
 
1 MVC – Ajax and Modal Views AJAX stands for Asynch.docx
1  MVC – Ajax and Modal Views AJAX stands for Asynch.docx1  MVC – Ajax and Modal Views AJAX stands for Asynch.docx
1 MVC – Ajax and Modal Views AJAX stands for Asynch.docx
 
Csphtp1 07
Csphtp1 07Csphtp1 07
Csphtp1 07
 
Java generics
Java genericsJava generics
Java generics
 
Everything needs to be according to the instructions- thank you! SUPPO.pdf
Everything needs to be according to the instructions- thank you! SUPPO.pdfEverything needs to be according to the instructions- thank you! SUPPO.pdf
Everything needs to be according to the instructions- thank you! SUPPO.pdf
 
Please find my solution.Please let me know in case of any issue..pdf
Please find my solution.Please let me know in case of any issue..pdfPlease find my solution.Please let me know in case of any issue..pdf
Please find my solution.Please let me know in case of any issue..pdf
 
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdfPROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Lezione03
Lezione03Lezione03
Lezione03
 

More from dbrienmhompsonkath75

Give examples of system which can achieve some security requirement.pdf
Give examples of system which can achieve some security requirement.pdfGive examples of system which can achieve some security requirement.pdf
Give examples of system which can achieve some security requirement.pdfdbrienmhompsonkath75
 
Help me with these questions please.1. Name four characteristics t.pdf
Help me with these questions please.1. Name four characteristics t.pdfHelp me with these questions please.1. Name four characteristics t.pdf
Help me with these questions please.1. Name four characteristics t.pdfdbrienmhompsonkath75
 
Describe current and emerging roles of the patient record in HIT toda.pdf
Describe current and emerging roles of the patient record in HIT toda.pdfDescribe current and emerging roles of the patient record in HIT toda.pdf
Describe current and emerging roles of the patient record in HIT toda.pdfdbrienmhompsonkath75
 
Directions Problem 1. A female with Muppetrus bristle mates with a .pdf
Directions Problem 1. A female with Muppetrus bristle mates with a .pdfDirections Problem 1. A female with Muppetrus bristle mates with a .pdf
Directions Problem 1. A female with Muppetrus bristle mates with a .pdfdbrienmhompsonkath75
 
Describe the roll of each of the following in membrane transport a.pdf
Describe the roll of each of the following in membrane transport a.pdfDescribe the roll of each of the following in membrane transport a.pdf
Describe the roll of each of the following in membrane transport a.pdfdbrienmhompsonkath75
 
Compare and contrast transactional and transformational leadership.pdf
Compare and contrast transactional and transformational leadership.pdfCompare and contrast transactional and transformational leadership.pdf
Compare and contrast transactional and transformational leadership.pdfdbrienmhompsonkath75
 
Calculator Which of the following is not an asset Oa, owners equi.pdf
Calculator Which of the following is not an asset Oa, owners equi.pdfCalculator Which of the following is not an asset Oa, owners equi.pdf
Calculator Which of the following is not an asset Oa, owners equi.pdfdbrienmhompsonkath75
 
You are installing a KVM switch for a small business customer so tha.pdf
You are installing a KVM switch for a small business customer so tha.pdfYou are installing a KVM switch for a small business customer so tha.pdf
You are installing a KVM switch for a small business customer so tha.pdfdbrienmhompsonkath75
 
Why is metabolism important for physiological processesSolution.pdf
Why is metabolism important for physiological processesSolution.pdfWhy is metabolism important for physiological processesSolution.pdf
Why is metabolism important for physiological processesSolution.pdfdbrienmhompsonkath75
 
Which macromolecule is primarily responsible for producing the pheno.pdf
Which macromolecule is primarily responsible for producing the pheno.pdfWhich macromolecule is primarily responsible for producing the pheno.pdf
Which macromolecule is primarily responsible for producing the pheno.pdfdbrienmhompsonkath75
 
which invertebrate phylum is considered the most evolutionarily succe.pdf
which invertebrate phylum is considered the most evolutionarily succe.pdfwhich invertebrate phylum is considered the most evolutionarily succe.pdf
which invertebrate phylum is considered the most evolutionarily succe.pdfdbrienmhompsonkath75
 
What suggestions would you offer to parents and teachers who want to.pdf
What suggestions would you offer to parents and teachers who want to.pdfWhat suggestions would you offer to parents and teachers who want to.pdf
What suggestions would you offer to parents and teachers who want to.pdfdbrienmhompsonkath75
 
What is the F2 generation when the F1 generation of +++y cv f is cr.pdf
What is the F2 generation when the F1 generation of +++y cv f is cr.pdfWhat is the F2 generation when the F1 generation of +++y cv f is cr.pdf
What is the F2 generation when the F1 generation of +++y cv f is cr.pdfdbrienmhompsonkath75
 
What features on the plasmid allows it to replicate independent o.pdf
What features on the plasmid allows it to replicate independent o.pdfWhat features on the plasmid allows it to replicate independent o.pdf
What features on the plasmid allows it to replicate independent o.pdfdbrienmhompsonkath75
 
What are the use of Fibonacci numbers or the Golden Ratio in nature,.pdf
What are the use of Fibonacci numbers or the Golden Ratio in nature,.pdfWhat are the use of Fibonacci numbers or the Golden Ratio in nature,.pdf
What are the use of Fibonacci numbers or the Golden Ratio in nature,.pdfdbrienmhompsonkath75
 
Based on the following schematic with 3 D flip-flops, Write complete .pdf
Based on the following schematic with 3 D flip-flops, Write complete .pdfBased on the following schematic with 3 D flip-flops, Write complete .pdf
Based on the following schematic with 3 D flip-flops, Write complete .pdfdbrienmhompsonkath75
 
Although O_2 does not participate directly in the reactions of the TC.pdf
Although O_2 does not participate directly in the reactions of the TC.pdfAlthough O_2 does not participate directly in the reactions of the TC.pdf
Although O_2 does not participate directly in the reactions of the TC.pdfdbrienmhompsonkath75
 
8. Which of the following is NOT a transcriptionally repressed genom.pdf
8. Which of the following is NOT a transcriptionally repressed genom.pdf8. Which of the following is NOT a transcriptionally repressed genom.pdf
8. Which of the following is NOT a transcriptionally repressed genom.pdfdbrienmhompsonkath75
 
49. Autonomic nervous system function is influenced by A) cerebral co.pdf
49. Autonomic nervous system function is influenced by A) cerebral co.pdf49. Autonomic nervous system function is influenced by A) cerebral co.pdf
49. Autonomic nervous system function is influenced by A) cerebral co.pdfdbrienmhompsonkath75
 
Think about the structure of a root tip. In which region would you e.pdf
Think about the structure of a root tip. In which region would you e.pdfThink about the structure of a root tip. In which region would you e.pdf
Think about the structure of a root tip. In which region would you e.pdfdbrienmhompsonkath75
 

More from dbrienmhompsonkath75 (20)

Give examples of system which can achieve some security requirement.pdf
Give examples of system which can achieve some security requirement.pdfGive examples of system which can achieve some security requirement.pdf
Give examples of system which can achieve some security requirement.pdf
 
Help me with these questions please.1. Name four characteristics t.pdf
Help me with these questions please.1. Name four characteristics t.pdfHelp me with these questions please.1. Name four characteristics t.pdf
Help me with these questions please.1. Name four characteristics t.pdf
 
Describe current and emerging roles of the patient record in HIT toda.pdf
Describe current and emerging roles of the patient record in HIT toda.pdfDescribe current and emerging roles of the patient record in HIT toda.pdf
Describe current and emerging roles of the patient record in HIT toda.pdf
 
Directions Problem 1. A female with Muppetrus bristle mates with a .pdf
Directions Problem 1. A female with Muppetrus bristle mates with a .pdfDirections Problem 1. A female with Muppetrus bristle mates with a .pdf
Directions Problem 1. A female with Muppetrus bristle mates with a .pdf
 
Describe the roll of each of the following in membrane transport a.pdf
Describe the roll of each of the following in membrane transport a.pdfDescribe the roll of each of the following in membrane transport a.pdf
Describe the roll of each of the following in membrane transport a.pdf
 
Compare and contrast transactional and transformational leadership.pdf
Compare and contrast transactional and transformational leadership.pdfCompare and contrast transactional and transformational leadership.pdf
Compare and contrast transactional and transformational leadership.pdf
 
Calculator Which of the following is not an asset Oa, owners equi.pdf
Calculator Which of the following is not an asset Oa, owners equi.pdfCalculator Which of the following is not an asset Oa, owners equi.pdf
Calculator Which of the following is not an asset Oa, owners equi.pdf
 
You are installing a KVM switch for a small business customer so tha.pdf
You are installing a KVM switch for a small business customer so tha.pdfYou are installing a KVM switch for a small business customer so tha.pdf
You are installing a KVM switch for a small business customer so tha.pdf
 
Why is metabolism important for physiological processesSolution.pdf
Why is metabolism important for physiological processesSolution.pdfWhy is metabolism important for physiological processesSolution.pdf
Why is metabolism important for physiological processesSolution.pdf
 
Which macromolecule is primarily responsible for producing the pheno.pdf
Which macromolecule is primarily responsible for producing the pheno.pdfWhich macromolecule is primarily responsible for producing the pheno.pdf
Which macromolecule is primarily responsible for producing the pheno.pdf
 
which invertebrate phylum is considered the most evolutionarily succe.pdf
which invertebrate phylum is considered the most evolutionarily succe.pdfwhich invertebrate phylum is considered the most evolutionarily succe.pdf
which invertebrate phylum is considered the most evolutionarily succe.pdf
 
What suggestions would you offer to parents and teachers who want to.pdf
What suggestions would you offer to parents and teachers who want to.pdfWhat suggestions would you offer to parents and teachers who want to.pdf
What suggestions would you offer to parents and teachers who want to.pdf
 
What is the F2 generation when the F1 generation of +++y cv f is cr.pdf
What is the F2 generation when the F1 generation of +++y cv f is cr.pdfWhat is the F2 generation when the F1 generation of +++y cv f is cr.pdf
What is the F2 generation when the F1 generation of +++y cv f is cr.pdf
 
What features on the plasmid allows it to replicate independent o.pdf
What features on the plasmid allows it to replicate independent o.pdfWhat features on the plasmid allows it to replicate independent o.pdf
What features on the plasmid allows it to replicate independent o.pdf
 
What are the use of Fibonacci numbers or the Golden Ratio in nature,.pdf
What are the use of Fibonacci numbers or the Golden Ratio in nature,.pdfWhat are the use of Fibonacci numbers or the Golden Ratio in nature,.pdf
What are the use of Fibonacci numbers or the Golden Ratio in nature,.pdf
 
Based on the following schematic with 3 D flip-flops, Write complete .pdf
Based on the following schematic with 3 D flip-flops, Write complete .pdfBased on the following schematic with 3 D flip-flops, Write complete .pdf
Based on the following schematic with 3 D flip-flops, Write complete .pdf
 
Although O_2 does not participate directly in the reactions of the TC.pdf
Although O_2 does not participate directly in the reactions of the TC.pdfAlthough O_2 does not participate directly in the reactions of the TC.pdf
Although O_2 does not participate directly in the reactions of the TC.pdf
 
8. Which of the following is NOT a transcriptionally repressed genom.pdf
8. Which of the following is NOT a transcriptionally repressed genom.pdf8. Which of the following is NOT a transcriptionally repressed genom.pdf
8. Which of the following is NOT a transcriptionally repressed genom.pdf
 
49. Autonomic nervous system function is influenced by A) cerebral co.pdf
49. Autonomic nervous system function is influenced by A) cerebral co.pdf49. Autonomic nervous system function is influenced by A) cerebral co.pdf
49. Autonomic nervous system function is influenced by A) cerebral co.pdf
 
Think about the structure of a root tip. In which region would you e.pdf
Think about the structure of a root tip. In which region would you e.pdfThink about the structure of a root tip. In which region would you e.pdf
Think about the structure of a root tip. In which region would you e.pdf
 

Recently uploaded

Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 

Recently uploaded (20)

Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 

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() {
  • 13. return String.format("%d (%s)", numOfWindows, window); } @Override public boolean equals(Object that){ if(that instanceof Room){ Room r = (Room)that; 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() {
  • 14. 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; } 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) {
  • 15. if(window.equals(order.window)) { this.num += order.num; } return this; } WindowOrder times(int number) { this.num *= number; 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; } }