Question

Homework 7 October 26, 2016 1 Whats the problem? You will re-implement homework 6 using List and ArrayList. Recall that it is a little tricky to merge arrays of window orders when the arrays may have different 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. You need to modify a few methods in Apartment and Building while other classes can be reused from homework 6 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 mplement 3 What is provided? We provide a driver class Hwk7.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 W If you run the driver class Hwk7.java you will see output that looks like the hat does output look like? followings (next page)

Templates

Apartment.java

package hwk7;

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 = numOfUnits + " 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

package hwk7;

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 + "\n";

       }

       return ret;

   }

}

Hwk7.java

package hwk7;

public class Hwk7 {

   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:\n" + orders);

   }

}

Room.java

package hwk7;

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(other instanceof Room){

           Room r = (Room)other;

           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());

   }

}

TotalOrder.java

package hwk7;

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
   }
}

Window.java

package hwk7;

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(size.equals(order.size)) {

           this.num += order.num;

       }

       return this;

   }

   WindowOrder times(int number) {

       this.num *= number;

       return this;

   }

  

   public String toString() {

       return String.format("%d %s", num, size);

   }

  

   @Override

   public boolean equals(Object that) {

       if(that instanceof WindowOrder){

           WindowOrder w= (WindowOrder)that;

           return this.num==w.num &&this.size.equals(w.size);

       }

       return false;

   }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer: Sample coding: Hwk7.java: public class Hwk7 public static void main(String[l args) Apartment[] apart = { new oneBedroWindowOrder[ oneUnitOrder() Windoworder ord new Windoworder [this.apt rooms.lengthl; for (int i 0: i < this.apt roomslength iclass oneBedroom extends Apartment OneBedroom (int num of units) super (num of units, new Apt roomsl new LivingRoom (), new Mreturn accumulator; WindowOrderil ord() return ordl); public string tostring ) string sIl new stringlthis.apart.length1; forreturn this.win width aw.win width && this.win height == aw.win height ; return false: class WindowOrder final Apt windows wipublic boolean eguvalent (Object that) if (this-that ) return true; if (that instanceof WindowOrder) WindowOrder wo(WindowOrd

Editable code:

Sample coding:

Hwk7.java:

public class Hwk7

{

    public static void main(String[] args)

    {

        Apartment[] apart = { new OneBedroom(20),

                                   new TwoBedroom(15),

                                   new ThreeBedroom(10) };

        Apt_buildings building = new Apt_buildings(apart);

        WindowOrder[] ords = building.ord();

        System.out.println(building);

        System.out.println("The window orders are: ");

        for (WindowOrder order : ords)

        {

            System.out.println(order);

        }

    }

}

Apartment.java:

public class Apartment

{

    int num_of_units;

    Apt_rooms[] apt_rooms;

    Apartment(int num_of_units, Apt_rooms[] apt_rooms)

    {

        this.num_of_units = num_of_units;

        this.apt_rooms = apt_rooms;

    }

    WindowOrder[] oneUnitOrder()

    {

        WindowOrder[] ord = new WindowOrder[this.apt_rooms.length];

        for (int i = 0; i < this.apt_rooms.length; i++)

        {

            ord[i] = this.apt_rooms[i].order();

        }

        return ord;

    }

    WindowOrder[] overallOrder()

    {

        WindowOrder[] ord = this.oneUnitOrder();

        for (WindowOrder i : ord)

        {

            i.times(num_of_units);

        }

        return ord;

    }

    public String toString()

    {

        String[] parts = new String[this.apt_rooms.length];

        for (int i = 0; i < parts.length; i++)

        {

            parts[i] = this.apt_rooms[i].toString();

        }

        return String.format("%d apartments with (%s)",

                             this.num_of_units,

                             String.join(")(", parts));

    }

}

class OneBedroom extends Apartment

{

    OneBedroom(int num_of_units)

    {

        super(num_of_units, new Apt_rooms[] { new LivingRoom(),

                                       new MasterBedroom() });

    }

}

class TwoBedroom extends Apartment

{

    TwoBedroom(int num_of_units)

    {

        super(num_of_units, new Apt_rooms[] { new LivingRoom(),

                                       new MasterBedroom(),

                                       new GuestRoom() });

    }

}

class ThreeBedroom extends Apartment

{

    ThreeBedroom(int num_of_units)

    {

        super(num_of_units, new Apt_rooms[] { new LivingRoom(),

                                       new MasterBedroom(),

                                       new GuestRoom(),

                                       new GuestRoom() });

    }

    WindowOrder[] orderForOneUnit()

    {

        Apt_rooms[] r = this.apt_rooms;

        return new WindowOrder[] {

            r[0].ord(),

            r[1].ord(),

            r[2].ord().add(r[3].ord())

        };

    }

}

Apt_buildings.java:

import java.util.ArrayList;

public class Apt_buildings

{

    Apartment[] apart;

    public Apt_buildings(Apartment[] apart)

    {

        this.apart = apart;

        assert(this.apart.length > 0);

    }

    WindowOrder[] ord1()

    {

        ArrayList<WindowOrder> accumulator = new ArrayList<WindowOrder>();

        for (Apartment apts : this.apart)

        {

            for (WindowOrder ord : apts.overallOrder())

            {

                boolean inAccumulator = false;

                for (WindowOrder accumulated_Order : accumulator)

                {

                    if (accumulated_Order.window.equals(ord.window))

                    {

                        accumulated_Order.add(ord);

                        inAccumulator = true;

                        break;

                    }

                }

                if (!inAccumulator)

                {

                    accumulator.add(ord);

                }

            }

        }

        return accumulator.toArray(new WindowOrder[accumulator.size()]);

    }

    WindowOrder[] ord2()

    {

      WindowOrder[] accumulator = this.apart[0].overallOrder();

        for (int i = 1; i < this.apart.length; i++)

        {

            WindowOrder[] current = this.apart[i].overallOrder();

            for (int j = 0; j < current.length; j++)

            {

                boolean inAccumulator = false;

                for (int k = 0; k < accumulator.length; k++)

                {

                    if (accumulator[k].window.equals(current[j].window))

                    {

                        accumulator[k].add(current[j]);

                        inAccumulator = true;

                        break;

                    }

                }

                if (!inAccumulator)

                {

                    WindowOrder[] newAccumulator = new WindowOrder[accumulator.length + 1];

                    for (int k = 0; k < accumulator.length; k++)

                    {

                        newAccumulator[k] = accumulator[k];

                    }

                    newAccumulator[accumulator.length] = current[j];

                    accumulator = newAccumulator;

                }

            }

        }

        return accumulator;

    }

    WindowOrder[] ord()

    {

        return ord1();

    }

    public String toString()

    {

        String s[] = new String[this.apart.length + 1];

        for (int i = 0; i < this.apart.length; i++)

        {

            s[i] = this.apart[i].toString();

        }

        s[this.apart.length] = "";

        return String.join("\n", s);

    }

    WindowOrder[] ords()

    {

        throw new UnsupportedOperationException("Not supported yet...");

    }

}

Apt_rooms.java:

public class Apt_rooms

{

    Apt_windows windows;

    int num_of_windows;

    Apt_rooms(Apt_windows windows, int num_of_windows)

    {

        this.windows = windows;

        this.num_of_windows = num_of_windows;

    }

    WindowOrder order()

    {

        return new WindowOrder(windows, num_of_windows);

    }

    public String toString()

    {

        return String.format("%d (%s)", this.num_of_windows, this.windows);

    }

    public boolean equvalent(Object that)

    {

        if (this == that)

        {

            return true;

        }

        if (that instanceof Apt_rooms)

        {

            Apt_rooms ar = (Apt_rooms)that;

            return this.windows.equals(ar.windows) &&

                this.num_of_windows == ar.num_of_windows;

        }

        return false;

    }

    WindowOrder ord()

    {

        throw new UnsupportedOperationException("Not supported yet.");

    }

}

class MasterBedroom extends Apt_rooms

{

    MasterBedroom()

    {

        super(new Apt_windows(4, 6), 3);

    }

    public String toString()

    {

        return String.format("Master bedroom: %s", super.toString());

    }

}

class GuestRoom extends Apt_rooms

{

    GuestRoom()

    {

        super(new Apt_windows(5, 6), 2);

    }

    public String toString()

    {

        return String.format("Guest room: %s", super.toString());

    }

}

class LivingRoom extends Apt_rooms

{

    LivingRoom()

    {

        super(new Apt_windows(6, 8), 5);

    }

    public String toString()

    {

        return String.format("Living room: %s", super.toString());

    }

}

Apt_windows.java:

public class Apt_windows

{

    private final int win_width, win_height;

    public Apt_windows(int win_width, int win_height)

    {

        this.win_width = win_width;

        this.win_height = win_height;

    }

    public String toString()

    {

        return String.format("%d X %d window", this.win_width, this.win_height);

    }

    public boolean equvalent(Object that)

    {

        if (this == that)

        {

            return true;

        }

        if (that instanceof Apt_windows)

        {

            Apt_windows aw = (Apt_windows)that;

            return this.win_width == aw.win_width &&

                this.win_height == aw.win_height;

        }

        return false;

    }

}

class WindowOrder

{

    final Apt_windows window;

    int num;

    WindowOrder(Apt_windows window, int num)

    {

        this.window = window;

        this.num = num;

    }

    WindowOrder add(WindowOrder ord)

    {

        if (this.window.equals(ord.window))

        {

            this.num += ord.num;

        }

        return this;

    }

    WindowOrder times(int number)

    {

        this.num *= number;

        return this;

    }

    public String toString()

    {

        return String.format("%d %s", this.num, this.window);

    }

    public boolean equvalent(Object that)

    {

        if (this == that)

        {

            return true;

        }

        if (that instanceof WindowOrder)

        {

            WindowOrder wo = (WindowOrder)that;

            return this.window.equals(wo.window) &&

                this.num == wo.num;

        }

        return false;

    }

}

Add a comment
Know the answer?
Add Answer to:
Templates Apartment.java package hwk7; public class Apartment {    int numOfApartments; // the number of apartments...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The current code I have is the following: package uml; public class uml {        public...

    The current code I have is the following: package uml; public class uml {        public static void main(String[] args) {              // TODO Auto-generated method stub        } } class Account { private String accountID; public Account(String accountID) { this.accountID = accountID; } public String getAccountID() { return accountID; } public void setAccountID(String accountID) { this.accountID = accountID; } @Override public String toString() { return "Account [accountID=" + accountID + "]"; } } class SuppliesAccount extends Account { private...

  • Adapt your Rational class : public class Rational { private int num; private int denom; public...

    Adapt your Rational class : public class Rational { private int num; private int denom; public Rational() { num = 0; denom = 1; } public Rational(int num, int denom) { this.num = num; this.denom = denom; } int getNum() { return num; } int getDenom() { return denom; } public Rational add(Rational rhs) { return new Rational(num * rhs.denom + rhs.num * denom, denom * rhs.denom); } public Rational subtract(Rational rhs) { return new Rational(num * rhs.denom - rhs.num...

  • package rectangle; public class Rectangle {    private int height;    private int width;    public...

    package rectangle; public class Rectangle {    private int height;    private int width;    public Rectangle(int aHeight, int aWidth) {    super();    height = aHeight;    width = aWidth;    }    public int getHeight() {    return height;    }    public int getWidth() {    return width;    }    public void setHeight(int aHeight) {    height = aHeight;    }    public void setWidth(int aWidth) {    width = aWidth;    }    public int...

  • For Questions 1-3: consider the following code: public class A { private int number; protected String...

    For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() {    super();    System.out.println(“B() called”);...

  • For Questions 1-3: consider the following code: public class A { private int number; protected String...

    For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() {   super();   System.out.println(“B() called”); } public...

  • Priority Queue Demo import java.util.*; public class PriorityQueueDemo {    public static void main(String[] args) {...

    Priority Queue Demo import java.util.*; public class PriorityQueueDemo {    public static void main(String[] args) {        //TODO 1: Create a priority queue of Strings and assign it to variable queue1               //TODO 2: Add Oklahoma, Indiana, Georgia, Texas to queue1                      System.out.println("Priority queue using Comparable:");        //TODO 3: remove from queue1 all the strings one by one        // with the "smaller" strings (higher priority) ahead of "bigger"...

  • departmentstore: package departmentstorepkg; import java.util.ArrayList; public class DepartmentStore {    private static final int DEFAULT_SIZE =...

    departmentstore: package departmentstorepkg; import java.util.ArrayList; public class DepartmentStore {    private static final int DEFAULT_SIZE = 10; private StaffMember [] myEmployees; private int myNumberEmployees; private String myFileName; private StaffMember[] employee; public DepartmentStore (String filename){ myFileName = filename; myEmployees = employee;    } public String toString(){ return this.getClass().toString() + ": " + myFileName; } public void addEmployee(Employee emp){ } /** * prints out all the employees in the array list held in this class */ public void print(){ for(int i =...

  • The method m() of class B overrides the m() method of class A, true or false?...

    The method m() of class B overrides the m() method of class A, true or false? class A int i; public void maint i) { this.is } } class B extends A{ public void m(Strings) { 1 Select one: True False For the following code, which statement is correct? public class Test { public static void main(String[] args) { Object al = new AC: Object a2 = new Object(); System.out.println(al); System.out.println(a): } } class A intx @Override public String toString()...

  • In Java. Please use the provided code Homework 5 Code: public class Hw05Source { public static...

    In Java. Please use the provided code Homework 5 Code: public class Hw05Source { public static void main(String[] args) { String[] equations ={"Divide 100.0 50.0", "Add 25.0 92.0", "Subtract 225.0 17.0", "Multiply 11.0 3.0"}; CalculateHelper helper= new CalculateHelper(); for (int i = 0;i { helper.process(equations[i]); helper.calculate(); System.out.println(helper); } } } //========================================== public class MathEquation { double leftValue; double rightValue; double result; char opCode='a'; private MathEquation(){ } public MathEquation(char opCode) { this(); this.opCode = opCode; } public MathEquation(char opCode,double leftVal,double rightValue){...

  • What is wrong with this Code? Fix the code errors to run correctly without error. There...

    What is wrong with this Code? Fix the code errors to run correctly without error. There are seven parts for one code, all are small code segments for one question. All the 'pets' need to 'speak', every sheet of code is connected. Four do need need any Debugging, three do have problems that need to be fixed. Does not need Debugging: public class Bird extends Pet { public Bird(String name) { super(name); } public void saySomething(){} } public class Cat...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT