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;
}
}
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;
}
}
Templates Apartment.java package hwk7; public class Apartment { int numOfApartments; // the number of apartments...
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 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 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 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 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) { //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 = 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? 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 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 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...