Java Reference
In-Depth Information
Display 6.4
Partially Filled Array (part 3 of 3)
65 /**
66 Precondition: numberUsed <= a.length.
67 The first numberUsed indexed variables of a have values.
68 Postcondition: Gives screen output showing how much each of the first
69 numberUsed elements of the array a differ from their average.
70 */
71 public static void showDifference( double [] a, int numberUsed)
72 {
73 double average = computeAverage(a, numberUsed);
74 System.out.println("Average of the " + numberUsed
75 + " scores = " + average);
76 System.out.println("The scores are:");
77 for ( int index = 0; index < numberUsed; index++)
78 System.out.println(a[index] + " differs from average by "
79 + (a[index] - average));
80 }
81 }
Sample Dialogue
This program reads golf scores and shows
how much each differs from the average.
Enter golf scores:
Enter up to 10 nonnegative numbers.
Mark the end of the list with a negative number.
69 74 68 -1
Average of the 3 scores = 70.3333
The scores are:
69.0 differs from average by -1.33333
74.0 differs from average by 3.66667
68.0 differs from average by -2.33333
Self-Test Exercises
13. Complete the defi nition of the following method that could be added to the
class GolfScores in Display 6.4 :
/**
Precondition: numberUsed <= argumentArray.length;
the first numberUsed indexed variables of argumentArray
have values.
Returns an array of length numberUsed whose ith element
is argumentArray[i] - adjustment.
*/
public static double [] differenceArray(
double [] argumentArray, int numberUsed, double adjustment)
(continued)
 
Search WWH ::




Custom Search