Java Reference
In-Depth Information
*/
public double getSum() { . . . }
. . .
}
Then implement a class
PairTester
that constructs a
Pair
object,
invokes its methods, and prints the results.
Exercise P4.5. Define a class
DataSet
that computes the sum and
average of a sequence of integers. Supply methods
ȗ
void addValue(int x)
ȗ
int getSum()
ȗ
double getAverage()
Hint: Keep track of the sum and the count of the values.
Then write a test program
DataSetTester
that calls
addValue
four
times and prints the expected and actual results.
Exercise P4.6. Write a class
DataSet
that computes the largest and
smallest values in a sequence of numbers. Supply methods
ȗ
void addValue(int x)
ȗ
int getLargest()
ȗ
int getSmallest()
Keep track of the smallest and largest values that you've seen so far. Then
use the
Math.min
and
Math.max
methods to update them in the
addValue
method. What should you use as initial values? Hint:
Integer.MIN_VALUE
,
Integer.MAX_VALUE
.
Write a test program
DataSetTester
that calls
addValue
four times
and prints the expected and actual results.
Exercise P4.7. Write a program that prompts the user for a measurement in
meters and then converts it into miles, feet, and inches. Use a class
public class Converter