Question

In: Computer Science

Elevator (C++) Following the diagram shown below, create the class Elevator. An Elevator represents a moveable...

Elevator (C++)

Following the diagram shown below, create the class Elevator. An Elevator represents a moveable carriage that lifts passengers between floors. As an elevator operates, its sequence of operations are to open its doors, let off passengers, accept new passengers, handle a floor request, close its doors and move to another floor where this sequence repeats over and over while there are people onboard. A sample driver for this class is shown below. Each elevator request translates into just a single line of output shown below. You should probably make a more thorough driver to test your class better.

Elevator1  Elevator2

Elevator

Elevator( );
void openDoors( );
void closeDoors( );
void letOffPassengers( int amount );
void acceptPassengers( int amount );
void requestFloor( int floor );
bool isOnFloor( int floor );
int getFloor( );
int getPassengers( );

int my_Floor;
int my_NumberOfPassengers;
bool my_DoorsOpen;

Sample Driver Code

Elevator e;
e.openDoors();
e.acceptPassengers( 5 );
e.requestFloor( 3 );
e.closeDoors();
e.openDoors();
e.letOffPassengers( 1 );
e.acceptPassengers( 2 );
e.requestFloor( 4 );
e.closeDoors();
e.openDoors();
e.letOffPassengers( 3 );

Sample Driver Output

Elevator on Floor 1 with 0 passengers
Elevator Door's Open
Elevator has 5 passengers
Passengers want floor 3
Elevator moving to floor 3
Elevator Door's Open
Elevator has 4 passengers
Elevator has 6 passengers
Passengers want floor 4
Elevator moving to floor 4
Elevator Door's Open
Elevator has 3 passengers

Solutions

Expert Solution

Elevator.cpp ------

#include<iostream>

using namespace std;

class Elevator
{
   private:
       int my_Floor;
       int my_NumberOfPassengers;
       bool my_DoorsOpen;
   public:
       Elevator();//constructor
       void openDoors();//this function is used to open the door of the elevator
       void closeDoors();//this function is used to close the door of the elevator
       void letOffPassengers(int);//this function is used to let off passengers form the lift
       void requestFloor(int);//this function is used to go to a floor
       bool isOnFloor(int);//this function checks whether the lift is in specified floor
                    //and if it is in the floor then it returns true else returns false
       int getFloor();//this function returns the current floor number
       int getPassengers();//this function returns total number of passengers currently is in the elevator
       void acceptPassengers(int);//accepts passengers in the elevator
};

void Elevator::requestFloor(int floor)
{
   if(floor<0)
   {
       cout<<"Invalid Floor\n";
       return;
   }
   cout<<"Passengers want floor "<<floor<<endl<<"Elevator moving to floor "<<floor<<endl;
}

void Elevator::letOffPassengers(int amount)
{
   my_NumberOfPassengers-=amount;
   cout<<"Elevator has "<<my_NumberOfPassengers<<" passengers\n";
}

void Elevator::acceptPassengers(int amount)
{
   my_NumberOfPassengers+=amount;
   cout<<"Elevator has "<<my_NumberOfPassengers<<" passengers\n";
}

void Elevator::closeDoors()
{
   my_DoorsOpen=false;
}

void Elevator::openDoors()
{
   cout<<"Elevator Door's Open\n";
   my_DoorsOpen=true;
}

Elevator::Elevator()
{
   cout<<"\nElevator on Floor 1 with 0 passengers\n";
   my_Floor=1;//initally the elevator is in the floor 1
   my_NumberOfPassengers=0;//initially the elevator is empty
   my_DoorsOpen=false;//initiall the door of the elevator is closed
}

int Elevator::getFloor()
{
   return my_Floor;
}

int Elevator::getPassengers()
{
   return my_NumberOfPassengers;
}

bool Elevator::isOnFloor(int floor)
{
   if(floor==my_Floor)
       return true;
   else
       return false;
}

int main()
{
   Elevator e;
   e.openDoors();
   e.acceptPassengers( 5 );
   e.requestFloor( 3 );
   e.closeDoors();
   e.openDoors();
   e.letOffPassengers( 1 );
   e.acceptPassengers( 2 );
   e.requestFloor( 4 );
   e.closeDoors();
   e.openDoors();
   e.letOffPassengers( 3 );
   return 0;
}


