Hi so I’m working on a program that is designed to take a set of random Gaussian numbers in an array and put them into a textual version of a histogram (not much fun). I still have a long ways to go, but I’m stuck at this point because when I compile and run my code, I recieve an error of java.lang.StackOverflowError. It is referring to my client program that runs the statistics of the numbers (mean, sum, variance, etc.) and says the error occurs in line 44. Here is my code so far:
public class DescriptiveStatistics
{
public static void main(String args[])
{
double data[] = DataSet.getGaussianNumbers(1000);
}
public static double getMinimum()
{
double[] nums = DataSet.getGaussianNumbers(1000);
double min = nums[0];
System.out.println("min value is " + min);
return getMinimum();
}
public static double getMaximum()
{
double[] nums = DataSet.getGaussianNumbers(1000);
double max = nums[nums.length-1];
return getMaximum();
}
public static double getSum()
{
double[] nums = DataSet.getGaussianNumbers(1000);
return getSum();
}
public static double getMean()
{
int n1 = 1;
int n2 = 3;
int n3 = 5;
int n4 = 7;
int n5 = 9;
int sum = n1 + n2 + n3 + n4 + n5;
double mean = (double)sum/5.0;
return getMean();
}
public static double getVariance()
{
int n1 = 1;
int n2 = 3;
int n3 = 5;
int n4 = 7;
int n5 = 9;
int sum = n1 + n2 + n3 + n4 + n5;
double mean = (double)sum/5.0;
double difference1 = (n1 - mean) * (n1 - mean);
double difference2 = (n2 - mean) * (n2 - mean);
double difference3 = (n3 - mean) * (n3 - mean);
double difference4 = (n4 - mean) * (n4 - mean);
double difference5 = (n5 - mean) * (n5 - mean);
double Sigma = difference1 + difference2 + difference3 + difference4 + difference5;
double variance = (0.2 * Sigma);
return getVariance();
}
public static double getStandardDeviation()
{
int n1 = 1;
int n2 = 3;
int n3 = 5;
int n4 = 7;
int n5 = 9;
int sum = n1 + n2 + n3 + n4 + n5;
double mean = (double)sum/5.0;
double difference1 = (n1 - mean) * (n1 - mean);
double difference2 = (n2 - mean) * (n2 - mean);
double difference3 = (n3 - mean) * (n3 - mean);
double difference4 = (n4 - mean) * (n4 - mean);
double difference5 = (n5 - mean) * (n5 - mean);
double Sigma = difference1 + difference2 + difference3 + difference4 + difference5;
double variance = (0.2 * Sigma);
double deviation = Math.sqrt(variance);
return getStandardDeviation();
}
}
I know my arithmetic is messy, but they work. I’m just not sure why I’m getting the error. If there are any other glaring mistakes, please let me know! Thanks!
Every single one of your methods should throw a
StackOverflowError.By returning
getMean()in the methodgetMean(), you are creating infinite recursion. I think you want to be returningmean. (All your other methods do the same thing, so you might want to look into fixing those too.)