Misleading
The exercise wasn't meant to throw you off, but rather to make you think. With the first edition of this book, several readers said the exercises were too easy. This is sort of a response to those comments.
First, I hope no one looks at the solution before they attempt to write their own answer. The real issue here was to see if you could: 1) figure out a random number generator method was needed, 2) find the random class in the library, and 3) if you could use that method correctly.
Assuming you wrote your own version, no doubt you didn't use the const keyword. since it is not introduced until the next chapter. However, after writing your version and looking at mine, I'll bet you did look the keyword up and figured out why it was used. Now, if you were a teacher, which method helps the student learn and retain: 1) a method that just presents an answer, or 2) a method that makes the student devise and understand the answer?
Finally, I almost always present answers in the book the way I would answer the problem. Indeed, you can leave const out of the solution and it works just fine. Figuring that out, you should then ask yourself: "If it works without it, why did he use it?" Taking that approach will make you a better programmer.
In some cases, my solution may use a concept not yet covered, but if that is the case, the concept will always be simple and is used to illustrate its use, not really teach its use. If you find another answer like this, just take a quick look in the index and read a paragraph or two and it should become clear. When the concept is actually discussed in the book, hopefully you'll recall this program and the "ah-hah" light will go on.
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
|