Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Overview For this assignment, implement and use the methods for a class called Elevator. Elevator class This class will simulate very basic operations of an

Overview

For this assignment, implement and use the methods for a class called Elevator.

Elevator class

This class will simulate very basic operations of an elevator.

Data Members

The data members for the class are:

an integer that holds the current floor where the elevator is located

an integer symbolic constant that holds the number of the bottom floor in a building. Use a value of 1.

an integer symbolic constant that holds the number of the top floor in a building. Use a value of 15.

The symbolic constants need to be handled in a slightly different manner in this program than in previous programs. The issue with this program is that the symbolic constant is a member of the class, but a class definition does not take up any memory since it is just a pattern for what the class should contain. This means that there is no memory to store the values 1 and 15. To solve this problem:

static const int TOP_FLOOR;

const int Elevator::TOP_FLOOR = 15;

Note the inclusion of "Elevator::" in front of the constant name. This lets the compiler know that the constant belongs to the Elevator class.

Define the symbolic constants with the keywords "static const" as part of the definition. For example:

Initialize the constants value outside of the class definition.

Constructors

This class has two constructors. The default constructor (the one that takes no arguments) should initialize the current floor to the bottom floor of the building.

The other constructor for the class should initialize the data member using the passed in argument. It takes 1 argument: an integer that holds the floor where the elevator is located. If the argument is invalid (ie. it's value is less than the bottom floor value or greater than the top floor value, then the current floor data member should be initialized to the bottom floor. Otherwise, initialize the current floor data member using the passed in argument.

Methods

void request( int newFloor )

This is a public method that simulates the movement of the elevator to a new floor. It takes 1 argument: an integer that represents the floor number that the elevator should be moved to. It returns nothing.

If the passed in argument is invalid, display an error message that the floor number is invalid and do not move the elevator. If the elevator should move up, call the goUp method that is described below. If the elevator should move down, call the goDown method that is described below. If the elevator is already on the requested floor, display a message to the user and do not move the elevator.

int getCurrentFloor()

This is a public method that returns the current floor where the elevator is located. It takes no argument. It returns an integer, which is the current floor data member.

void goDown( int lowerFloor )

This is a private method that simulates the movement of the elevator to a lower floor. It takes 1 argument: an integer that represents the new lower floor number that the elevator should be moved to. It returns nothing.

Start by displaying the current floor where the elevator is located. After that, display a message for each floor that the elevator passes as it's "moving" to the desired lower floor. Once the elevator "arrives" at the desired lower floor, display a message to reflect that. For example, if the elevator is currently on floor #10 and the requested floor is #3, then the output should resemble:

Starting at floor #10 Going down -- now at floor #9 Going down -- now at floor #8 Going down -- now at floor #7 Going down -- now at floor #6 Going down -- now at floor #5 Going down -- now at floor #4 Welcome to floor #3 

void goUp( int higherFloor )

This is a private method that simulates the movement of the elevator to a higher floor. It takes 1 argument: an integer that represents the new higher floor number that the elevator should be moved to. It returns nothing.

Start by displaying the current floor where the elevator is located. After that, display a message for each floor that the elevator passes as it's "moving" to the desired higher floor. Once the elevator "arrives" at the desired higher floor, display a message to reflect that. For example, if the elevator is currently on floor #7 and the requested floor is #11, then the output should resemble:

Starting at floor #7 Going up -- now at floor #8 Going up -- now at floor #9 Going up -- now at floor #10 Welcome to floor #11 

main()

In main(), create 2 Elevator objects. They should contain the values:

The first Elevator object should be created using the default constructor (the one that doesn't take any arguments)

The second Elevator object should be created so that the elevator starts on floor number 6.

The rest of main() will include using the various methods on each of the Elevator objects. Display a label similar to "The first Elevator object" before anything is outputted for each of the objects.

For the first elevator, move to the 10th floor, move to the 5th floor, try to move to the 5th floor again, move to the 15th floor, and finally, move to the 1st floor.

For the second elevator, move to the 1st floor, move to the 13th floor, try to move to the -4th floor, and finally, display the current floor where the elevator is located.

Programming Notes

Each method must have a documentation box like a function.

Hand in a copy of your source code using Blackboard.

Output

The first Elevator object Starting at floor #1 Going up -- now at floor #2 Going up -- now at floor #3 Going up -- now at floor #4 Going up -- now at floor #5 Going up -- now at floor #6 Going up -- now at floor #7 Going up -- now at floor #8 Going up -- now at floor #9 Welcome to floor #10 Starting at floor #10 Going down -- now at floor #9 Going down -- now at floor #8 Going down -- now at floor #7 Going down -- now at floor #6 Welcome to floor #5 You're already on the floor Starting at floor #5 Going up -- now at floor #6 Going up -- now at floor #7 Going up -- now at floor #8 Going up -- now at floor #9 Going up -- now at floor #10 Going up -- now at floor #11 Going up -- now at floor #12 Going up -- now at floor #13 Going up -- now at floor #14 Welcome to floor #15 Starting at floor #15 Going down -- now at floor #14 Going down -- now at floor #13 Going down -- now at floor #12 Going down -- now at floor #11 Going down -- now at floor #10 Going down -- now at floor #9 Going down -- now at floor #8 Going down -- now at floor #7 Going down -- now at floor #6 Going down -- now at floor #5 Going down -- now at floor #4 Going down -- now at floor #3 Going down -- now at floor #2 Welcome to floor #1 The second Elevator object Starting at floor #6 Going down -- now at floor #5 Going down -- now at floor #4 Going down -- now at floor #3 Going down -- now at floor #2 Welcome to floor #1 Starting at floor #1 Going up -- now at floor #2 Going up -- now at floor #3 Going up -- now at floor #4 Going up -- now at floor #5 Going up -- now at floor #6 Going up -- now at floor #7 Going up -- now at floor #8 Going up -- now at floor #9 Going up -- now at floor #10 Going up -- now at floor #11 Going up -- now at floor #12 Welcome to floor #13 Error: invalid floor number The elevator is currently on floor 13

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions