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

Write program that mimics the operations of the inside of an elevator. More spec

ID: 3688778 • Letter: W

Question

Write program that mimics the operations of the inside of an elevator. More specifically, the program

simulates what happens when user chooses to go to a particular floor, and simulates what happens

when the user pulls the fire alarm on the elevator. Assume the elevator is in a high building and the floors are numbered 1 through 100

Within your Elevator class, include these methods:

selectFloor This method prompts the user for a floor selection and then performs input

validation for the floor selection. If the floor selection is inappropriate (less than 1, greater than 100),

then the method prints an error message. If the floor selection is OK, the method simulates going to

that floor. See the sample session below for the format of the simulation message.

fireAlarm This method prints a “danger” message and then simulates going to the first floor.

Note that I’m assuming this is a high tech fire alarm that is programmed to force the elevator to go to

the first floor! See the sample session below for the format of the “danger” message

Within your ElevatorDriver class, use a loop that continues until the user enters “q” for quit.

See the sample session for details.

5

© 2014 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any

manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.

As you implement your solution, you should find that the selectFloor and fireAlarm methods

contain some repeated coding logic. To avoid unnecessary redundancies in your code, have those

methods call an appropriate helper method.

Sample session:

Welcome to John's elevator simulator!

Options: (s)elect a floor, (f)ire alarm, (q)uitEnter s, f, or q ==>

x

Invalid selection.Options: (s)elect a floor, (f)ire alarm,

(q)uitEnter s, f, or q ==> F

Danger! You must exit the building now!Options: (s)elect a floor,

(f)ire alarm, (q)uitEnter s, f, or q ==> s

Enter the floor that you'd like to go to ==> 102

Invalid floor selection - must be between 1 and 100.Options: (s)elect

a floor, (f)ire alarm, (q)uitEnter s, f, or q ==> s

Enter the floor that you'd like to go to ==> 15

Going

up..2..3..4..5..6..7..8..9..10..11..12..13..14..15..Ding!Options:

(s)elect a floor, (f)ire alarm, (q)uitEnter s, f, or q ==> s

Enter the floor that you'd like to go to ==> 10

Going down..14..13..12..11..10..Ding!Options: (s)elect a floor,

(f)ire alarm, (q)uitEnter s, f, or q ==> f

Danger! You must exit the building now!Going

down..9..8..7..6..5..4..3..2..1..Ding!Options: (s)elect a floor,

(f)ire alarm, (q)uitEnter s, f, or q ==> q

Explanation / Answer

Elevator.java

import java.util.Scanner;

public class Elevator {
private int currentFloor;

public Elevator() {

currentFloor = 0;
}
public static void main (String[]args){
Elevator myElevator=new Elevator();
myElevator.selectFloor();
}
public void selectFloor() {

Scanner scnr = new Scanner(System.in);
int newFloor;

System.out.println("Enter the floor you'd like to go to ==> ");
newFloor = scnr.nextInt();
if (newFloor > 100 || newFloor < 0 || newFloor == 13) {
System.out.println("Invalid selection");
}

else { // The if was not necessary
int direction = 0;
if(currentFloor < newFloor){
direction = 1; // going up;
System.out.print("going up");
} else if (currentFloor > newFloor) {
direction = -1; //going down;
System.out.print("going up");
} else {
direction = 0; //going nowhere;
System.out.print("Please choose another floor to the one you are on");
}
for (; currentFloor != newFloor; currentFloor += newFloor)
System.out.println("...");
System.out.println("Ding!");
}
}

public void fireAlarm() {
System.out.println("Danger, you must exit the building now!");
}

}

sample output:

Enter the floor you'd like to go to ==>
7
going up...
Ding!

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