Goal: The purpose of this assignment is to write a Java program that models an elevator, where the elevator
itself is a stack of people on the elevator and people wait in queues on each
oor to get on the elevator.
Scenario: A hospital in a block of old buildings has a nearly-antique elevator that is at one end of a narrow
hallway on each
oor. The elevator is so narrow that the people in it have to stand in one column. There is
a strict limit on the number of passengers because of its narrowness.
Patients and their families complained about the narrowness of the elevator causing claustrophobia, In
response to the complaints, management has restricted the use of the elevator to employees only, while
encouraging employees to stay healthy by taking the stairs where possible.
To minimize con
ict getting on and o the narrow elevator in the narrow hallway, management had two
lines drawn on the
oor to divide the hallway into three columns leading to and from the elevator:
The rst column is for employees waiting to go up on the elevator;
The second column is for employees waiting to go down; and
The third column is for employees who have exited the elevator to traverse the hall and go to their
destinations.
To maximize use of the elevator, everyone behaves as follows:
At any
oor, people waiting in a queue to get on the elevators are polite: people do not try to get on
the elevator until all people on the elevator who want to get o have exited the elevator.
People only get on the elevator if the elevator is going in their desired direction. For example, when
the elevator is going up, only people who are going up get on and people waiting to go down do not
get on the elevator at that time.
Elevator behaviour: The time it takes the elevator to go from one
oor to the next is equal to the time the
elevator takes to open its door and hold it open for a while. In eect, time is divided into equal time units
during which the elevator is either opening its doors for people to exit and enter or the elevator is moving to
2
the next
oor in whichever direction it is currently moving or it is doing nothing if nobody is using or wants
to use the elevator at the moment.
To decide which action to take, the elevator does the following at the start of each time unit:
First, it decides whether to change directions or not. It will change direction if:
{ It cannot go any further in its current direction, | that is, if it is at the top
oor when it is going
up or it is at the bottom
oor going down.
{ It will not be helping anyone if it continues in this direction, but it will be helping someone if it
changes direction | that is, nobody is on the elevator, nobody is waiting for the elevator on
oors
in the current direction, and somebody is waiting for the elevator on
oors in the other direction.
Second, it decides if it will open its doors or move to the next
oor or do nothing:
{ The elevator will use this time unit to open and hold open its doors if anybody on the elevator
wants to get o on the current
oor, or if anybody is waiting on the current
oor to go in the
current direction and there is still some room on the elevator for another person.
{ The elevator will use this time unit to go to the next
oor in the current direction if there's
anybody on the elevator or if somebody is waiting on a
oor that is in the current direction |
that is, somebody is waiting on a
oor above if the elevator is going up or waiting on a
oor below
if the elevator is going down.
{ If neither of the above two conditions hold, the elevator will simply stay where it is doing nothing
| that is, it just waits for the next time unit to try again.
The options are listed in priority order: opening the door has higher priority than moving to the next
oor, which has higher priority than doing nothing. So, for example, it will open its door if somebody
wants to get o, even if somebody else on the elevator wants to keep going in the current direction.
Program overview: The program must prompt for and read in an input le, which contains set-up
information about the elevator, followed by many lines giving information about employees arriving to use
the elevator; see details about the input le below.
The program must create an elevator simulation using the elevator set-up information from the le; see
details about the Elevator class you will create and how to model the elevator system below. Then the
program must process each of the employee arrivals as it reads it in, using methods in the Elevator class.
When the last employee arrival has been processed, the program must continue running the simulation
until all employees have reached their desired destinations.
Program output: The program must print the elevator set-up information, and each employee arrival.
The program must also print a message indicating what happens in the simulation during each time unit.
After the simulation is complete, the program must print some statistics:
The total number of trips taken by all employees during the simulation.
The total of all trip times, that is, the sum of the times for all employee trips. The time for an
employee trip is the time interval from when the employee arrives to wait for the elevator to the time
the employee gets o the elevator on their desired
oor.
The average employee trip time.
The minimum trip time and details about that particular trip.
The maximum trip time and details about that particular trip.
3
See the sample input and output below for more detail.
Modelling the elevator system: The elevator itself is modelled as a stack of employees in this system.
The top of the stack is the employee closest to the door of the elevator and the bottom of the stack is the
employee furthest from the door.
When an employee wants to get o the elevator on the current
oor, that person may not be at the front
of the elevator (the top of the stack). So the employees in front will have to get o the elevator (stack)
temporarily to let the person o, and then get back on the elevator in the same order they were in before (in
the reverse of the order that they got o the elevator). Use a temporary stack to accomplish this process.
A column of employees waiting for the elevator is modelled as a queue of employees. You should have a
pair of queues for every
oor: one queue for people waiting to go up and another for people waiting to go
down. Use a multi-dimensional array to model all the queues on all the
oors. (Yes, you don't need a queue
for employees going down on the bottom
oor nor one for employees going up on the top
oor. Don't worry
about that | you will never use those particular queues. But having those un-needed queues might make
your code simpler at very little cost.)
Classes you must implement: You will need at least the following classes:
A3<lastName><firstName>: Of course, you need the application class (the class that contains main()),
which prompts the user for the le, reads in the le and simulates the elevator by calling appropriate
methods in the Elevator class (see below).
Employee: An instance of class Employee represents one employee.
An instance must contain at least the employee's ID number, what time the employee arrived at
the elevator queues (simulation time, not real time), what
oor the employee started waiting for the
elevator on, and what
oor the employee wants to go to. See also the description of the input le below
for more information.
Other than a constructor and the normal accessors (getters), this class should not do any processing,
with one exception: you should implement a proper toString() method. Reminder: a toString()
method does not do any printing; it simply returns a text representation (a String) of the class instance
it is called on.
Elevator: As described above, an instance of this class represents the entire elevator system, including the
elevator itself and the queues of waiting employees on each of the
oors the elevator serves. Furthermore,
you will need an array of elevator buttons that model the
oor buttons inside the elevator that an
employee presses to indicate which
oor they want to go to. Your buttons can be counters to count
the number of people wanting to go to each
oor, if you wish. You will also need some variables to
accumulate information about the simulation so that you can print the statistics at the end, and to
keep track of the current simulation time.
Other than the constructor, the class will contain at least four other methods:
A method to model the elevator behaviour during one time unit,
A method to process an employee arrival,
A method to nish running the simulation after the last employee arrival, and
A method to print the statistics about the simulation.
You should implement other methods to make the code readable.
Stack: You must implement a stack of employees. The stack must be implemented using the array imple-
mentation described in class. It must be a proper stack with the standard stack operations (correctly
named). It should not have any non-standard operations, with one possible exception: you might want
to implement a toString() method for debugging purposes.
4
Queue: You must implement a queue of employees. The queue must be implemented using a circular linked
list as described in class. The implementation must not have a dummy node and must have a single
pointer (called end) that points to the last node in the queue. It must be a proper queue with the
standard queue operations (correctly named). It should not have any non-standard operations, with
one possible exception: you might want to implement a toString() method for debugging purposes.
Node: You will need an ordinary node class for the Queue class to use.
The Node class can be a private class inside the Queue class and can have public instance members if it is.
Otherwise, none of the classes should be inside any of the other classes, and their instance members should
be private.
Input le: The rst line of the input le contains two positive integers separated by blanks.
The rst number is the elevator capacity, that is, the maximum number of employees that the elevator
can hold.
The second number is the number of
oors that the elevator services. Assume that the bottom
oor is
oor number 0 and the
oor numbers increase by 1 as you go up.
Each line after the rst line in the input le contains four positive integers separated by blanks representing
one employee arriving at the elevator to take it to another
oor.
The rst number is the time at which the employee arrives to take the elevator. This is elevator system
time, Elevator system time starts at 0 at the beginning of the simulation and increases by 1 for each
elevator time unit that passes.
The second number is the employee's ID number.
The third number is the arrival
oor, that is, the
oor at which the employee arrives for this trip on
the elevator.
The fourth number is the
oor that the employee wishes to travel to.
Additional Notes:
You may assume that the input le does not contain any errors. All commands will be valid commands,
and the format of each line in the le will be as shown above.
Use the sample input below as a starting point. The le A3Input.txt contains additional test input.
The markers will use other input les to test your code, so you should also test your code thoroughly
with your own input.
Sample input and output les: For the following sample input le:
5 7
0 12 2 4
0 6 2 0
1 26 0 5
1 3 3 6
The sample solution program produces the following output:
COMP 2140 A3 Sample Solution --- Fall 2019
Enter the input file name (.txt files only):
A3SmallInput.txt
5
Elevator dimensions:
Number of floors to service: 7
Maximum number of people (load capacity): 5
Elevator begins on floor 0.
Time 0: A person begins waiting to go up: Employee 12, arrival floor 2, arrival time 0, desired floor 4
Time 0: A person begins waiting to go down: Employee 6, arrival floor 2, arrival time 0, desired floor 0
Time 0: Elevator moves up to floor 1
Time 1: A person begins waiting to go up: Employee 26, arrival floor 0, arrival time 1, desired floor 5
Time 1: A person begins waiting to go up: Employee 3, arrival floor 3, arrival time 1, desired floor 6
Time 1: Elevator moves up to floor 2
Time 2: Got on the elevator: Employee 12, arrival floor 2, arrival time 0, desired floor 4
Time 3: Elevator moves up to floor 3
Time 4: Got on the elevator: Employee 3, arrival floor 3, arrival time 1, desired floor 6
Time 5: Elevator moves up to floor 4
Time 6: Got off the elevator: Employee 12, arrival floor 2, arrival time 0, desired floor 4
Time 7: Elevator moves up to floor 5
Time 8: Elevator moves up to floor 6
Time 9: Elevator changed direction: Now going down.
Time 9: Got off the elevator: Employee 3, arrival floor 3, arrival time 1, desired floor 6
Time 10: Elevator moves down to floor 5
Time 11: Elevator moves down to floor 4
Time 12: Elevator moves down to floor 3
Time 13: Elevator moves down to floor 2
Time 14: Got on the elevator: Employee 6, arrival floor 2, arrival time 0, desired floor 0
Time 15: Elevator moves down to floor 1
Time 16: Elevator moves down to floor 0
Time 17: Elevator changed direction: Now going up.
Time 17: Got off the elevator: Employee 6, arrival floor 2, arrival time 0, desired floor 0
Time 17: Got on the elevator: Employee 26, arrival floor 0, arrival time 1, desired floor 5
Time 18: Elevator moves up to floor 1
Time 19: Elevator moves up to floor 2
Time 20: Elevator moves up to floor 3
Time 21: Elevator moves up to floor 4
Time 22: Elevator moves up to floor 5
Time 23: Got off the elevator: Employee 26, arrival floor 0, arrival time 1, desired floor 5
Elevator simulation statistics:
Total number of trips: 4
Total passenger time: 53
Average trip time: 13.25
Minimum trip time: 6
Minimum trip details: Employee 12, arrival floor 2, arrival time 0, desired floor 4
Maximum trip time: 22
Maximum trip details: Employee 26, arrival floor 0, arrival time 1, desired floor 5
Processing ends normally
6
Get Free Quote!
389 Experts Online