Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The below starter code for a class Building that you should use as a driver for

ID: 3814945 • Letter: T

Question

The below 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.

-----------Starter code---------------

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

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 1: create classroom 2: create create elevator" + " 3: 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(rand.nextInt(1000) + 100, ch);
               // 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));
               }
               // add current to the ArrayList
           }
           System.out.println("Enter 1: create classroom 2: create create elevator" + " 3: exit");
           inp = kybd.nextInt();
       }
       kybd.close();
       // create a for loop to walk through the ArrayList its elements, one per line

   }

}

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

Explanation / Answer

Room.java

public class Room {

   // Declaring instance variables

   private int area;

   // Parameterized constructor

   public Room(int area) {
       super();
       this.area = area;
   }

   // getters and setters
   public int getSquareFeet() {
       return area;
   }

   public int getCapacity() {
       return area / 9;
   }

   // toString method is used to display the contents of an object inside it
   @Override
   public String toString() {
       return "Area of the room is " + area + " with capacity ="
               + getCapacity();
   }

}

_____________

Classroom.java

public class Classroom extends Room{
   //Declaring instance variables

   private int noOfChairs;


   //Parameterized constructor


   public Classroom(int area) {
      
       super(area);
   }
   //Parameterized constructor
   public Classroom(int area, int noOfChairs) {
       super(area);
       this.noOfChairs = noOfChairs;
   }
   //getters
   public int getNoOfChairs() {
       return noOfChairs;
   }

   public void setNoOfChairs(int noOfChairs) {
       this.noOfChairs = noOfChairs;
   }
  
   @Override
   public int getCapacity() {
       return noOfChairs;
   }

   //toString method is used to display the contents of an object inside it
   @Override
   public String toString() {
       return "Classroom whose area is :"+getSquareFeet()+" and capacity of room :"+getCapacity()+" having no of chairs :"+noOfChairs;
   }

}

_____________________

Elevator.java

public class Elevator extends Room {
   private int currentFlor;
   public Elevator(int area)
   {
       super(area);
   }
  
   public void up(int floors)
   {
       this.currentFlor=currentFlor+floors;
   }
   public void down(int floors)
   {
       this.currentFlor=currentFlor-floors;
   }

   @Override
   public String toString() {
       return "The Area of the Elevator is "+getSquareFeet()+" whose capacity is "+getCapacity()+" and its current floor is "+currentFlor;
   }
  

}

_____________________

Building.java

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class Building {

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

       // declare an ArrayList containing Room elements
       ArrayList<Room> arl = new ArrayList<Room>();

       Random rand = new Random();

       System.out.println("Enter 1: create classroom 2: create elevator"+ " 3: 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(rand.nextInt(1000) + 100, ch);

               // add current to the ArrayList
               arl.add(current);

           } 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));
               }
               // add current to the ArrayList
               arl.add(current);
           }
           System.out.println("Enter 1: create classroom 2: create create elevator"+ " 3: exit");
           inp = kybd.nextInt();
       }
       kybd.close();
       // create a for loop to walk through the ArrayList its elements, one per
       // line

       for (Room rm : arl) {
           System.out.println(rm.toString());
       }

   }

}

__________________

Output:

Enter
   1: create classroom
   2: create elevator
   3: exit
1
How many chairs?
100
Enter
   1: create classroom
   2: create create elevator
   3: exit
2
Enter
   1: create classroom
   2: create create elevator
   3: exit
3
Classroom whose area is :471 and capacity of room :100 having no of chairs :100
The Area of the Elevator is 83 whose capacity is 9 and its current floor is 1

______________Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote