Programming Exercises


Enable the program in Listing 5.1 to exhibit the following functionality by changing its source code:


Print "The simulation has commenced" on the command console right after the program is started.


Print "The simulation has ended" as the last thing just before the program is terminated.


Instead of merely choosing floors between 1 and 30, the Person class chooses floors between 0 and 50.


On its first ride, Elevator starts at floor number 0 instead of floor number 1.


The Elevator object does 10 journeys instead of the 5 it is doing now.


Elevator is currently counting the total floors traveled with the totalFloorsTraveled variable. Declare an instance variable inside the Elevator class that keeps track of the number of trips this elevator completes. You could call this variable totalTripsTraveled. The Elevator should update this variable by adding one to totalTripsTraveled after every trip. Update the ReportStatistics method of the Elevator class to print out not only totalFloorsTraveled but also totalTripsTraveled, accompanied by an explanation for what is being printed.


Add an instance variable to the Elevator class that can hold the name of the elevator. You can call it myName. Which type should you use for this instance variable? Should you use private or public when you declare it? Write a constructor by which you can set the value of this variable as you create the Elevator object with new and assign it to a variable. (Hint: The constructor is a method and must have the same name as its class. This constructor must have a formal parameter of type string in its header.) Adjust the call to the constructor when using the keyword new by inserting the name of the Elevator as an argument (between the parenthesis instead of new Elevator(), write new Elevator("ElevatorA").

Every time the Elevator completes a trip, it should print its name along with its departing and arrival floor. In other words, instead of printing

 Departing floor: 2 Traveling to floor: 24 

it should print

 ElevatorA: Departing floor: 2 Traveling to floor: 24 

where ElevatorA is the name of the Elevator residing inside myName.


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: