Rooms and More Can you please help me the JAVA program LabIn
Rooms and More
Can you please help me the JAVA program?
LabInheritStarter.zip file:
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 \ \\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(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 \ \\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
}
}
=================================================
I already asked someone here, but he hasn\'t completed the program. You can take a look of his code here.
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 arrayList = new ArrayList();
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(rand.nextInt(1000) + 100, ch);
arrayList.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));
}
arrayList.add(current);
}
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
for(int i=0;i System.out.println(arrayList.get(i));
}
}
}
class Room {
private int area;
Room(){}
Room(int area){
this.area = area;
}
public int getSquareFeet(){
return area;
}
public int getCapacity(){
return this.getSquareFeet()/9;
}
public String toString(){
return this.getSquareFeet() + \" \" + this.getCapacity();
}
}
class Classroom extends Room{
private int noOfChairs;
private int area;
Classroom(int area){
this.area = area;
}
Classroom(int area,int noOfChairs){
this.area = area;
this.noOfChairs = noOfChairs;
}
public int getChairs(){
return this.noOfChairs;
}
public void setChiars(int chairsNumber){
this.noOfChairs = chairsNumber;
}
public int getCapacity(){
return getChairs();
}
public String toString(){
return getSquareFeet()+\" \"+getCapacity()+\" \"+getChairs();
}
}
class Elevator extends Room{
private int currentFloor;
private int area;
Elevator(int area){
this.area = area;
}
public void up(int floors){
currentFloor += floors;
}
public void down(int floors){
currentFloor -= floors;
}
public int getCurrentFloor(){
return currentFloor;
}
public String toString(){
return getSquareFeet()+\" \"+getCapacity()+\" \"+getCurrentFloor();
}
}
Solution
PROGRAM CODE:
Building.java
package samplepro;
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> rooms = new ArrayList<Room>();
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(rand.nextInt(1000) + 100, ch);
rooms.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));
}
rooms.add(current);
}
System.out.println(\"Enter \ \\t1: create classroom\ \\t2: create elevator\" + \"\ \\t3: exit\");
inp = kybd.nextInt();
}
kybd.close();
for(int i=0; i<rooms.size(); i++)
{
System.out.println((i+ 1)+ \". \" + rooms.get(i));
}
}
}
Room.java
package samplepro;
class Room {
private int area;
Room(){}
Room(int area){
this.area = area;
}
public int getSquareFeet(){
return area;
}
public int getCapacity(){
return this.getSquareFeet()/9;
}
public String toString(){
return \"Square feet: \" + this.getSquareFeet() + \" Capacity: \" + this.getCapacity();
}
}
Elevator.java
package samplepro;
class Elevator extends Room{
private int currentFloor;
Elevator(int area){
super(area);
}
public void up(int floors){
currentFloor += floors;
}
public void down(int floors){
currentFloor -= floors;
}
public int getCurrentFloor(){
return currentFloor;
}
public String toString(){
return \"Square feet: \" + getSquareFeet()+\" Capacity: \"+getCapacity()+\" Current floor: \"+getCurrentFloor();
}
}
Classroom.java
package samplepro;
class Classroom extends Room{
private int noOfChairs;
Classroom(int area){
super(area);
}
Classroom(int area,int noOfChairs){
super(area);
this.noOfChairs = noOfChairs;
}
public int getChairs(){
return this.noOfChairs;
}
public void setChiars(int chairsNumber){
this.noOfChairs = chairsNumber;
}
public int getCapacity(){
return getChairs();
}
public String toString(){
return \"Square feet: \" + getSquareFeet()+\" Capacity: \"+getCapacity()+\" Number of chairs: \"+getChairs();
}
}
OUTPUT:
Enter
1: create classroom
2: create create elevator
3: exit
1
How many chairs?
4
Enter
1: create classroom
2: create elevator
3: exit
2
Enter
1: create classroom
2: create elevator
3: exit
3
1. Square feet: 619 Capacity: 4 Number of chairs: 4
2. Square feet: 31 Capacity: 3 Current floor: -1
// the floor is set from Building class. which is a driver program so I did not change that.




