CSC1110 Homework 8
Complete the following program
Write a program that mimics the operations of the inside of an elevator. More specifically, the program simulates what happens when the user chooses to go to a particular floor, and it simulates what happens when the user pulls the elevator’s fire alarm. Assume the elevator is in a high-rise building that has floors numbered 1 through 100.
Within your Elevator
class, include these methods:
- selectFloor ─ This method takes your Scanner as a parameter and prompts the user for a floor selection, 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.
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 Output
Welcome to John's elevator simulator! Options: (s)elect a floor, (f)ire alarm, (q)uit Enter s, f, or q ==> x Invalid selection. Options: (s)elect a floor, (f)ire alarm, (q)uit Enter s, f, or q ==> F Danger! You must exit the building now! Options: (s)elect a floor, (f)ire alarm, (q)uit Enter 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)uit Enter 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)uit Enter 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)uit Enter 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)uit Enter s, f, or q ==> q
Optional extension: Some hotels cater to superstitious people by omitting the 13th floor. Modify your program so that there is no 13th floor.
Submission Instructions:
Commit and Push your code to GitHub Classroom.