Page 270 - Introduction to Programming with Java: A Problem Solving Approach
P. 270
236 Chapter 6 Object-Oriented Programming
§6.11 Argument Passing
15. How is a method parameter like a local variable, and how do they differ?
16. What is the relationship and difference between a method argument and a method parameter?
§6.12 Specialized Methods—Accessors, Mutators, Boolean Methods 17. What is the standard prefix for an accessor method?
18. What is the standard prefix for a mutator method?
19. What is the standard prefix for a Boolean method?
§6.13 Problem Solving with Simulation (Optional)
20. Identify two general ways to reduce the size of the error in a simulation. For a given accuracy, which way is
more efficient?
Exercises
1. [after §6.2] Suppose you are asked to model plants using an OOP program. For each of the following plant- related entities, specify the most appropriate item to use for its implementation. For each entity, select one of the following: instance variable, object, method, or class.
a) plant height
b) sequence of activities that occur when a seed germinates
c) an indication of whether the plant contains a vascular system d) an individual plant
2. [after §6.3] In Java, how do you encapsulate an instance variable?
Apago PDF Enhancer
3. [after §6.4] Describe the relationship between the main method and driver and driven classes. Give an example of a class that runs by itself and does not need a separate driver.
4. [after §6.4] Wrapper objects: The wrapper classes discussed in Chapter 5 also provide you with the ability to instantiate objects that are wrapped versions of primitive variables. For example, to create a wrapped version of the double number x, you can do this:
double x = 55.0;
Double xWrapped = new Double(x)
This instantiates an object of type Double, which is a wrapped version of the primitive variable, x. Then it assigns a reference to that object to the reference variable, xWrapped. The Double class has a number of pre-built methods that work with Double objects. You can read about these methods in Sun’s documentation on the Double class. The following program illustrates some of these methods.
/*************************************************************
*
Wrapper.java
Dean & Dean
This program exercises some wrapped primitive numbers.
*************************************************************/
*
*
*
public class Wrapper
{
public static void main(String[] args)
{
double x = 44.5;
double y = 44.5;