Why is my code coming out with the wrong output? Is my insert class wrong?

自作多情 提交于 2020-01-07 08:32:20

问题


I have to create classes to be implemented with the main class someone else has and for some reason I am not getting the right outputs, I'm not sure is my calculations are off which I don't think they are or my insert class is wrong.

Expected Output:

Median = 44.5

Mean = 49.300

SD = 30.581

Actual Output:

Median = 0.0

Mean = 0.967

SD = 4.712

public class StatPackage { 
int count; 
double [] scores; 
final int MAX = 500; 


StatPackage() { 
count = 0; 
scores = new double[MAX]; 
} 
public void insert (double value) { 
if (count < MAX){ 
scores[count] = value; 
++ count; 
} 
} 
public double Mean () { 
    double sum = 0; 
    //For loop for calculating average or mean
    for(int i = 0; i < scores.length; i++){
            sum += (scores[i]);
            count++;    
    }
    double average = sum/count;  
    return average;
    } 

public double Median() { 
int min; 
int tmp; 
int size; 

for (int i = 0; i < scores.length - 1; i ++) 
{ 
min = i; 
for (int pos = i + 1; pos < scores.length; pos ++) 
if (scores [pos] < scores [min]) 
min = pos; 

tmp = (int)scores [min]; 
scores [min] = scores [i]; 
scores [i] = tmp; 

} 
double median = 0;
if  (scores.length % 2 == 0){
    median = (scores[scores.length/2-1] + scores[scores.length/2])/2;
}
else {
    median = (scores[((scores.length/2))]);
}
return median;
} 

public double Variance () { 
    double variance = 0;
    double sum = 0;
    //For loop for getting the variance
    for(int i = 0; i < scores.length; i++){
        sum += scores[i];
        variance += scores[i] * scores[i];
        count++;
    }
    double varianceFinal = ((variance/count)-(sum*sum)/(count*count));
    return (varianceFinal);
} 

public double StdDev (double variance) { 
    double sum = 0;
    for(int i = 0; i < scores.length; i++){
        sum += scores[i];
        variance += scores[i] * scores[i];
        count++;
    }
    double varianceFinal = ((variance/count)-(sum*sum)/(count*count));
return Math.sqrt(varianceFinal);

}

}   

回答1:


The length of your scores array is 500, so every time you are using it in a calculation you are running that 500 times. You need to make your loop continuation conditions dependent on the number of values in the array, no the actual length of the array. I would be careful of your variable naming as well, you are using count in two places sometimes and it has global scope! This method stores the number of values in the array in the count variable:

public void insert (double value) { 
    if (count < MAX){ 
        scores[count] = value; 
        ++count; 
    } 
} 

So use the count variable as the loop-continuation condition when you are getting values from the array, like so:

    public double mean() { 

        double sum = 0; 

        //For loop for calculating average or mean
        for(int i = 0; i < count; i++){
            sum += (scores[i]);  
        }

        double average = sum / count;  
        return average;
    } 

That should help a little, I don't have time to check out your other methods but maybe this will give you a good starting place. I figured out what was happening by inserting print statements in your methods to make sure the values were as expected. It's a helpful thing to do when debugging. Your mean() method with the print statements looks like this:

public double mean() { 

    double sum = 0; 

    //For loop for calculating average or mean
    for(int i = 0; i < count; i++){
        sum += (scores[i]);    
    }

    // print statements for debugging
    System.out.println("count is " + count);
    System.out.println("sum is " + sum);

    double average = sum / count;  
    return average;
} 



回答2:


Because the solution is easily found by debugging, I will only give you a hint:

The mean of 3, 4 and 5 is 4: (3+4+5)/3, not (3+4+5)/(n*3) where n is a positive integer.

If you look at your mean and std and divide it by the expected result, you will see it's a rounded number.

Once you find the solution to 1 problem, you will immediately know why the other results are faulty as well =)



来源:https://stackoverflow.com/questions/36389405/why-is-my-code-coming-out-with-the-wrong-output-is-my-insert-class-wrong

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!