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

                6.7 TracinganOOPProgram 213
 input
10
    MouseDriver2
Mouse
         main
             line#
rate
gus
jaq
line#
setPGR
this
rate
grow
this
disp
 this
output
    Figure 6.10 Trace setup for the Mouse program
setPercentGrowthRate, grow, and display methods, we include a column for this so the trace can keep track of which object called the method.
Note the vacant area under the Mouse heading. We’ll fill in more headings there as we execute the trace. Trace Execution
Using Figure 6.10’s trace setup as a starting point, we’ll walk you through the key sections of the trace shown in Figure 6.11. We’ll focus on the OOP parts of the trace since those are the parts that are new to you. When starting a method, under the method’s local variable headings, write initial values for each of the local variables. Use a question mark for local variables that are uninitialized. In the first three lines of Figure 6.11’s trace, note the ?’s for the uAninpitialigzedogroPwtDhRFateE(abnbrhevaiatnedctoeratre), gus, and jaq local variables.
When an object is instantiated, under the object’s class-name heading, provide a column heading named “obj#”, where # is a unique number. Under the obj# heading, provide an underlined column heading for each of the object’s instance variables. Under the instance variable headings, write initial values for each of the instance variables. In Figure 6.11’s trace, note the obj1 and obj2 column headings and their age, weight, and percentGrowthRate (abbreviated to rate) subheadings. Also note the initial values for the age, weight, and percentGrowthRate instance variables.
When there’s an assignment into a reference variable, write obj# under the reference variable’s column heading, where obj# matches up with the associated obj# in the object portion of the trace. For example, in Figure6.11’strace,wecreatedobj1whiletracingthegus = new Mouse();statement.Subsequently,we put obj1 under the gus column heading.
When there’s a method call, under the called method’s this column heading, write the calling object’s obj#. In Figure 6.11’s trace, note obj1 under setPercentGrowthRate’s this heading. If the method call contains an argument, write the argument’s value under the called method’s associated parameter. In the trace, note the passed-in 10 under the setPercentGrowthRate’s percentGrowthRate heading. Inside the method, if there’s a this reference, find the obj# under the method’s this column heading. Then go to the found obj#’s heading and read or update the obj#’s value accordingly. In Figure 6.9’s Mouse class, note this.percentGrowthRate in the setPercentGrowthRate method body. In the trace, note that setPercentGrowthRate’s this reference refers to obj1, so obj1’s percentGrowthRate is updated accordingly.
When you finish tracing a method, draw a horizontal line under the method’s variable values to indicate the end of the method trace and to signify that the values in the method’s local variables are wiped out. For example, in the trace, the heavy horizontal line in Mouse line #20 under set PGR indicates the end of the setPercentGrowthRate method, and it signifies that percentGrowthRate’s value is wiped out.








































































   245   246   247   248   249