Answers to Chapter 5 Review Questions

   


Chapter 5

1:

What is lexical analysis?

A:

Lexical analysis is a process performed by the compiler to distinguish the atomic parts (in the source code of a computer language) from each other.

2:

What are the atomic parts of a C# program?

A:

The atomic C# parts constitute the unbreakable parts, such as keywords, identifiers, curly braces, parentheses, semicolon, and so on.

3:

What is Pascal casing and camel casing? For which parts of the C# program should they be used?

A:

If an identifier is written with Pascal casing, all words within this name begin with an uppercase character, as in CalculateMaxDistance.

Camel casing is nearly identical to Pascal casing, the only difference being that identifiers written with camel casing do not contain an uppercase letter in its first word. numberOfElevators is written with camel casing.

Pascal casing should be used for class and method identifiers and camel casing for variables.

4:

What is the main difference between variables and literals?

A:

A literal has the fixed value that is written in the source code. For example, the literal 10 always has the value 10. In contrast a variable called myNumber can represent different values.

5:

What are operators and operands? How do they relate?

A:

Operators act on operands.

6:

Is 50 an expression? Is (50 + x)? Is public?

A:

50 and (50 + x) are both expressions, but the keyword public is not an expression.

7:

Give examples of typical keywords in C#.

A:

if, public, private.

8:

Why is pseudocode not well suited for expressing the overall design of an object-oriented program?

A:

Pseudocode is highly suited to express a set of detailed actions (an algorithm) that must be executed sequentially to solve a task. However, classes break free from this sequential execution form. This renders the pseudocode incapable of describing overall class designs.

9:

What kind of relationship does a BankCustomer object have with a BankTeller object? How is this expressed in UML?

A:

An association relationship, which in UML is illustrated with a simple line.

10:

What kind of relationship does a Heart object have with a HumanBody object. How is this expressed in UML?

A:

A composition relationship, which in UML is illustrated with a line that has a black diamond attached.

11:

What kind of relationship does a LightBulb have with a Lamp object. How is this expressed in UML?

A:

An aggregation relationship, which in UML is illustrated with a line that has a white diamond attached.

12:

How is an association relationship implemented in C#?

A:

Through instance variables that reference other objects.

13:

How can instance variables be initialized when an object is created?

A:

Instance variables can be initialized as part of their declaration statement or in a constructor.

14:

Describe passenger when declared as in the following line:

 private Person passenger; 
A:

The instance variable passenger can hold a reference to a Person object.

Answers to Programming Exercises in Chapter 5

1:

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

2:

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

3:

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

4:

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

5:

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

6:

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.

A:

The following program contains answers to all six programming exercises presented in Chapter 5. Lines that are added or altered compared to Listing 5.1 in Chapter 5 due to an exercise are marked with a comment. For example, a line affected by exercise 1 is marked //Ex.1.

 // A simple elevator simulation using System; class Elevator {     private int currentFloor = 0; //Ex.4     private int requestedFloor = 0;     private int totalFloorsTraveled = 0;     private int totalTripsTraveled = 0; //Ex.6     private Person passenger;     public void LoadPassenger()     {         passenger = new Person();     }     public void InitiateNewFloorRequest()     {         requestedFloor = passenger.NewFloorRequest();         Console.WriteLine("Departing floor: " + currentFloor             + " Traveling to floor: " + requestedFloor);         totalFloorsTraveled = totalFloorsTraveled +             Math.Abs(currentFloor - requestedFloor);         totalTripsTraveled = totalTripsTraveled + 1; //Ex.6         currentFloor = requestedFloor;     }     public void ReportStatistic()     {         Console.WriteLine("Total floors traveled: " + totalFloorsTraveled);         Console.WriteLine("Total trips traveled: "             + totalTripsTraveled); //Ex.6     } } class Person {     private System.Random randomNumberGenerator;     public Person()     {         randomNumberGenerator = new System.Random();     }     public int NewFloorRequest()     {          // Return randomly generated number         return randomNumberGenerator.Next(0,50); //Ex.3     } } class Building {     private static Elevator elevatorA;     public static void Main()     {         Console.WriteLine("The simulation has commenced"); //Ex.1         elevatorA = new Elevator();         elevatorA.LoadPassenger();         elevatorA.InitiateNewFloorRequest();         elevatorA.InitiateNewFloorRequest();         elevatorA.InitiateNewFloorRequest();         elevatorA.InitiateNewFloorRequest();         elevatorA.InitiateNewFloorRequest();         elevatorA.InitiateNewFloorRequest();//Ex.5         elevatorA.InitiateNewFloorRequest();//Ex.5         elevatorA.InitiateNewFloorRequest();//Ex.5         elevatorA.InitiateNewFloorRequest();//Ex.5         elevatorA.InitiateNewFloorRequest();//Ex.5         elevatorA.ReportStatistic();         Console.WriteLine("The simulation has ended"); //Ex.2     } } 


   


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

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net