Question

In: Computer Science

Write a class DataSet that stores a number of values of type double in an array...

Write a class DataSet that stores a number of values of type double in an array list. Provide methods to insert a value to the array list and to compute the sum,
average, and maximum value.

Please put a note on what you did is for what. So I can understand this question better.

Code provided:

import java.util.*;

public class DataSet
{
   private ArrayList<Double> data;

/**
Constructs an empty data set.
@param maximumNumberOfValues the maximum this data set can hold
*/
public DataSet()
{
   data = new ArrayList<Double>();
}

/**
Adds a data value to the data set if there is a room in the array.
@param value a data value
*/
public void add(double value)
{
   // COMPLETE THIS METHOD
}

/**
Gets the sum of the added data.
@return sum of the data or 0 if no data has been added
*/
public double getSum()
{
   // COMPLETE THIS METHOD
}

/**
Gets the average of the added data.
@return average of the data or 0 if no data has been added
*/
public double getAverage()
{
   // COMPLETE THIS METHOD
}

/**
Gets the maximum value entered.
@return maximum value of the data
NOTE: returns -Double.MAX_VALUE if no values are entered.
*/
public double getMaximum()
{
   // COMPLETE THIS METHOD
}

}

Solutions

Expert Solution

/*DataSet.java*/

import java.util.*;

public class DataSet
{
private ArrayList<Double> data;

/**
Constructs an empty data set.
@param maximumNumberOfValues the maximum this data set can hold
*/
public DataSet()
{
data = new ArrayList<Double>();
}

/**
Adds a data value to the data set if there is a room in the array.
@param value a data value
*/
public void add(double value)
{
data.add(value); // add value to arraylist
}

/**
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.0;
if(data.size() < 0) return 0; // 0 if no data has been added
for (int i = 0; i < data.size(); i++) {//run until array list size
  
sum+=data.get(i); // calc sum
  
}
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()
{
if(data.size() < 0)return 0;//0 if no data has been added
return getSum()/data.size();// find avg
}

/**
Gets the maximum value entered.
@return maximum value of the data
NOTE: returns -Double.MAX_VALUE if no values are entered.
*/
public double getMaximum()
{
double max = data.get(0);
if(data.size() < 0){ //Double.MAX_VALUE if no data has been added
return Double.MAX_VALUE;
}
for (int i = 0; i < data.size(); i++) {
if(max < data.get(i)){
max = data.get(i); // assign max element to max
}
}
return max;
  
}

}


/*Driver class */

/* Main.java*/

public class Main {

public static void main(String[] args) {
DataSet obj = new DataSet();
for (int i = 0; i < 5; i++) {
obj.add((i+1));
}
System.out.println("Sum: "+obj.getSum());
System.out.println("Average: "+obj.getAverage());
System.out.println("Maximum: "+obj.getMaximum());
}
}


Related Solutions

Write a function that will take in an array (of type double), and will return the...
Write a function that will take in an array (of type double), and will return the array with all of the elements doubled. You must use pass-by-reference and addressing and/or pointers to accomplish this task. C++
I need to write a function the will take in an array (of type double), and...
I need to write a function the will take in an array (of type double), and will return the array with all of the elements doubled while using pass-by-reference and addressing and/or pointers. This is what i have so far, but im messing up in line 31 where i call the function i believe so im not returning the correct array? please help edit. #include <iostream> #include <iomanip> using namespace std; double *doubleArray ( double arr[], int count, int SIZE);...
Write a method that, given an array of grades stored as double values, computes and return...
Write a method that, given an array of grades stored as double values, computes and return their mean.    Write another method that will return an array of the mean grade obtained by an arbitrary number of student.    This method will take a 2-dimensional array of double values in which each row corresponds to the grade vector    for a different student. You can compute the mean for each row as you did before...    Once you are done,...
(C++ only please) Write a function called maximum that takes an array of double values as...
(C++ only please) Write a function called maximum that takes an array of double values as a parameter and returns the largest value in the array. The length of the array will also be passed as a parameter. (Note that the contents of the array should NOT be modified.) Write a function called printReverse that takes an array of characters and the length of the array as parameters. It should print the elements of the array in reverse order. The...
Write a Java program to randomly create an array of 50 double values. Prompt the user...
Write a Java program to randomly create an array of 50 double values. Prompt the user to enter an index and prints the corresponding array value. Include exception handling that prevents the program from terminating if an out of range index is entered by the user. (HINT: The exception thrown will be ArrayIndexOutOfBounds) *Please do it in eclipse and this is java language*
in java Write a while loop to ask the user to type number of hours(double) they...
in java Write a while loop to ask the user to type number of hours(double) they work per day. Calculate and print the salary for each valid input. If number of hours is greater than or equal to 0 and less than 5, then:  salary = numberofhours * 5, loop continues, the user can type another number If number of hours is greater or equal to 5, and less than 10, then: salary = numberofours * 8, loop continues, the user...
Write a program that takes its input from a file of number type double and outputs...
Write a program that takes its input from a file of number type double and outputs the average of the numbers in the file to the screen. The file contains nothing but numbers of the type double separated by blanks and/ or line breaks. If this is being done as a class assignment, obtain the file name from your instructor. File name: pr01hw05input.txt 78.0 87.5 98.1 101.0 4.3 17.2 78.0 14.5 29.6 10.2 14.2 60.7 78.3 89.3 29.1 102.3 54.1...
Write a while loop to ask the user to type number of hours(double) they work per...
Write a while loop to ask the user to type number of hours(double) they work per day. Calculate and print the salary for each valid input. If number of hours is greater than 0 and less than 5, then:  salary = numberofhours * 12, loop continues, the user can type another number If number of hours is greater or equal to 5, and less than 12, then: salary = numberofours * 14, loop continues, the user can type another number If...
Write a class named GasTank containing: An instance variable named amount of type double, initialized to...
Write a class named GasTank containing: An instance variable named amount of type double, initialized to 0. An instance variable named capacity of type double. A constructor that accepts a parameter of type double. The value of the parameter is used to initialize the value of capacity. A method named addGas that accepts a parameter of type double. The value of the amount instance variable is increased by the value of the parameter. However, if the value of amount is...
Write a function in C# that takes an array of double as the parameter, and return...
Write a function in C# that takes an array of double as the parameter, and return the average
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT