showSums
Category: Token-Based File Processing
Author: Stuart Reges
Book Chapter: 6.2
Problem: showSums
Write a static method called
showSums that takes as a parameter a Scanner containing a sequence of
integers and that reports each cumulative sum of the sequence and the
average of the numbers. For example, if the following text is stored in a
Scanner called data:
8 4 13 5 9
and we make the following call:
showSums(data);
your method should produce the following output:
Sum of 1 = 8
Sum of 2 = 12
Sum of 3 = 25
Sum of 4 = 30
Sum of 5 = 39
Average = 7.8
Notice that the various lines of output report the sum including just the
first number, then including the first two numbers, then including the first
three numbers, and so on, up to the sum including all numbers. The final
line of output reports the average of the sequence. Notice that this is the
average of the numbers themselves, not the average of the cumulative sums.
If the Scanner contains the following values:
1 2 3 4
the method should produce the following output:
Sum of 1 = 1
Sum of 2 = 3
Sum of 3 = 6
Sum of 4 = 10
Average = 2.5
You are to exactly reproduce the format of these sample outputs. You may
assume that the Scanner has at least one integer to be processed.