•• E7.9 Write a class DataSet that stores a number of values of type double. Pro
ID: 3875194 • Letter: #
Question
•• E7.9 Write a class DataSet that stores a number of values of type double. Provide a constructor
public DataSet(int maximumNumberOfValues)
and a method
public void add(double value)
that adds a value, provided there is still room.
Provide methods to compute the sum, average, maximum, and minimum value.
/**
* Code for E7.9
* @author
*/
public class DataSet
{
// Implmentation
. . .
/**
Constructs an empty data set.
@param maximumNumberOfValues the maximum this data set can hold
*/
public DataSet(int maximumNumberOfValues)
{
. . .
}
/**
Adds a data value to the data set if there is room in the array.
@param value a data value
*/
public void add(double value)
{
. . .
}
/**
Gets the sum of the added data.
@return sum of the data or 0 if no data has been added
*/
public double getSum()
{
. . .
}
/**
Gets the average of the added data.
@return average of the data or 0 if no data has been added
*/
public double getAverage()
{
. . .
}
/**
Gets the maximum value entered.
@return maximum value of the data
NOTE: returns -Double.MAX_VALUE if no values are entered.
*/
public double getMaximum()
{
. . .
}
/**
Gets the minimum value entered.
@return minimum value of the data
NOTE: returns Double.MAX_VALUE if no values are entered.
*/
public double getMinimum()
{
. . .
}
}
-----
Here are some ran tests for the Data Set
/**
Run some tests for the DataSet class
*/
public class DataSetTester
{
public static void main(String[] args)
{
DataSet data = new DataSet(5);
data.add(3.5);
data.add(7.9);
data.add(15.2);
data.add(-7.3);
System.out.println("Sum: " + data.getSum());
System.out.println("Expected: 19.3");
System.out.println("Average: " + data.getAverage());
System.out.println("Expected: 4.825");
System.out.println("Maximum: " + data.getMaximum());
System.out.println("Expected: 15.2");
System.out.println("Minimum: " + data.getMinimum());
System.out.println("Expected: -7.3");
}
}
Explanation / Answer
/**
* Code for E7.9
*
* @author
*/
public class DataSet {
// Implmentation
double[] data;
int pos;
/**
* Constructs an empty data set.
*
* @param maximumNumberOfValues
* the maximum this data set can hold
*/
public DataSet(int maximumNumberOfValues) {
data = new double[maximumNumberOfValues];
pos = 0;
}
/**
* Adds a data value to the data set if there is room in the array.
*
* @param value
* a data value
*/
public void add(double value) {
if (pos < data.length) {
data[pos] = value;
pos++;
}
}
/**
* Gets the sum of the added data.
*
* @return sum of the data or 0 if no data has been added
*/
public double getSum() {
double sum = 0;
for (int i = 0; i < pos; i++) {
sum += data[i];
}
return sum;
}
/**
* Gets the average of the added data.
*
* @return average of the data or 0 if no data has been added
*/
public double getAverage() {
return getSum() / pos;
}
/**
* Gets the maximum value entered.
*
* @return maximum value of the data NOTE: returns -Double.MAX_VALUE if no
* values are entered.
*/
public double getMaximum() {
if (data.length == 0)
return Double.MAX_VALUE;
else {
double max = data[0];
for (int i = 1; i < pos; i++) {
if (max < data[i])
max = data[i];
}
return max;
}
}
/**
* Gets the minimum value entered.
*
* @return minimum value of the data NOTE: returns Double.MAX_VALUE if no
* values are entered.
*/
public double getMinimum() {
if (data.length == 0)
return Double.MAX_VALUE;
else {
double min = data[0];
for (int i = 1; i < pos; i++) {
if (min > data[i])
min = data[i];
}
return min;
}
}
}
/**
* Run some tests for the DataSet class
*/
public class DataSetTester {
public static void main(String[] args) {
DataSet data = new DataSet(5);
data.add(3.5);
data.add(7.9);
data.add(15.2);
data.add(-7.3);
System.out.println("Sum: " + data.getSum());
System.out.println("Expected: 19.3");
System.out.println("Average: " + data.getAverage());
System.out.println("Expected: 4.825");
System.out.println("Maximum: " + data.getMaximum());
System.out.println("Expected: 15.2");
System.out.println("Minimum: " + data.getMinimum());
System.out.println("Expected: -7.3");
}
}
OUTPUT:
Sum: 19.3
Expected: 19.3
Average: 4.825
Expected: 4.825
Maximum: 15.2
Expected: 15.2
Minimum: -7.3
Expected: -7.3
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.