Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11
at DataSet.<init>(DataSet.java:50)
at DataSetClient.main(DataSetClient.java:43)
This is the error I keep getting with my code It is arrays and constructors being used, I cant find the error to correct it. I am using Eclipse by the way below is my main and my constructor class.
import java.util.Arrays;
import java.util.Random;
public class DataSetClient
{
public static void main(String[] args)
{
Random generator = new Random();
double[] randomData = new double[ generator.nextInt(4) + 10 ];
for (int index = 0; index < randomData.length; index++)
randomData[index] = Math.round(generator.nextDouble() * 10000) / 100.0;
System.out.println("============ Arrays Demo ================================");
double[] sortedData = Arrays.copyOf(randomData, randomData.length);
Arrays.sort( sortedData );
System.out.println( "Random\n" + Arrays.toString(randomData) );
System.out.println( "Sorted\n" + Arrays.toString(sortedData) );
System.out.println();
System.out.println("============ DataSet Test ================================");
DataSet analyzer = new DataSet( randomData );
System.out.println(analyzer.size() + " Data Items");
System.out.println( Arrays.toString( analyzer.getData() ) );
System.out.println( "Minimum: " + analyzer.minimum() +
" at index " + analyzer.indexOfMin() );
System.out.println( "Maximum: " + analyzer.maximum() +
" at index " + analyzer.indexOfMax() );
System.out.println( "Data Range: " + analyzer.range() );
System.out.println( "Median : " + analyzer.median() );
System.out.println( "Mean : " + analyzer.mean() );
System.out.println( "Variance : " + analyzer.variance() );
System.out.println( "Std Dev : " + analyzer.standardDeviation() );
}
}
import java.util.Arrays;
public class DataSet
{
//Instance Data
private double[] data;
private int indexMax = 0;
private double maximum = 0.0;
private int indexMin = 0;
private double minimum = 0.0;
private double median = 0.0;
private double mean = 0.0;
private double variance = 0.0;
//Constructor: Create a new DataSet
// Parameter : data points to be included into this DataSet
// Exception : the number of data points must be at least 2
public DataSet(double[] data)
{
if (data.length < 2)
throw new RuntimeException("Data points must be at least 2");
else
this.data = data;
//find index of max, indexof min and mean
double sum = 0;
for(int i = 0; i < data.length; i++)
{
if(data[i] > data[i+1] )
indexMax = i;
else if(data[i] < data[i+1] )
indexMin = i;
sum = sum + data[i];
}
mean = sum / data.length;
// find median
double[] sortedData = Arrays.copyOf(data, data.length);
Arrays.sort( sortedData );
int medianPosition;
medianPosition = data.length / 2;
median = data[medianPosition];
//Find variance
double difference= 0.0;
for(int in = 0; in < data.length; in++)
{
difference = difference + (data[in] - mean) * (data[in] - mean);
}
variance = difference / data.length;
}
//Return the number of data-points in this DataSet
public int size()
{
return data.length;
}
//Accessor : Return copies of the data-points in this DataSet
public double[] getData()
{
return data;
}
// position of the biggest value
//Return the index of the largest point in this DataSet
public int indexOfMax()
{
return indexMax;
}
//same as indexMas
//Return the value of the largest point in this DataSet
public double maximum()
{
return maximum = data[indexMax];
}
//Return the index of the smallest point in this DataSet
public int indexOfMin()
{
return indexMin;
}
//Return the value of the smallest point in this DataSet
public double minimum()
{
return minimum = data[indexMin];
}
//Return the difference between the largest and smallest
// points in this DataSet
public double range()
{
return maximum - minimum;
}
//Return the value of the median point in this DataSet
public double median()
{
return median;
}
//Return the average value of the points in this DataSet
public double mean()
{
return mean;
}
//difference bettween the item and the mean add them and then divide
//Return the variance of the points in this DataSet
public double variance()
{
return variance;
}
//Return the standard deviation of the points in this DataSet
public double standardDeviation()
{
return variance / variance;
}
}