Exercises in this lecture   previous -- Keyboard shortcut: 'p'  next -- Keyboard shortcut: 'n'  Go to the notes, in which this exercise belongs -- Keyboard shortcut: 'u'   Alphabetic index   Course home      

Exercise 5.18
Additional Die events


In the Die event example, we have a public event called twoSixesInARow which is triggered if a die tosses two sixes in a row. In the sample client program we add an anonymous method to this event which reports the string parameter of the event on standard output.

Add yet another method to the twoSixesInARow event which counts the number of two sixes in a row. For this purpose we need - quite naturally - an integer variable for counting. Where is this variable located in relation to the counting event? Will you place the variable inside the new method, inside the Die class, or inside the client class of the Die? Please argue in favor of your choice.

Add a similar event called fullHouse, of the same type Notifier, which is triggered if the Die tosses a full house. A full house means (inspired from the rules of Yahtzee) two tosses of one kind and three tosses of another kind - in a row. For instance, the toss sequence 5 6 5 6 5 leads to a full house. Similarly, the 1 4 4 4 1 leads to a full house. The toss sequence 5 1 6 6 6 6 5 is not a full house, and the toss sequence 6 6 6 6 6 is not a full house.

Be sure to test-drive the program and watch for triggering of the new event.

The twoSixesInARow event (and probably also the new fullHouse event) is public in class Die. Reorganize the Die class and its client such that the events become private in class Die. What does it take to carry out this reorganization?