Exercise 5.8
- What is wrong with each method?
(a) public static int largest (int a, int b)
{
// return the larger of the two parameter values
if (a > b)
return a;
else if (b > a)
return b;
}
(b) public static void applylnterest (double amount, double i)
{
// apply interest to amount at rate i%
amount *= 1 + i/l00;
return;
}
(c) public static double cubeRoot (double x)
{
// return the cube root of the parameter, x
System.out.println("What is value?")
x = In.getDouble();
return math.pow(x,1.0/3.0);
}
- Add statements to the method range that could be used to trace its actions.
public static int range (int n)
{
int low, high;
low = high In.getInt();
for (int i 2; i <= n; i++)
{
int next = In.getInt();
if (next>high)
high = next;
if (next < low)
low = next;
}
return high - low;
}
| |