-1

For some reason the size of my array is invalid and I am not sure how I should change NextIndex to fix this. The size should be dynamic and changing NextIndex to 1 doesn't help it.

 /**
 * Code for E7.9
 * @Michael Goedken
 */


public class DataSet
{
   // Implmentation

int nextIndex = 0;

 double[] list = new double[1];

 public static void main (String args[])
 {
      DataSet data = new DataSet(5);
      data.add(3.5);
      data.add(7.9);
      data.add(15.2);
      data.add(-7.3);

      System.out.println("Sum: " + data.getSum());
      System.out.println("Expected: 19.3");
      System.out.println("Average: " + data.getAverage());
      System.out.println("Expected: 4.825");
      System.out.println("Maximum: " + data.getMaximum());
      System.out.println("Expected: 15.2");
      System.out.println("Minimum: " + data.getMinimum());
      System.out.println("Expected: -7.3");
     }

Constructs an empty data set. @param maximumNumberOfValues the maximum this data set can hold

   public DataSet(int maximumNumberOfValues)
   {
       double[] list = {maximumNumberOfValues};

   }

Adds a data value to the data set if there is room in the array. @param value a data value

   public void add(double value)
   {

       list[nextIndex] = value;
       nextIndex++;

   }

Gets the sum of the added data. @return sum of the data or 0 if no data has been added

   public double getSum()
   {
      double sum = 0;

  for (double i : list)
        sum += i;

  return sum;
       }

Gets the average of the added data. @return average of the data or 0 if no data has been added

   public double getAverage()
   {
       double sum = 0;
       double avg = 0;

          for (double i = 0;i<list.length;i++)
              sum += i;

          avg = sum/list.length;

          return avg;
       }

Gets the maximum value entered. @return maximum value of the data NOTE: returns -Double.MAX_VALUE if no values are entered.

   public double getMaximum()
   {
      double max = list[0];

  for (int i = 1; i < list.length; i++)
  {
      if (list[i] > max)
      {
          max = list[i];
      }
  }

  return max;

   }

Gets the minimum value entered. @return minimum value of the data NOTE: returns Double.MAX_VALUE if no values are entered.

   public double getMinimum()
   {
       double min = list[0];

      for (int i = 1; i < list.length; i++)
      {
          if (list[i] < min)
          {
              min = list[i];
          }
      }

      return min;
   }
}
UmarZaii
  • 1,315
  • 1
  • 12
  • 25
MikeG
  • 15
  • 4

4 Answers4

1
public DataSet(int maximumNumberOfValues) {
  double[] list = {maximumNumberOfValues}; 
}

Here, list s a local variable declaration (and only has one element), which has nothing to do with the member variable of the same name.

Also this expression {maximumNumberOfValues} actually means "create an array with a single element with value maximumNumberOfValues"

Assign a value to the member variable instead, where that value is a new array with the desired number of elements:

list = new double[maximumNumberOfValues];
Zilvinas
  • 5,065
  • 3
  • 21
  • 24
Andy Turner
  • 122,430
  • 10
  • 138
  • 216
1

Change

int nextIndex = 0;

double[] list = new double[1];

to

int nextIndex = 0;

double[] list;

Change your constructor from

public DataSet(int maximumNumberOfValues)
{
    double[] list = {maximumNumberOfValues};
}

to

public DataSet(int maximumNumberOfValues)
{
    list = new double[maximumNumberOfValues];     
}
jmarkmurphy
  • 9,829
  • 28
  • 51
0

double[] list = {maximumNumberOfValues}; That's not how you create and allocate array in Java with the size of maximumNumberOfValues.

You should allocate it like this: double[] list = new double[maximumNumberOfValues];

Update: Also, double[] list = {maximumNumberOfValues}; should be declared like this in the constructor: list = new double[maximumNumberOfValues], otherwise you are declaring a local variable. You should declare list as private outside of your constructor.

Ervin Szilagyi
  • 2,393
  • 2
  • 10
  • 20
0

I am not quite sure what your problem is.

int nextIndex = 0;

double[] list = new double[1];

I might be wrong... but your array is always the size of 1. And as Ervin Szaligyi said -> you have to create the array differently

cmprinho
  • 57
  • 11