Page 277 - Introduction to Programming with Java: A Problem Solving Approach
P. 277
10. For an object’s instance variables, the default values are: int = 0, double = 0.0, boolean = false.
11. gus’s age is an instance variable. Instance variables persist for the duration of a particular object. Since the gus object is declared in main, gus and its instance variables (including age) persist for the duration of the main method.
12. Some reasons to construct a UML class diagram before writing code:
a) It provides a complete “to do” list. When you are into the details of writing one method, and wondering
whether that method should perform a particular function, the diagram reminds you of what other meth-
ods might be able to perform that function.
b) It provides a complete “parts list,” like the parts list of a typical user-assembled “kit.” This pre-defined
list helps you avoid accidentally generating different and conflicting names for variables and parameters
as you write your code.
c) It’s a working document that can change as work progresses. Changing the UML class diagram helps
identify needed alterations to previous work.
13. Immediately after the statement Mouse mickey; the value of mickey would be garbage.
14. False. Normally, for a method that returns a value, you should have a single return statement at the end of
the method. However, it’s also legal to have return statements in the middle of a method. That might be appropriate in a very short method, where an internal return is immediately obvious. If the method is relatively long, however, a reader might not notice an internal return. With a large method, it’s better practice to arrange things so that there is only one return, located at the end of the method.
15. Parameters and local variables both have method scope and persistence. The code inside the method treats parameters just like it treats local variables. The method initializes the local variables, while the method call initializes the parameters.
16. Arguments and parameters are two different words describing data that passes into a called method. An arguments is the methoAd cpalal’s gnamoe forPthDe dFata, aEndna pharamnetecr isethre method’s name for the same data. A parameter is just a copy of the method call’s argument, however, so if the called method changes the value of one if its parameters, this does not alter the value of the method call’s argument.
17. The standard prefix for an accessor method is get.
18. The standard prefix for a mutator method is set.
19. The standard prefix for a Boolean method is is.
20. To reduce the error in a simulation, you can reduce step size or switch to a step-with-midpoint algorithm.
For a given accuracy, the step-with-midpoint algorithm is more efficient.
Review Question Solutions 243