Unformatted text preview: 15100 RECITATION 7  FALL 2007
Introduction A very simple elevator starts at a given floor of a 20story building with no basement. The maximum capacity of the elevator is 15 people. The elevator starts with no people on board. In the elevator, there is an UP button and a DOWN button. If the UP button is pushed, the elevator immediately goes up one floor. However, if the UP button is pushed and the elevator is already at the top floor, it stays there. If the DOWN button is pushed, the elevator immediately goes down one floor. However, if the DOWN button is pushed and the elevator is already at the bottom (first) floor, it stays there. At each floor, we have the option to add passengers to the elevator or remove passengers from the elevator. If we add passengers and there is not enough room for all of the passengers we're adding, then we add only an amount that will bring the elevator to maximum capacity (the others are left to wait). If we remove passengers and the amount we want to remove is more than we have in the elevator, we will remove all remaining passengers on the elevator. In this recitation, you will define the various parts of a class named Elevator to simulate this object. Exercises Download the zip file you need for this recitation from the course website. Move this to your workspace and open it in Eclipse. There is a ElevatorSimulator class that has a main method that will create an elevator and then present the user with a menu. Each menu option corresponds to an action we can perform on the elevator. You must complete the Elevator class to model an elevator based on the introduction above. 1. Perform the following to start the Elevator class: • • Read the variable definitions for the fields that are necessary to represent a simple elevator along with their data types. Read through the accessor methods that are provided to see how they work. Complete the constructor for Elevator that creates an elevator that is initially empty and starts at the floor given by an int parameter. If the given floor is invalid, start the elevator at the 1st floor by default. Run the program several times to see if your elevator is created correctly. Make sure you try to initialize the elevator to a floor that is invalid to see if the constructor deals with this problem correctly. THE OTHER MENU CHOICES WILL NOT WORK YET. • 2. Add the following mutators to the Elevator class: • • • Complete the up method that moves the elevator up one floor, if possible. This method requires no parameters and does not return anything. (Why?) Complete the down method that moves the elevator down one floor, if possible. This method requires no parameters and does not return anything. (Why?) Run the program several times to see if your elevator moves up and down correctly. Make sure you try to move above the top floor and below the bottom floor to see if your methods work correctly. THE OTHER MENU CHOICES WILL NOT WORK YET. 3. Add the following additional mutators to the Elevator class: • Complete the addPassengers method that adds passengers to this elevator. The number of passengers to be added is given by an int parameter. You may assume that the parameter's value is positive. If the number of passengers to be added causes this elevator to go above maximum capacity, the elevator should add only enough passengers to reach maximum capacity. This method does not return anything. Complete the removePassengers method that removes passengers from this elevator. The number of passengers to be removed is given by an int parameter. You may assume that the parameter's value is positive. If the number of passengers to be removed causes this elevator to go below 0 passengers, the elevator should reduce the number of passengers to 0. This method does not return anything. Run the program several times to see if you can add and remove passengers from the elevator correctly. Make sure you try to add more passengers than the elevator can handle and try to remove more passengers than the elevator has inside to see if your methods work correctly. Finally, run the program to see if all operations work correctly in any order. • • Additional Exercises (time permitting) 1. Read through the main method to learn how to create a menu system for this elevator simulator. 2. Add one more mutator of your choice to the Elevator and modify the elevator simulator so the user can select your new behavior. When You're Done... Create a zip file of the project folder for this recitation and submit this zip file on Blackboard to the Recitation area. DO NOT ERASE YOUR RECITATION PROJECT FOLDER IN CASE WE NEED TO SEE IT LATER (IF THE SUBMISSION IS DONE INCORRECTLY). ...
View
Full
Document
This note was uploaded on 01/25/2010 for the course CS 15100 taught by Professor Tom during the Fall '07 term at Carnegie Mellon.
 Fall '07
 TOM

Click to edit the document details