Page 237 - Introduction to Programming with Java: A Problem Solving Approach
P. 237
Now, take a look at the Mouse class’s display and grow methods. The display method is straight- forward; it prints a mouse’s age and weight. The grow method simulates one day of weight gain for a mouse. The weight-gain formula adds a certain percentage of the current weight to the current weight. That means that the mouse will continue to grow every day of its life. That’s a simple, but not very accurate, portrayal of normal weight gain. We’ve intentionally kept the weight-gain formula simple in order to avoid getting bogged down in complicated math. In the final section of this chapter, we provide more realistic growth models.
Finally, take a look at the Mouse class’s comments. Note the descriptions above each method. Proper style suggests that, above each method, you should have a blank line, a line of asterisks, a blank line, a description of the method, and another blank line. The blank lines and asterisks serve to separate the meth- ods. The method descriptions allow someone who’s reading your program to quickly get an idea of what’s going on.
6.4 Driver Class What Is a Driver?
Driver is a common computer term that applies to a piece of software that runs or “drives” something else. For example, a printer driver is a program that is in charge of running a printer. Likewise, a driver class is a class that is in charge of running another class.
In Figure 6.5, we present a MouseDriver class. We name the class MouseDriver because it is in charge of driving the Mouse class. We say that the MouseDriver class drives the Mouse class because it
jaq = new Mouse() statements. That code creates Mouse objects gus and jaq.1 In addition note Apago PDF Enhancer
the gus.setPercentGrowthRate(growthRate) code. That code manipulates the gus object by updating gus’s percentGrowthRate value.
Normally, a driver class consists entirely of a main method and nothing else. The driver class, with its main method, is the starting point for the program. It calls upon the driven class to create objects and manipulate them. The driven class dutifully carries out the object creation and object manipulation requests. Normally, carrying out those tasks is the primary focus of the program, and their implementation requires the majority of the program’s code. Thus, driven classes are typically (but not always) longer than driver classes.
Driver classes, such as the MouseDriver class, are in separate files from the classes that they drive.
To make them accessible from the outside world, driver classes must be public. Each public class must
be stored in a separate file whose name is the same as the class name, so the MouseDriver class must
be stored in a file named MouseDriver.java. For MouseDriver’s code to find the Mouse class, both
2
In the MouseDriver class, we create Mouse objects, and we refer to those Mouse objects using gus and jaq, where gus and jaq are reference variables. The value contained in a reference variable is a “refer- ence” to an object (thus the name reference variable). More precisely, a reference variable holds the address of where an object is stored in memory. For a pictorial explanation, see Figure 6.6. In the figure, the little
1 Father of two preschool girls, author John Dean is immersed in all things Disney. Gus and Jaq are mice in the Disney classic,
Cinderella.
2 We’re keeping things simple by telling you to put both classes in the same directory. Actually, the files may be in different directo- ries, but then you’d need to use a package to group together your classes. Appendix 4 describes how to group classes into a package.
classes should be in the same directory.
Reference Variables
6.4 DriverClass 203
creates Mouse objects and then manipulates them. For example, note the gus
= new
Mouse() and the