<< Chapter < Page Chapter >> Page >

Focus on problem solving

Problem: Constructing an Elevator Object

In this application, you are required to simulate the operation of an elevator. Output is required that describes the current floor on which the elevator is stationed or is passing. Additionally, an internal elevator button that is pushed as a request to move to another floor should be provided. The elevator can travel between the first and fifteenth floors of the building in which it is situated.

Analyze the problem

For this application, we have one object, an elevator. The only attribute of interest is its location. A rider can request a change in the elevator’s position (state). Additionally, we must be able to establish the initial floor position when a new elevator is put in service. Figure below illustrates an object diagram that includes both the required attributes and operations.

Object diagram

Figure 7.3 An Elevator Class Diagram

Develop a solution

For this application, the location of the elevator, which corresponds to its current floor position, can be represented by an integer member variable whose value ranges between 1 and 15. The variable is named currentFloor. The value of currentFloor effectively represents the current state of the elevator. The services that we provide for changing the state of the elevator are an initialization function to set the initial floor position when a new elevator is put in service and a request function to change the elevator’s position (state) to a new floor. Putting an elevator in service is accomplished by declaring a single class instance and requesting a new floor position is equivalent to pushing an elevator button.

The response to the elevator button should be as follows:

If a request is made for either a nonexistent floor or the current floor,

Do nothing

Else if the request is for a floor above the current floor,

Display the current floor number

While not at the designated floor

Increment the floor number

Display the new floor number

End while

Display the ending floor number

Else

Display the current floor number

While not at the designated floor

Decrement the floor number

Display the new floor number

End while

Display the ending floor number

Endif

Coding the solution

From the design, a suitable class declaration is:

//class declaration

class Elevator

{

private:

int currentFloor;

public:

Elevator(int = 1); //constructor

void request(int);

};

The two declared public member functions, Elevator() and request(), are used to define the external services provided by each Elevator object. The Elevator() function becomes a constructor function that is automatically called when an object of type Elevator is created. We use this function to initialize the starting floor position of the elevator. The request() function is used to alter its position. To accomplish these services, a suitable class implementation section is:

// class implementation section

Elevator::Elevator(int cfloor)

{

currentFloor = cfloor;

}

void Elevator::request(int newfloor)

{

if (newfloor<1 || newfloor>MAXFLOOR || newfloor == currentFloor)

Questions & Answers

the diagram of the digestive system
Assiatu Reply
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
what is cell divisoin?
Aron Reply
Diversity of living thing
ISCONT
what is cell division
Aron Reply
Cell division is the process by which a single cell divides into two or more daughter cells. It is a fundamental process in all living organisms and is essential for growth, development, and reproduction. Cell division can occur through either mitosis or meiosis.
AI-Robot
What is life?
Allison Reply
life is defined as any system capable of performing functions such as eating, metabolizing,excreting,breathing,moving,Growing,reproducing,and responding to external stimuli.
Mohamed
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Programming fundamentals in c++. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10788/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Programming fundamentals in c++' conversation and receive update notifications?

Ask