Page 244 - Introduction to Programming with Java: A Problem Solving Approach
P. 244

                210 Chapter 6 Object-Oriented Programming
Here’s a summary of default values for instance variables:
 Instance Variable’s Type
 Default Value
 integer
  0
 floating point
  0.0
 boolean
  false
 reference
 null
     Instance Variable Persistence
Now consider variable persistence. Persistence refers to how long a variable’s value survives before it’s wiped out. Instance variables persist for the duration of a particular object. Thus, if an object makes two method calls, the second called method does not reset the calling object’s instance variables to their initial- ized values. Instead, the object’s instance variables retain their values from one method call to the next. For example, in the MouseDriver class, gus calls grow twice. In the first call to grow, gus’s age incre- ments from 0 to 1. In the second call to grow, gus’s age starts out as 1 and increments to 2. gus’s age retains its value from one grow call to the next because age is an instance variable.
6.7 Tracing an OOP Program
To reinforce what you’ve learned so far in this chapter, we’ll trace the Mouse program. Remember the trac-
ing procedure we used in prior chapters? It worked fine for programs with only one method—the main Apago PDF Enhancer
method. But for OOP programs with multiple classes and multiple methods, you’ll need to keep track of which class and which method you’re in and which object called that method. In addition, you’ll need to keep track of parameters and instance variables. This requires a more elaborate trace table.
In tracing the Mouse program, we’ll use a slightly different driver, the MouseDriver2 class, shown in Figure 6.8. In MouseDriver2, we delay the instantiation of the individual mice and assign their growth rates (by calling setPercentGrowthRate) immediately after each instantiation. This is better style, because
        Use trace to find cause of problem.
it more closely associates each object’s instantiation with its growth rate assignment. How- ever, in changing the driver we “accidentally” forget to call setPercentGrowthRate for jaq, the second mouse. You can see the effect of this logic error in the output—jaq doesn’t grow (after the first day, jaq still weighs 1 gram). But let’s pretend that you don’t know why this error occurs and use the trace to help find its cause. Remember—tracing is
    an effective tool when you need help debugging a program.
To perform the trace, in addition to the driver, you’ll also need the code for the driven class. For your
convenience, we repeat the original driven Mouse class in Figure 6.9. Trace Setup
Figure 6.10 shows the setup. As with the traces in the previous chapters, the input goes in the top-left corner. Unlike the traces in the previous chapters, the headings under the input now require more than one line. The first line of headings shows the class names—MouseDriver2 and Mouse. Under each class name head- ing, there’s a heading for each of the class’s methods. In the trace setup, find the setPercentGrowth- Rate, grow, and display method headings (to save space, we abbreviated setPercentGrowthRate and display to setPGR and disp, respectively). And under each method-name heading, there’s a heading for each of the method’s local variables and parameters.










































































   242   243   244   245   246