Related Solutions

Write a C program to create a series of processes, as shown below in the diagram....
Write a C program to create a series of processes, as shown below in the diagram. P |------ ---- > c1 |------------>c2                              |------- ------>c3 ---------->c4                             In other words, the parent process p creates process c1, c1 creates c2 and c3, and c3 creates c4. A sample run of the program shows Your program should output something similar to what is shown above. You could optionally use wait/waitpid/sleep/exit in your program. Comment your code to show where you created...
In C code Consider the finite state machine shown below that controls a two story elevator....
In C code Consider the finite state machine shown below that controls a two story elevator. The elevator has a toggle button that can be in the UP position or the DOWN position, and an LED light that can be RED or GREEN . When the elevator is on the GROUND floor the light is RED, and when the elevator is on the FIRST floor the light is GREEN. Assume the existence of function enum Button getButton(); that returns a...
In C++ Create a class called Rational (separate the files as shown in the chapter) for...
In C++ Create a class called Rational (separate the files as shown in the chapter) for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private data of the class-the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form. For example,...
Consider the finite state machine shown below that controls a two story elevator. The elevator has...
Consider the finite state machine shown below that controls a two story elevator. The elevator has a toggle button that can be in the UP position or the DOWN position, and an LED light that can be RED or GREEN . When the elevator is on the GROUND floor the light is RED, and when the elevator is on the FIRST floor the light is GREEN. Assume the existence of function enum Button getButton(); that returns a value of DOWN...
Four displacement vectors, A, B, C, and D, are shown in the diagram below. Their magnitudes...
Four displacement vectors, A, B, C, and D, are shown in the diagram below. Their magnitudes are:  A = 16.2 m, B = 11.0 m, C = 12.0 m, and D =  24.0 m What is the magnitude, in meters, and direction, in degrees, of the resultant vector sum of A, B, C, and D? Give the direction as an angle measured counterclockwise from the +x direction.
Complete the C++ class Triple below so that it represents a vector with 3 elements: (a,...
Complete the C++ class Triple below so that it represents a vector with 3 elements: (a, b, c) Most of these function bodies can be written in only a few lines. Error checking is not required. Test your class using the test.h file. The triple.cpp file is the code unit tested on submission. main.cpp #include #include #include #include "triple.h" #include "test.h" using namespace std; int main() { myTest(); return 0; } triple.h #ifndef TRIPLE_H #define TRIPLE_H #include #include using namespace...
using C++ Please create the class named BankAccount with the following properties below: // The BankAccount...
using C++ Please create the class named BankAccount with the following properties below: // The BankAccount class sets up a clients account (using name & id), and - keeps track of a user's available balance. - how many transactions (deposits and/or withdrawals) are made. public class BankAccount { private int id; private String name private double balance; private int numTransactions; // ** Please define method definitions for Accessors and Mutators functions below for the class private member variables string getName();...
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int...
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int maxRevenue(int[] billboard, int[] revenue, int distance, int milesRes) { int[] MR = new int[distance + 1]; //Next billboard which can be used will start from index 0 in billboard[] int nextBillBoard = 0; //example if milesRes = 5 miles then any 2 bill boards has to be more than //5 miles away so actually we can put at 6th mile so we can add...
********************C# C# C#******************** Part A: Create a project with a Program class and write the following...
********************C# C# C#******************** Part A: Create a project with a Program class and write the following two methods(headers provided) as described below: 1. A Method, public static int InputValue(int min, int max), to input an integer number that is between (inclusive) the range of a lower bound and an upper bound. The method should accept the lower bound and the upper bound as two parameters and allow users to re-enter the number if the number is not in the range...
create class diagram of the following case. Case: Once a month, the person in charge of...
create class diagram of the following case. Case: Once a month, the person in charge of the human resources area of ​​the organization prepares the payroll to be paid. Once the calculation is made, the manager accesses and authorizes the payment of the information generated for payment, which is updated in the database to be sent to the Bank. The person in charge of the payroll, in turn, is in charge of making the entry and / or withdrawal of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT