Java Reference
In-Depth Information
needed and it provides communication between objects of the class. Make a
method static if the only variables it references are its parameters and local vari-
ables and the only methods of the class that it calls are static methods.
• Object-oriented design. OO design lays emphasis on making a list of the
nouns of the problem domain; they represent objects, and classes are designed
whose instances are those objects. Verbs tend to be models for the methods of the
classes.
Exercises for Chapter 3
E1. Names come in several forms, given by these examples: (1) David Gries (2)
David J. Gries (3) David Joseph Gries (4) Gries, David (5) Gries, David J. and
(6) Gries, David Joseph. Design a class that will allow someone to create an
instance with a first name, middle name, and last name and retrieve any of these
six forms from it. You might allow a person to omit the middle name if there is
none. Here are two different schemes. In one scheme, there are six methods —
to retrieve a name in each of the six formats. In the other scheme, there is method
for indicating which of the six formats is desired and only one method for
retrieving a name —using the desired form. Which of these schemes do you pre-
fer? Why? If you use the second alternative, use constants to name the six for-
mats.
E2. Implement and test the class of the previous exercise. How many test cases
do you need to ensure that it is correct? If you need help with handling strings,
see Sec. 5.2.
E3. Design a class an instance of which is a pair of dice. Each die has six faces,
with the numbers in 1, 2, 3, 4, 5, 6 on them. At all times, one face is face-up.
What methods will you have? You will probably want one for “rolling” the dice,
so that new faces become face-up in a random manner.
E4. Implement and test the class designed in the previous exercise. In imple-
menting a roll of the dice, you may want to look at class Random (see Sec. 5.6).
E5. Design a class whose instances are standard playing cards. Each card has a
suit (one of spades, hearts, clubs, and diamonds) and a value (one of 2, 3, 4, 5,
6, 7, 8, 9, 10, Jack, Queen, King, Ace). Think carefully about what methods you
incorporate in the class. Also, think about having constants to denote the suits
and values. See Sec. 3.3.1.
E6. Implement and test the class of the previous exercise.
E7. Design a class that represents a list of cards (of playing cards; see exercise
E5). The whole deck of 52 cards could be an instance of this class. However, a
player's “hand” —say, in poker, 5 cards, could be an instance as well. In design-
ing the methods, think of a card game that you have played and figure out what
Search WWH ::




Custom Search