36.5k views
3 votes
Assume the existence of a Building class. Define a derived class, ApartmentBuilding that contains four (4) data members: an integer named numFloors, an integer named unitsPerFloor, a boolean named hasElevator, and a boolean named hasCentralAir. There is a constructor containing parameters for the initialization of the above variables (in the same order as they appear above). There are also two function: the first, getTotalUnits, accepts no parameters and returns the total number of units in the building; the second, isLuxuryBuilding accepts no parameters and returns true if the building has central air, an elevator and 2 or less units per floor.

User Pharalia
by
2.6k points

1 Answer

4 votes

Answer:

Step-by-step explanation:

The following code is written in Java. The class ApartmentBuilding extends the assumed class of Building but does not call any of its methods since we do not have access to it. The ApartmentBuilding class contains all of the variables listed, as well as constructor and methods, including getter and setter methods for each variable. A test case for the class is provided in the image below with it's output.

class ApartmentBuilding extends Building {

int numFloors, unitsPerFloor;

boolean hasElevator, hasCentralAir;

public ApartmentBuilding(int numFloors, int unitsPerFloor, boolean hasElevator, boolean hasCentralAir) {

this.numFloors = numFloors;

this.unitsPerFloor = unitsPerFloor;

this.hasElevator = hasElevator;

this.hasCentralAir = hasCentralAir;

}

public int getTotalUnits() {

int total = this.numFloors * this.unitsPerFloor;

return total;

}

public boolean isLuxuryBuilding() {

if ((this.hasCentralAir == true) && (this.hasElevator == true) && (this.unitsPerFloor <= 2)) {

return true;

} else {

return false;

}

}

public int getNumFloors() {

return numFloors;

}

public void setNumFloors(int numFloors) {

this.numFloors = numFloors;

}

public int getUnitsPerFloor() {

return unitsPerFloor;

}

public void setUnitsPerFloor(int unitsPerFloor) {

this.unitsPerFloor = unitsPerFloor;

}

public boolean isHasElevator() {

return hasElevator;

}

public void setHasElevator(boolean hasElevator) {

this.hasElevator = hasElevator;

}

public boolean isHasCentralAir() {

return hasCentralAir;

}

public void setHasCentralAir(boolean hasCentralAir) {

this.hasCentralAir = hasCentralAir;

}

}

Assume the existence of a Building class. Define a derived class, ApartmentBuilding-example-1
User Jeff Fritz
by
3.2k points