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

This is the assignment..... Write a class DataSet that stores a number of values

ID: 3818049 • Letter: T

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 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 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<values.length;i++){
        sum+= values[i];
    }
}
public double getSum(double[] values){
    for(int i=0; i<values.length;i++){
        sum+= values[i];
}
    return sum;
}
public double getAverage(){
double total =0;
for(double element : values){
total = total + element;
}
double average = 0;
if(values.length>0){
average = total/values.length;
}
return sum/values.length;
}
public double getMaximum(){
     double largest = Double.MIN_VALUE;
     for(int i = 1;i<values.length;i++){
     if(values[i]<largest)
     largest=values[i];
    }
    return Math.max(largest, largest);
}
public double getMinimum(){
    double smallest = Double.MAX_VALUE;
    for(int i =0;i<values.length;i++){
    if(values[i]<smallest)
    smallest = values[i];
    }
    return Math.min(smallest, smallest);
}

public static void main(String[] args){
final int LENGTH = 10;
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());
}   
}

Explanation / Answer

import java.util.Scanner;

/**
*
* @author Sam
*/
public class DataSet {

    private double[] values;
    private int count;
    double maximum;
    double minimum;
    double sum;

    public DataSet(int maxNumberOfValues) {
        values = new double[maxNumberOfValues];
        count = 0;
        sum = 0;
    }

    public void addValue(double value) {
        if (count == values.length){
            System.out.println("DataSet full");
            return;
        }
        sum = sum + value;
        values[count++] = value;
        if (count == 1) //when first value is added
            maximum = minimum = value;
        else {
            if (value > maximum)
                maximum = value;
            if (value < minimum)
                minimum = value;
        }
    }

    public double getSum() {
        return sum;
    }

    public double getAverage() {
        if (count > 0) {
            return sum/count;
        }
        return 0; //if no element is present in the array
    }

    public double getMaximum() {
        return maximum;
    }

    public double getMinimum() {
        return minimum;
    }

    public static void main(String[] args) {
        final int LENGTH = 10;
        DataSet d1 = new DataSet(LENGTH);
        Scanner in = new Scanner(System.in);
        System.out.print("Enter number : ");
        while (in.hasNextDouble()) {
            d1.addValue(in.nextDouble());
        }

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

Nice try champ, but your code didn't match the program description provided by you. I updated the code so that it matches OOPs standard. I have commented few lines of the code, and the remaining part of the code is quite simple. I hope that you like the code. Incase you are facing problem understanding the logic, please let me know. I shall be glad to help you with your doubts,

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