?
Solved

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

Posted on 2006-05-01
7
Medium Priority
?
260 Views
Last Modified: 2010-03-31
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
Comment
Question by:marcoullisp
  • 4
  • 3
7 Comments
 
LVL 3

Accepted Solution

by:
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

by:marcoullisp
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

by:arun_kuttz
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:marcoullisp
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

by:marcoullisp
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

by:arun_kuttz
ID: 16576023
Please find my comments inline..


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

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

-PKM
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

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.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month16 days, 5 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question