Java Reference
In-Depth Information
numbers and strings. A Random object generates random numbers. It is a good idea
to choose class names for actors that end in Ȓ-erȓ or Ȓ-orȓ. (A better name for the
Random class might be RandomNumberGenerator .)
Very occasionally, a class has no objects, but it contains a collection of related static
methods and constants. The Math class is a typical example. Such a class is called a
utility class.
Finally, you have seen classes with only a main method. Their sole purpose is to
start a program. From a design perspective, these are somewhat degenerate examples
of classes.
What might not be a good class? If you can't tell from the class name what an object
of the class is supposed to do, then you are probably not on the right track. For
example, your homework assignment might ask you to write a program that prints
paychecks. Suppose you start by trying to design a class PaycheckProgram . What
would an object of this class do? An object of this class would have to do everything
that the homework needs to do. That doesn't simplify anything. A better class would
be Paycheck . Then your program can manipulate one or more Paycheck objects.
Another common mistake, particularly by students who are used to writing programs
that consist of functions, is to turn an action into a class. For example, if your
homework assignment is to compute a paycheck, you may consider writing a class
ComputePaycheck . But can you visualize a ȒComputePaycheckȓ object? The fact
that ȒComputePaycheckȓ isn't a noun tips you off that you are on the wrong track. On
the other hand, a Paycheck class makes intuitive sense. The word Ȓpaycheckȓ is a
noun. You can visualize a paycheck object. You can then think about useful methods
of the Paycheck class, such as computeTaxes , that help you solve the
assignment.
S ELF C HECK
1. What is the rule of thumb for finding classes?
2. Your job is to write a program that plays chess. Might ChessBoard be
an appropriate class? How about MovePiece ?
337
Search WWH ::




Custom Search