Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft CSharp Programming For The Absolute Beginner (2002) [eng]-1.pdf
Скачиваний:
46
Добавлен:
16.08.2013
Размер:
15.71 Mб
Скачать

Closing Up the code

This program has a number of structures embedded inside each other. Ensuring that all the code ends properly can be very difficult if you aren’t careful about indentation and comments. However, I was careful about these things, so closing up all the code was a straightforward job:

} // end foreach } // end while loop

} // end main } // end class

}// end namespace

Summary

You learned some very important skills in this chapter. You investigated the for loop, which is used to repeat code a certain number of times. You also looked at how a while loop can be used when the code must repeat an undetermined number of times. You investigated the string object and used some of its methods and properties to do interesting things with text, such as capitalization, searching for a phrase, and determining the length of a string. Finally, you learned how to use the STAIR process to plan a program. In the next chapter, you’ll begin to create your own objects.

Challenges

Write a program that simulates a 10−lap race. Have the program print out the lap time each time through the loop.

Write a program that asks a user how many dice to roll, rolls a six−sided die that many times, and returns the total and average roll.

Modify the Math Game from Chapter 2 so that it generates five questions of each type.

Modify the Math Game so that each question is repeated until the user gets the correct response.

Create a cartoon simulator that emulates the speech pattern of a cartoon character by replacing all r’s with w’s so that You crazy rabbit becomes You cwazy wabbit.

77

Chapter 4: Objects and Encapsulation: The Critter

Program

You have learned most of the critical aspects of programming a computer. From now on, you will be learning variations of these basic skills. To help you manage the complexity of larger programs, C# uses an important programming paradigm called object−oriented programming (OOP). You are already slightly familiar with object orientation because you’ve been using objects that come with the .NET environment, such as the string object, convert object, and console object. However, you will discover the real power of OOP when you create objects of your own. In a nutshell, object−oriented programming enables you to combine the information and instructions of your programs into objects. The objects you have already used feature properties, which encapsulate the data in a program, and methods, which house the instructions in a program. You will learn how to build your own objects with these properties and methods. After reading this chapter you will be able to

Create your own objects.

Add methods to your program.

Communicate to and from your methods.

Build a classic console−based menu system.

Trap for certain kinds of errors.

Create your own custom object.

Add methods and properties to your object.

Understand how basic scope modifiers work.

Use properties to improve the reliability of your object.

Introducing the Critter Program

Like most of the examples in this book, the main program for this chapter has little practical merit but is entertaining. You’ll build a virtual pet named a critter. The critter is very simple, but as you see in Figures 4.1 through 4.5, it’s fun to interact with.

Figure 4.1: Introducing your critter. Go ahead and talk to it!

78

Figure 4.2: The critter tells you about itself.

Figure 4.3: After talking for a while, the critter becomes melancholy.

Figure 4.4: Without appropriate attention, the critter becomes angry.

79

Соседние файлы в предмете Программирование на C++