Java Reference
In-Depth Information
Figure 5.6
Output of the ThisDemo program.
Lab 5.1: Simulating an Elevator
In this lab, you will write an Elevator class containing various methods.
Then, you will write a program that creates Elevator objects and invokes
the various methods. The purpose of the lab is to help you understand
how to write methods and how methods manipulate the data of an object.
1. Using your text editor, write a class named Elevator. Add fields for
the following attributes: an int for the current floor, an int for the
floor that the elevator is heading to, a boolean to denote whether the
elevator is going up or down once it reaches its destination, and a
boolean to denote whether the elevator doors are open or closed.
2. Add a method named goToFloor() that changes the floor that the
elevator is heading to. Use the System.out.println() method to dis-
play a message that you are changing the value.
3. Add methods named openDoors() and closeDoors() that change the
appropriate boolean field accordingly. Again, display a message
within each method so that you can see when the methods are
invoked.
4. Add methods named goingUp() and goingDown() that change the
appropriate boolean field accordingly.
5. Save and compile your Elevator class.
6. Write a class named ElevatorProgram that contains main().
7.
Within main(), instantiate two Elevator objects. Invoke the various
methods of the Elevator class on these two objects, ensuring that all
your Elevator methods work successfully.
Search WWH ::




Custom Search