Question: This is the assignment..... Write a class DataSet that stores a number of values of type double. Provide a constructor public DataSet(int maxNumberOfValues) and a
This is the assignment.....
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.
This is what I have, its suppose to be using arrays also the double smallest = Double.MAX_VALUE; and double largest = Double.MIN_VALUE; are there so I don't need to create an element. I need to use enhanced for loop, not if else statments. thank you
import java.util.Scanner; public class DataSet1 { double[]values; int sum; double average; double maximum; double minimum; public DataSet1(int maxNumberOfValues){ values = new double[maxNumberOfValues]; } public void addValue(int[]values){ for(int i=0; i public static void main(String[] args){ final int LENGTH = 100; double[]values = new double[LENGTH]; DataSet1 d1 = new DataSet1(LENGTH); int currentSize =0; Scanner in=new Scanner(System.in); System.out.print("Enter number : "); while(in.hasNextDouble()){ values[currentSize]=in.nextDouble(); currentSize++; } System.out.println("Sum is : "+d1.getSum(values)); 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
Get step-by-step solutions from verified subject matter experts
