4.31 Mб


Other example of associations that are not aggregations are

Employee works for Company

BankCustomer interacts with the BankTeller


Associations that precisely connect two classes are called binary associations; they are the most common kind of association.


This chapter consists of two main parts. The first part is about the lexical structure of a C# source program. The second part provides an example of an object-oriented program, which relates directly to the discussion in Chapter 3 about abstraction and encapsulation.

The following are the most important points covered in this chapter:

A C# source program can be viewed as a collection of identifiers, keywords, whitespace, comments, literals, operators, and separators.

C# is a case-sensitive language. To improve the clarity for other readers of the code, it is important to adhere to a certain capitalization style. Pascal Casing (ThisIsPascalCasing) and Camel Casing (thisIsCamelCasing) are the preferred styles and are used for different C# constructs.

A literal has the value that is written in the source code (what you see is what you get). Values like 10 and “This is a dog” are examples of literals.

Separators, such as semicolons (;), commas (,), and periods (.), separate different elements in C# from each other.

Operators act on operands. Operands combine with operators to form expressions.

Instance variables must be initialized when an object is created. This is either done automatically by the runtime, by a declaration initialization, or by an instance constructor.

An object can hold a reference to another object in an instance variable. Such a permanent relationship is called an association.

An object is created by using the new keyword.

In an object-oriented program, classes collaborate to provide the functionality of the program.

Two classes can collaborate by having a relationship.

Common association relationships are aggregations and compositions.

The Unified Modeling Language (UML) is by far the most popular graphical modeling language used to express and illustrate object-oriented program designs.


Review Questions

1.What is lexical analysis?

2.What are the atomic parts of a C# program?

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

4.What is the main difference between variables and literals?

5.What are operators and operands? How do they relate?

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

7.Give examples of typical keywords in C#.

8.Why is pseudocode not well suited for expressing the overall design of an object-ori- ented program?

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

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

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

12.How is an association relationship implemented in C#?

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

14.Describe passenger when declared as in the following line:

private Person passenger;

Programming Exercises

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

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.

7.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.

Introduction to Windows Forms


The Hello Windows Forms Application


Creating and Using an Event Handler


Defining the Border Style of the Form


Adding a Menu 196


Adding a Menu Shortcut 198


Handling Events from Menus 199