1

Hey guys looking for some help with my ArrayTester class... The array is supposed to 1.create a 20 element int array and populates it with random elements in the range 20 through 65 (both inclusive). -I Believe I have already done this.

2.Use the java.util.Random class with a seed of 2621 to generate repeatable output for the data. -I have also already done this

3.The tester class should then test each of these methods(mean, median, mode, max, min, standard divination) -this is where I'm having trouble, I already have the code for the mean, mode, max, min, and standard divination.. the problem I'm having is with the median as i would like to sort my array in the tester class before having it called by my main Class.

Is there any way to do this?

Here is my code:

public class ArrayTest
{
    public static void main(String [] args)
    {
        int[] numbers;
        numbers = new int [20];
        Random rand = new Random(2621); 
        int maxRange = 65;
        int minRange = 20;

        for(int i=0; i<20; i++)
        {
            numbers[i] = rand.nextInt(maxRange - minRange + 1) + minRange;
            //checks to see if printing out ocrrectly
            System.out.println(numbers[i]);

        }
    }
}
2
  • Standard "deviation." "Divination" involves slaughtering small animals and looking at their entrails. Arrays.sort Commented Feb 13, 2013 at 1:10
  • "standard divination": Do you mean "standard deviation"? Commented Feb 13, 2013 at 1:10

1 Answer 1

5

You can use Arrays.sort(numbers);

Sign up to request clarification or add additional context in comments.

4 Comments

I know that but if I put it right before the system.out.println I keep getting an error
You have to do this after the loop, otherwise your array will not be fully populated.
I'm having trouble even with that, as of right now when I put the Arrays.sort(numbers); outside the for loop nothing changes when I outprint the array (the numbers are still printing out, they're just not sorted) This made me believe that I had to also move my System.out.println(numbers[i]) outside the for loop as well. However when I do this I get an error saying that it cannot find the variable [i]
You will need 2 loops, fist to generate the random numbers which you already have and then callArrays.sort and then another loop to print the array again which will be now sorted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.