Java Reference
In-Depth Information
public static double
[] differenceArray(
double
[] argumentArray,
int
numberUsed,
double
adjustment)
{
double
[] temp =
new double
[numberUsed];
for
(
int
i = 0; i < numberUsed; i++)
temp[i] = argumentArray[i] - adjustment;
return
temp;
}
14. The only changes are to add the method
differenceArray
and to rewrite the
method
showDifference
as follows (the complete class definition is in the file
GolfScoresExercise.java
on the accompanying website):
extra code
on website
public static void
showDifference(
double
[] a,
int
numberUsed)
{
double
average = computeAverage(a, numberUsed);
System.out.println("Average of the " + numberUsed
+ " scores = " + average);
double
[] difference =
differenceArray(a, numberUsed, average);
System.out.println("The scores are:");
for
(
int
index = 0; index < numberUsed; index++)
System.out.println(a[index] +
" differs from average by "
+ difference[index]);
}
15. The main differences are to remove parameters, replace the array name
a
by
score
, and make the method
fillArray
a
void
method. This code is in the file
GolfScoresStaticExercise.java
on the accompanying website.
extra code
on website
import
java.util.Scanner;
public class
GolfScoresStaticExercise
{
public static final int
MAX_NUMBER_SCORES = 10;
private static double
[] score =
new double
[MAX_NUMBER_SCORES];
private static int
numberUsed = 0;
/**
Shows differences between each of a list of golf scores
and their average.
*/
public static void
main(String[] args)
{