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

  • Context: Java 
  • Thread starter Thread starter Frosty415
  • Start date Start date
  • Tags Tags
    Code Confused Java
Click For Summary

Discussion Overview

The discussion revolves around a Java programming assignment that involves generating random samples from a uniform distribution, calculating means, and addressing issues with code execution and variable naming conventions. Participants explore the implementation of statistical calculations and troubleshoot errors in the provided code.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • The original poster (OP) seeks help with generating 225 samples of size 625 from a uniform distribution and calculating various statistics, including the mean and standard deviation.
  • Some participants point out that the OP has not called the method 'probabilityMean' from the main method, which may be causing issues.
  • There is a discussion about variable naming, specifically the use of lowercase 'i' versus uppercase 'I', which can lead to confusion in Java due to case sensitivity.
  • One participant suggests that the OP's casting in the 'probabilityMean' method could be improved by using a double for the denominator instead of casting an integer.
  • Multiple participants report seeing errors related to the use of uppercase 'I' in the code when tested in different IDEs, indicating a potential issue with how the code is being interpreted.
  • There is a mention of formatting issues in the forum post, where the use of 'I' led to unintended italics in the displayed code.
  • A participant shares a humorous aside about a trick involving code on older systems that could mislead teachers, highlighting the playful nature of programming challenges.

Areas of Agreement / Disagreement

Participants generally agree on the issues related to variable naming and method calls, but there is no consensus on the best approach to resolve the errors, as different IDEs yield different results.

Contextual Notes

Limitations include potential misunderstandings due to variable naming conventions and formatting issues in the forum that may affect code readability. The discussion does not resolve the underlying problems with the code execution.

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

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
11
Views
2K