Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here are the instructions: The attached file contains starter code for a class Building that you should use as a driver for the following set

Here are the instructions:

The attached file contains starter code for a class Building that you should use as a driver for the following set of classes.

Write a class Room.

A Room class contains an int instance variable for the area (in square feet) of the room one constructor that takes the area of the room as a parameter an accessor, int getSquareFeet() that returns the area of the room an accessor, int getCapacity() that returns the capacity of the room. The capacity is given by dividing the square feet by 9 (using integer division). an accessor, String toString() that returns the square feet and capacity of the room.

Write a class Classroom that extends Room.

A Classroom class contains an int instance variable for the number of chairs in the classroom a constructor that takes the area of the classroom as a parameter a constructor that takes the area of the classroom and the number of chairs as parameters getter and setter for chairs an override for getCapacity. The capacity of a classroom is the number of chairs. an accessor, String toString() that returns the square feet and capacity of the room as well as the number of chairs.

Write a class Elevator that extends Room.

An Elevator class contains an int instance variable for the current floor of the elevator a constructor that takes the area of the elevator as a parameter a mutator void up(int floors) that increases the current floor by the parameter a mutator void down(int floors) that decreases the current floor by the parameter an accessor String toString() that returns the square feet and capacity of the elevator, as well as its current floor.

Here is my code: I am having some trouble with the output. In the tester, I commented out one of the random number generators. I appreciate any help you can give me.

Room.java

public class Room { //super class private int squareF; // instance variable public Room(int sqF) { // assigns square feet squareF = sqF; }

public int getSquareFeet() { // getter return squareF; }

public int getCapacity() { // getter int capacity; capacity = squareF/9; return capacity; } public String toString() { // returns a string return " Square feet of room is: " + getSquareFeet() + " and the capacity of the room is: " + getCapacity(); } }

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Classroom.java

public class Classroom extends Room{ private int numOfChairs = 0;

public Classroom(int squareF) { // constructor that takes in one parameter super(squareF); } public Classroom(int squareF, int chairs) { // constructor that takes in parameters super(squareF); numOfChairs = chairs; } public int getNumOfChairs() { return numOfChairs; } public void setNumOfChairs(int chairsNumber) { this.numOfChairs = chairsNumber; } public int getCapacity() { System.out.println("Capacity n= " + numOfChairs); return numOfChairs; } public String toString() { return super.toString() + " and the number of chairs are: " + getCapacity(); }

}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Elevator.java

public class Elevator extends Room{

private int floor = 0; // instant variable public Elevator(int squareF) { // constructor super(squareF); } public void up(int floors) { //up method floor += floors; } public void down(int floors) { // down method floor -= floors; } public String toString() { // return toString method return super.toString() + " and the elevator is now at floor " + floor; } }

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Tester - please ignore the ArrayList comments as it has nothing to do with this program.

public class Building {

public static void main(String[] args) { Scanner kybd = new Scanner(System.in);

// declare an ArrayList containing Room elements Random rand = new Random();

System.out.println("Enter \t1: create classroom \t2: create create elevator" + " \t3: exit"); int inp = kybd.nextInt(); while (inp != 3) { if (inp == 1) { System.out.println("How many chairs? "); //int ch = kybd.nextInt(); Room current = new Classroom(4); System.out.println(current.toString()); // add current to the ArrayList } else if (inp == 2) { Elevator current = new Elevator(rand.nextInt(100) + 10); if (rand.nextInt(2) == 0) { //current.up(rand.nextInt(10)); } else { //current.down(rand.nextInt(10)); } System.out.println(current.toString()); // add current to the ArrayList } System.out.println("Enter \t1: create classroom \t2: create create elevator" + " \t3: exit"); inp = kybd.nextInt(); } kybd.close(); // create a for loop to walk through the ArrayList its elements, one per line

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Guide To Client Server Databases

Authors: Joe Salemi

2nd Edition

1562763105, 978-1562763107

More Books

Students also viewed these Databases questions