# Java Confused java HW code

1. Apr 24, 2015

### Frosty415

1. Generate 225 samples of size 625 random numbers from U(10,22). For each of these 225 samples calculate the mean.
a) Find the simulated probability that the mean is between 10 and 11.

b) Find the mean of the means.

c) Find the standard deviation of the means.

d) Draw the histogram of the means.

Here's my code: I am unable to print from my method of probabilityMean. Anyone know whats wrong?
package test;

Code (Java):

import java.util.Random;

public class test {

public static double average(int[] x) {

double sum = 0;

for (int j = 0; j < x.length; j++) {
sum += x[j];
}

double average = sum / x.length;

return average;

}

public static double average(double[] x) {

double sum = 0;

for (int j = 0; j < x.length; j++) {
sum += x[j];
}

double average = (sum) / x.length;

return average;

}

public static double probabilityMean(double[] x) {
int count = 0;
double avg = average(x);
if (avg >= 10 && avg <= 11) {
count++;
}
double probab = (float) count / 625;
return probab;
}

public static double standardDeviation(double[] values) {

double deviation = 0.0;

if ((values != null) && (values.length > 1)) {

double mean = average(values);

for (double value : values) {

double delta = value - mean;

deviation += delta * delta;

}

deviation = Math.sqrt(deviation / values.length);

}

return deviation;

}

public static void main(String[] args) {
Random rand = new Random();

double finalmean, sd;

double[] mean = new double[225];

int[][] x = new int[225][625];
for (int j = 0; j < 225; j++) {
for (int k = 0; k < 625; k++) {
x[j][k] = rand.nextInt((22 - 10) + 1) + 10;
}
mean[j] = average(x[j]);
System.out.println("Mean of array" + j + " is " + mean[j]);

}

finalmean = average(mean);

sd = standardDeviation(mean);

System.out.println("Mean of means:" + finalmean + "\nStandard deviation:" + sd);
}

}

Mod edit: To deal with the problem of i index turning to I and being treated as italics tags, I changed the index i to j, and in one method with both i and j, changed i to j and j to k.

Last edited by a moderator: Apr 25, 2015
2. Apr 24, 2015

### Staff: Mentor

You defined it but where are you calling it from?

Also I edited your post adding code tags for pretty printing it.

I also noticed that in your main you have the variable lowercase I for the for loop and an uppercase I for indexing your array. Remember in Java, the variable case name is important, so Java thinks They are two different variables.

3. Apr 24, 2015

### Frosty415

That's really weird because on netbeans it is all lower case for me.

4. Apr 24, 2015

### Frosty415

Also, I tried calling it into the main method but kept receiving errors. I deleted it on the code provided because of the errors. I wanted to see if anyone was able to call upon the method and print from it. not sure if the method is wrong or I'm calling it wrong.

5. Apr 25, 2015

### Staff: Mentor

So what did you do and what are the errors youre seeing?

From your code i see all your methods are static meaning they are class methods not instance methods. Flexibility is improved if you make them instance methods but since the class doesnt define any variable then its okay for now.

I did notice your cast in the probabilityMean where youre doing an integer divide casting it as a float and then assigning it to a double. Doesn't that strike you as odd? Why not make the 625 a double ie 625.0 and drop the cast?

6. Apr 25, 2015

### gabbagabbahey

not sure about netbeans, but when I paste your code into eclipse I see 6 error messages relating to upper case "I" being used. When I change the 6 upper case "I"s to lower case I am able to compile and run your Class and the results seem pretty reasonable.

7. Apr 25, 2015

### Staff: Mentor

It could be that the OP typed i, but some spell-check setting somewhere is assuming that it really should be I (upper-case i).

8. Apr 25, 2015

### Staff: Mentor

Yeah, I wasn't sure what happened. I edited the post to add code tags and the end listing showed up with italics tags that's the [ ] with a /I inside which appeared to come from the usage of the I for indexing.

9. Apr 25, 2015

### Staff: Mentor

I think you're right. Inside a pair of code tags, if you use i as the index into an array, the BBCode rendering part of things thinks you meant [ I ], for italics. Even if you use lower case i, it still thinks this is an italics tag, and capitalizes the letter i. As far as I know, there's no workaround other than to not use i as an index in arrays.

10. Apr 25, 2015

### Staff: Mentor

Wow software bugs within bugs. This would make a great sci-fi story plot. The listing you see is not the code that is running.

Aside:

I saw code like this on a Commodore 64 to fake out the teacher. The student would call the teacher over and say my programs not working. Listing it would show something real simple but when run a different answer is shown. Unbeknownst to the teacher was that the real program is actually responding to the list and run typed input and displaying faked output.

It's a great trick to play naive computer science teachers.