Скачиваний:
64
Добавлен:
15.03.2015
Размер:
4.31 Mб
Скачать

Understanding C# Programs

45

Summary

Today’s lesson continued to help build a foundation that will be used to teach you C#. Today you learned about some of the basic parts of a C# application. You learned that comments help make your programs easier to understand.

You also learned about the basic parts of a C# application, including whitespace, C# keywords, literals, and identifiers. Looking at an application, you saw how these parts are

combined to create a complete listing. This included seeing a special identifier used as a 2 starting point in an application—Main().

After you examined a listing, you explored an overview of object-oriented programming, including the concepts of encapsulation, polymorphism, inheritance, and reuse.

Today’s lesson concluded with some basic information on using the

System.Console.WriteLine() and System.Console.Write() routines. You learned that these two routines are used to print information to the screen (console).

Q&A

Q What is the difference between component-based and object-oriented?

AC# has been referred to as component-based by some people. Component-based development can be considered an extension of object-oriented programming. A component is simply a standalone piece of code that performs a specific task.

Component-based programming consists of creating lots of these standalone components that can be reused. You then link them to build applications.

Q What other languages are considered object-oriented?

AOther languages that are considered object-oriented include C++, Java, and SmallTalk. Microsoft’s Visual Basic.NET can also be used for object-oriented programming. There are other languages, but these are the most popular.

Q What is composition? Is it an object-oriented term?

AMany people confuse inheritance with composition, but they are different. With composition, one object is used within another object. Figure 2.1 is a composition of many circles. This is different from the sphere in the previous example. The sphere is not composed of a circle; it is an extension of the circle. To summarize the difference between composition and inheritance: Composition occurs when one class (object) has another within it. Inheritance occurs when one class (object) is an expansion of another.

46

Day 2

FIGURE 2.1

A snowman made of circles.

Workshop

The Workshop provides quiz questions to help you solidify your understanding of the material covered and exercises to provide you with experience in using what you’ve learned. Try to understand the quiz and exercise answers before continuing to the next day’s lesson. Answers are provided in Appendix A, “Answers.”

Quiz

1.What are the three types of comments you can use in a C# program?

2.How are each of the three types of comments entered into a C# program?

3.What impact does whitespace have on a C# program?

4.Which of the following are C# keywords?

field, cast, as, object, throw, baseball, catch, football, fumble, basketball

5.What is a literal?

6.Which is true:

Expressions are made up of statements. Statements are made up of expressions.

Expressions and statements have nothing to do with each other.

7.What is an empty statement?

8.What are the key concepts of object-oriented programming?

Understanding C# Programs

47

9.What is the difference between WriteLine() and Write()?

10.What is used as a placeholder when printing a value with WriteLine() or

Write()?

Exercises

1. Enter and compile the following program (listit.cs). Remember, you don’t enter the

line numbers.

2

1: //

ListIT.cs - program to print a listing with line numbers

 

2:

//

-----------------------------------------------------------

 

3:

 

 

 

4:using System;

5:using System.IO;

7:class ListIT

8:{

9:public static void Main(string[] args)

10:{

11:try

12:{

13:

14:int ctr=0;

15:if (args.Length <= 0 )

16:{

17:Console.WriteLine(“Format: ListIT filename”);

18:return;

19:}

20:else

21:{

22:FileStream f = new FileStream(args[0], FileMode.Open);

23:try

24:{

25:

StreamReader t = new StreamReader(f);

26:

string line;

27:

while ((line = t.ReadLine()) != null)

28:

{

29:

ctr++;

30:

Console.WriteLine(“{0}: {1}”, ctr, line);

31:

}

32:

f.Close();

33:}

34:finally

35:{

36: f.Close();

37:}

38:}

39:}

40:catch (System.IO.FileNotFoundException)

41:{

48

Day 2

42: Console.WriteLine (“ListIT could not find the file {0}”, args[0]);

43: } 44:

45:catch (Exception e)

46:{

47:Console.WriteLine(“Exception: {0}\n\n”, e);

48:}

49:}

50:}

2.Run the program entered in exercise 1. What happens when you run this program? Run this program a second time. This time enter the following at the command line:

listit listit.cs

What happens this time?

3.Enter, compile, and run the following program. What does it do?

1:// ex0203.cs - Exercise 3 for Day 2

2://-----------------------------------------------

3:

4:class Exercise2

5:{

6:public static void Main()

7:{

8:int x = 0;

9:

10:for( x = 1; x <= 10; x++ )

11:{

12:System.Console.Write(“{0:D3} “, x);

13:}

14:}

15:}

4.BUG BUSTER: The following program has a problem. Enter it in your editor and compile it. Which lines generate error messages?

1:// bugbust.cs

2://-----------------------------------------------

3:

4:class bugbust

5:{

6:public static void Main()

7:{

8:System.Console.WriteLine(“\nA fun number is {1}”, 123 );

9:}

10:}

5.Write the line of code that prints your name on the screen.

WEEK 1

DAY 3

Storing Information with Variables

When you start writing programs, you are going to quickly find that you need to keep track of different types of information. This might be the tracking of your clients’ names, the amounts of money in your bank accounts, or the ages of your favorite movie stars. To keep track of this information, your computer programs need a way to store the values. Today you

Learn what is a variable

Discover how to create variable names in C#

Use different types of numeric variables

Evaluate the differences and similarities between character and numeric values

See how to declare and initialize variables