Question: Write a class DataSet that stores a number of values of type double. Provide a constructor public DataSet(int maxNumberOfValues) and a method public void addValue(double

Write a class DataSet that stores a number of values of type double. Provide a constructor

public DataSet(int maxNumberOfValues)

and a method

public void addValue(double value)

that add a value provided there is still room.

Provide methods to compute the sum, average, maximum and minimum value.

I need to set my values with the final int LENGTH can you help me rearrange my code so it does that? This has to use enhanced for loops not if else statements ,This is what I got....

import java.util.Scanner; public class DataSet1 { double[] array; int sum; double average; double maximum; double minimum; public DataSet1(int maxNumberOfValues) { array = new double[maxNumberOfValues]; } public void addValue(double value) { for (int i = 0; i < array.length; i++) { sum += array[i]; } } public double getSum(double[] array) { sum = 0; for (int i = 0; i < array.length; i++) { sum += array[i]; } return sum; } public double getAverage() { double total = 0; for (double element : array) { total = total + element; } double average = 0; if (array.length > 0) { average = total / array.length; } return sum / array.length; } public double getMaximum() { double largest = Double.MIN_VALUE; for (int i = 0; i < array.length; i++) { System.out.println(array[i]); largest = Math.max(largest, array[i]); } return largest; } public double getMinimum() { double smallest = Double.MAX_VALUE; for (int i = 0; i < array.length; i++) { smallest = Math.min(smallest, array[i]); } return smallest; } public static void main(String[] args) { final int LENGTH = 100; int currentSize = 0; Scanner in = new Scanner(System.in); System.out.print("Enter amount of numbers to be determined : "); int x = in.nextInt(); DataSet1 d1 = new DataSet1(x); System.out.print("Enter values to be used :"); for(int i=0;i d1.array[currentSize] = in.nextDouble(); currentSize++; } System.out.println("Sum is : " + d1.getSum(d1.array)); System.out.println("Average is :" + d1.getAverage()); System.out.println("Maximum is :" + d1.getMaximum()); System.out.println("Minimum is :" + d1.getMinimum()); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!