Solved

# Having problems with a specific kind of java problem...

Posted on 2006-05-01
Medium Priority
260 Views
Most of java seems pretty straight forward to me. I am in the learning process right now, and am currently stumped on one specific java question, namely:

1. how do i generate 10 random numbers.
2. then place the random numbers in an array.
3. then print out only the highest value, the lowest value and the average values.

I have been working on this off and on for a few days now, and could use some help.

Thanks,

PKM
0
Question by:marcoullisp
• 4
• 3

LVL 3

Accepted Solution

arun_kuttz earned 2000 total points
ID: 16575784
you could use the following code... the function generateNrndNumbersbetweenXandY() takes 3 integers
as arguments.
First Argument : N => number of integers (size of the Array to be returned.. ur case, 10)
Second Argument : X => random numbers that are generated will be >= X
Third Argument : Y => random numbers that are generated will be < Y

public static void main(String[] args) {
int[] rndArray = generateNrndNumbersbetweenXandY(100, 30, 80);
Arrays.sort(rndArray);
System.out.println("Smallest num : " + rndArray[0]);
System.out.println("Largest num : " + rndArray[rndArray.length - 1]);
float sum = 0;
for (int i = 0; i < rndArray.length; i++) {
sum += rndArray[i];
}
System.out.println("Average : " + (sum/ rndArray.length));
}

public static int[] generateNrndNumbersbetweenXandY(int n, int x, int y) {
int[] retVal = new int[n];
Random rnd = new Random();
for (int i = 0; i < n; i++) {
retVal[i] = rnd.nextInt(y - x) + x;
}
return retVal;
}
0

Author Comment

ID: 16575989
I'm getting errors... hold on let me see if I can clean it up... on first glance there is a problem with:

Arrays.sort(rndArray);

Did you try compiling this, b/'c that line is causing problems...

-Panos
0

LVL 3

Expert Comment

ID: 16575997
yes.. ive compiled and run it..
u might have forgotton to add

import java.util.Arrays;

what errors are u getting ?

-KuTtZ
0

Author Comment

ID: 16576005
never mind i figured it out it was the import you just mentioned... it runs fine, I just don't get what you did... and you know what they say... teach a man to fish...

but seriously, if you could just walk me through this with some comments, thats all I still need.

-PKM
0

Author Comment

ID: 16576011
i awarded the points, because your code works if i only understood it, I would be set...

:-)

PKM
0

LVL 3

Expert Comment

ID: 16576023

public static void main(String[] args) {
int[] rndArray = generateNrndNumbersbetweenXandY(100, 30, 80);
// sort() is a very usefull API provided by the Arrays utility class
// it sorts the given array in place..
Arrays.sort(rndArray);
// The first element of the sorted array will be the smallest int
System.out.println("Smallest num : " + rndArray[0]);
// The last element of the sorted array will be the largest int
System.out.println("Largest num : " + rndArray[rndArray.length - 1]);
float sum = 0;
// Here i find the sum of all the integers in the Array returned by my function
for (int i = 0; i < rndArray.length; i++) {
sum += rndArray[i];
}
// i print out the Average (sun/rndArray.length)
System.out.println("Average : " + (sum/ rndArray.length));
}

public static int[] generateNrndNumbersbetweenXandY(int n, int x, int y) {
int[] retVal = new int[n];
// create a Random object
Random rnd = new Random();
for (int i = 0; i < n; i++) {
// The rnd Object has an API nextInt() which takes an int (lets say i) parameter...
// It will return a random integer between 0(inclusive) and i(exclusive)
// so what i did was get a random integer between 0 and (y-x) and add x to it
// i then keep populating the integer array that ive created with the rand integer
// returned by this API.
retVal[i] = rnd.nextInt(y - x) + x;
}
return retVal;
}

regards
-KuTtZ
0

Author Comment

ID: 16576032
thanks man... you are the best... I will look this over, and my best wishes to you.

-PKM
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.