Java Reference
In-Depth Information
LISTING 7.15
continued
After changing the values:
f1 f2 f3
999 888 777
After calling changeValues:
a1 a2 a3
111 888 333
Listing 7.16 shows the
ParameterModifier
class, and Listing 7.17 shows the
Num
class. Inside the
changeValues
method, a modification is made to each of the
three formal parameters: the integer parameter is set to a different value, the value
stored in the first
Num
parameter is changed using its
setValue
method, and a new
LISTING 7.16
//********************************************************************
// ParameterModifier.java Author: Lewis/Loftus
//
// Demonstrates the effects of changing parameter values.
//********************************************************************
public class
ParameterModifier
{
//-----------------------------------------------------------------
// Modifies the parameters, printing their values before and
// after making the changes.
//-----------------------------------------------------------------
public void
changeValues (
int
f1, Num f2, Num f3)
{
System.out.println ("Before changing the values:");
System.out.println ("f1\tf2\tf3");
System.out.println (f1 + "\t" + f2 + "\t" + f3 + "\n");
f1 = 999;
f2.setValue(888);
f3 =
new
Num (777);
System.out.println ("After changing the values:");
System.out.println ("f1\tf2\tf3");
System.out.println (f1 + "\t" + f2 + "\t" + f3 + "\n");
}
}
Search WWH ::
Custom Search