Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

ID: 3823982 • Letter: W

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 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());
}
}

Explanation / Answer

Hi, I have organized all of your code.

Please let me know in case of any issue.

import java.util.Scanner;

public class DataSet1 {

   private double[] array;

   private int size;

   public DataSet1(int maxNumberOfValues) {

       size = 0;

       array = new double[maxNumberOfValues];

   }

   public void addValue(double value) {

       if(size < array.length)

           array[size++] = value;

   }

   public double getSum() {

       double sum = 0;

       for (int i = 0; i < size; i++) {

           sum += array[i];

       }

       return sum;

   }

   public double getAverage() {

       double total = 0;

       for (double element : array) {

           total = total + element;

       }

       return total/size;

   }

   public double getMaximum() {

       double largest = Double.MIN_VALUE;

       for (int i = 0; i < size; i++) {

           largest = Math.max(largest, array[i]);

       }

       return largest;

   }

   public double getMinimum() {

       double smallest = Double.MAX_VALUE;

       for (int i = 0; i < size; i++) {

           smallest = Math.min(smallest, array[i]);

       }

       return smallest;

   }

   public static void main(String[] args) {

      

       final int LENGTH = 100;

       Scanner in = new Scanner(System.in);

      

       // creating Object

       DataSet1 d1 = new DataSet1(LENGTH);

      

       System.out.print("Enter amount of numbers to be determined : ");

       int x = in.nextInt();

      

       System.out.println("Enter values to be used :");

      

       for(int i=0; i<x; i++){

          

           double val = in.nextDouble();

           d1.addValue(val);

       }

          

       System.out.println("Sum is : " + d1.getSum());

       System.out.println("Average is :" + d1.getAverage());

       System.out.println("Maximum is :" + d1.getMaximum());

       System.out.println("Minimum is :" + d1.getMinimum());

      

       in.close();

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote