I need to write an array with enhanced for loops to get, sum, average, max and min [on hold]

213
April 18, 2017, at 5:16 PM

I have this code and my question is why isn't my LENGTH not being used? I have been working on this for a bit. I need to "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

import java.util.Scanner
class DataSet1 {
    double[] array;
    int sum;
    double average;
    double maximum;
    double minimum;
    public DataSet1(int maxNumberOfValues) {
        array = new double[maxNumberOfValues];
    }
    public void addValue(int[] array) {
        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 < x; 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());
    }
}
READ ALSO
will two war with same package name under Tomcat interfere each other?

will two war with same package name under Tomcat interfere each other?

I've a question that if I export a spring project twice, and give them different names, then deploy to the server(exTomcat8), will these two web app interfere each other? (due to they're same project and having same package name) or will Tomcat deploy...

140
JavaFx stage.close() not working

JavaFx stage.close() not working

Blockquote Can be compiled, but when I click the EXIT button, it shows that the error as: Exception in thread "JavaFX Application Thread" javalang

344
How to compute mean of an 4D array individually?

How to compute mean of an 4D array individually?

I have an array img with shape is 64x128x512x3 that is concated from three images 64x128x512I want to compute mean of each image individually, given by the array img

239
How to create a connected graph in networkx

How to create a connected graph in networkx

I want to create a connected graph in ipython notebook through networkxPreviously, I use

269