Java Solve Confused Java HW: Generate 225 Samples Size 625 Random Nos U(10,22)

AI Thread Summary
The discussion revolves around generating 225 samples of 625 random numbers from a uniform distribution U(10,22) and calculating various statistics, including the mean, standard deviation, and a histogram of the means. The code provided encounters issues with printing results from the probabilityMean method and has problems related to variable naming conventions, specifically the use of uppercase "I" versus lowercase "i," which leads to compilation errors in Java. Suggestions include changing the cast in the probabilityMean method to avoid integer division and ensuring all variable names are consistently lowercase to prevent confusion. Additionally, the importance of defining methods as either static or instance methods is mentioned, with a recommendation to keep them static for simplicity in this context. The conversation also touches on the quirks of code rendering in forums, where certain characters can be misinterpreted, affecting how code is displayed.
Frosty415
Messages
3
Reaction score
0
  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 what's wrong?
package test;

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:
Technology news on Phys.org
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.
 
jedishrfu said:
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.
That's really weird because on netbeans it is all lower case for me.
 
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.
 
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 doesn't 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?
 
Frosty415 said:
That's really weird because on netbeans it is all lower case for me.
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.
 
gabbagabbahey said:
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.
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).
 
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.
 
jedishrfu said:
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.
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
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.
 

Similar threads

Back
Top