Page 388 - Introduction to Programming with Java: A Problem Solving Approach
P. 388
354 Chapter 9 Classes with Class Members
all class variable declarations above all instance variable declarations. Here is the preferred sequence of
declarations within a given class:
class constants instance constants class variables instance variables constructors methods
9.5 Writing Your Own Utility Class
Up to this point, you’ve implemented methods that solve problems for a particular class. Suppose you want to implement methods that are more general purpose, so that multiple and unforeseen classes can use them. Those types of methods are called utility methods. In the past, you’ve used utility methods from the Math class; for example, Math.round and Math.sqrt. In this section you learn to write your own utility methods as part of a utility class.
See Figure 9.3’s PrintUtilities class. It contains print-oriented utility constants and methods. The two constants, MAX_COL and MAX_ROW, keep track of the maximum column and maximum row for a standard-sized piece of paper. If you have multiple classes that print reports, those constants can help to ensure report-size uniformity. The printCentered method prints a given string horizontally centered. The printUnderlined method prints a given string with dashes underneath it. We put those methods in a utility class because they perforAmpraintgrooutinesPthDatFmighEt bne nheedaednbcy meulrtiple other classes.
In the PrintUtilities class, note that the constants and methods all use the public and static modifiers. That’s normal for utility class members. The public and static modifiers make it easy for other classes to access PrintUtilities’ members.
9.6 Using Class Members in Conjunction with Instance Members
Now, let’s look at a problem that requires a combination of instance members and class members. The goal is to model a collection of penny jars. With each insertion of a penny in any jar, we want the program to print “clink” and increment the penny count for that jar and the total penny count. When the total number of pennies exceeds a fixed goal, the program should print “Time to spend!” Then the program should print the total number of pennies in each jar and the total number of pennies in all jars.
The Primary Class
The most important part of this problem is the usage of instance and class members. So let’s address this complexity immediately. We need a class that describes both in- dividual penny jars and the collection of all penny jars. Figure 9.4 has a UML class diagram for a PennyJar class that does what we want. To handle the pennies in an
individual PennyJar object, it uses instance members—pennies, addPenny, and getPennies. To handle the pennies in the collection of all penny jars, it uses class members—GOAL, allPennies, and getAllPennies. In the UML diagram, you can tell that those three members are class members because they are underlined (as you may recall, UML standards suggest that you underline all class members). The UML diagram does not include a main method, so this class won’t run by itself. In effect, we’re starting
Address the most critical problem as soon as possible.