Chris Schuck Chris Schuck - 1 month ago 4
Java Question

How do I configure this Java method connected to several classes that all get more specific?

All of the program's templates. This was a past assignment but at this point, I'm just trying to understand what's going on.

Under the Apartment class, I'm confused on how to correctly return an array of window orders for one unit, all units, and then the @Override method under ThreeBedroom.

Just for reference of what I've done so far (probably not all correct):

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() {
String s = "";
s = width + " x " + height + " window";
return s;
}

// compare window objects by their dimensions
public boolean equals(Object that) {
if (that instanceof Window) {
Window w = (Window) that;
return this.width == w.width && this.height == w.height;
}

else { return false; }
}
}

class WindowOrder {
final Window window; // window description (its width and height)
int num; // number of windows for this order

WindowOrder(Window window, int num) {
this.window = window;
this.num = num;
}

// 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) {
if (order.equals(window)) {
this.num -= num;
return order;
}

else {
return order;
}
}

// update the num field of this object by multiplying it with the parameter
// and then return the current object
WindowOrder times(int number) {
WindowOrder window = new WindowOrder(this.window, this.num);
this.num *= number;

return window;
}

// print text like: 20 4 X 6 window
@Override
public String toString() {
String s = "";
s = num + " " + window.toString();
return s;
}

// Two orders are equal if they contain the same number of windows of the same size.
@Override
public boolean equals(Object that) {
if (that instanceof WindowOrder) {
WindowOrder order = (WindowOrder) that;
return this.num == order.num && this.window == order.window;
}

else { return false; }
}
}

public class Room {
Window window;
int numOfWindows;

Room(Window window, int numOfWindows) {
this.window = window;
this.numOfWindows = numOfWindows;
}

WindowOrder order() {
return new WindowOrder(window, numOfWindows);
}

// Print text like: 5 (6 X 8 window)
@Override
public String toString() {
String s = "";
s = numOfWindows + " (" + window.toString() + ")";
return s;
}

// Two rooms are equal if they contain the same number of windows of the same size
@Override
public boolean equals(Object that) {
if (that instanceof Room) {
Room room = (Room) that;
return this.window == room.window && this.numOfWindows == room.numOfWindows;
}
else { return false; }
}
}

class MasterBedroom extends Room {
MasterBedroom() {
super(new Window(4, 6), 3);
}

// Call parent's toString method
//
// return text like: Master bedroom: 3 (4 X 6 window)
@Override
public String toString() {
String s = "";
s = "Master bedroom: " + numOfWindows + " " + window.toString();
return s;
}
}

class GuestRoom extends Room {
GuestRoom() {
super(new Window(5, 6), 2);
}

// Call parent's toString method
//
// return text like: Guest room: 2 (5 X 6 window)
@Override
public String toString() {
String s = "";
s = "Guest room: " + numOfWindows + " " + window.toString();
return s;
}
}

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() {
String s = "";
s = "Living room: " + numOfWindows + " " + window.toString();
return s;
}
}


For Apartment's orderForOneUnit() method, I wrote this, but it seems to simplistic and I feel like I should be using a for loop..

WindowOrder[] orderForOneUnit() {
WindowOrder[] order = new WindowOrder[rooms.length];
return order;
}


Am I even close to correctly understanding this? What should be under the Apartment methods?

Answer

Didn't looks at the templates but from what you've provided, you're close. All you've done so far is create a WindowOrder[] array of length rooms. You need to add new WindowOrder(desc, num) to these arrays before return order;

/**
 * All apartment rooms have the same number of windows, with the
 * same size window for each of those.
 */
public class Apartment
{
    private int numRooms_;
    private int windowsPerRoom_;
    private Window window_;

    /**
     * Constructor
     */
    public Apartment(numRooms, windowsPerRoom, desiredWindowHeight, desiredWindowLength)
    {
        numRooms_ = numRooms;
        windowsPerRoom_ = windowsPerRoom;
        window_ = new Window(desiredWindowHeight, desiredWindowLenght);
    }

    /** 
     * Orders for one room in apartment
     */
    public WindowOrder orderForOneUnit() 
    {
        WindowOrder order = new WindowOrder(window_, 1);

        return order;
    }

    /** 
     * Orders for all rooms in apartment
     */
    public List<WindowOrder> orderForAllUnits() 
    {
        List<WindowOrder> orders = new ArrayList<WindowOrder>();

        WindowOrder order;
        for(i=0; i<numRooms_; i++)
        {
            orders.add(new WindowOrder(window_, windowsPerRoom_);
        }
        return orders;
    }
}

Now when you're in your code and you're ready for a new Apartment(x, x, x, x) you can do the following (I'll assume you're just in main())

public class ApartmentComplex
{
    public static void main(String[] args)
    {
        int numWindowsPerRoom = 3;
        int desiredWindowHeight = 10;
        int desiredWindowWidth = 10;

        int numRooms = 5;
        Apartment aptWithFiveRooms = new Apartment(numRooms, numWindowsPerRoom, desiredWindowHeight, desiredWindowWidth);

        WindowOrder singleSingleOrder = apt.orderForOneUnit();
        List<WindowOrder> allRoomsOrder = apt.orderForAllUnits();

        numRooms = 3;
        Apartment aptWithThreeRooms = new Apartment(numRooms, numWindowsPerRoom, desiredWindowHeight, desiredWindowWidth);
        List<WindowOrder> threeRoomsOrder = apt.orderForAllUnits();
    }
}
Comments