To thoroughly explore Javas array and arrayList collections. Learn about random numbers in java.


We use java.util.Random() to generate random numbers. We have to include the import:

Import java.util.Random;

Create a random object:

Random rnd = new Random();
int val = rnd.nextInt(100); // gives a value from 0 to 99
int val = rnd.nextInt(100) + 1; // gives a value from 1 to 100
int index = rnd.nextInt(myArray.length); gives a random valid index on myArray

Begin the lab:

Create a netbeans project called Array with a single java main class also called Array. All the code for the lab you will do today is in this single main class. Implement each problem in the main class and test it carefully as you go. In particular, watch out for off by one errors!


  • Declare an array of type int named dataPoints. dataPoints should have a length of 100. (i.e. it should hold 100 int values.)
  • Now, code a regular for loop that iterates through the dataPoints array and initializes each element in it to a random value between 1 and 100.
  • Code a second loop that displays the dataPoints values like this (values are all on the same line and separated by | e.g. space, vertical bar, space): val1 | val2 | val3 | HINT: use printf or System.out.print()
  • Now code a loop that calculates the sum and the average of the values in dataPoints. Add code to output/display the sum and the calculated average. (Be sure to include some sort of description in the output so it is clear (i.e. use complete sentences) : The average of the random array dataPoints is: or something similar. Dont just print out the average and sum as a raw numbers. (Do this for all the output here)

Paste the screen shot here

2.Linear scan (search):

  • Add code to prompt and input an int value between 1 and 100 from the user. (Use your static getRangedInt() method from the last lab here! Just copy it to this file.)
  • Code a loop that iterates through the entire dataPoints array and counts how many times the users value is found within the array. Indicate with a clear output statement how many times the loop found the users value within the array.

Paste the screen shot of the output here:

  • Prompt the user again for a val between 1 and 100. Code a loop that iterates through the dataPoints array checking each value to see if it matches the one the user input. This loop should short circuit (break) when it finds the value and display the first position of the value within the array. Again, make the output clear and explicit here. (e.g. The value VALUE was found at array index POSITION where VALUE is the users value you input and POSITION is the location (index) where the value is found in the array. If the value is not in the array, then the output statement should indicate that. Note you can use the break keyword to jump out of a loop before it completes. The problem here is that you have to keep track of whether or not the value was found at all.

Paste the screen shot here

  • Write a loop to scan for the minimum and maximum values in the dataPoints array. Display these values to the user. (Note that these might not be unique, there might be several occurrences of either the min or the max value in the random array.) Hint: you can assume initially that the first element is the min and max and iterate through the rest of the array to determine if there is a lower or higher value which then becomes the min or max respectively.

Paste the screen shot here

  • Write this method and test it:
public static double getAverage(int values[])
  • In your main code call it like this:
System.out.printLn(Average of dataPoints is: + getAverage(dataPoints));

Again, show the output here:

3.Extra Credit

Refactor you program and create the following static array methods. Recode your main method so you can compare the values returned by your inline code with the equivalent methods:

public static int min(int values[]) Returns the min value found
public static int max(int values[]) Returns the max value found
public static int occuranceScan(int values[], int target)
Returns the number of times target is found in the array
public static int sum(int values[]) Returns the sum of the array elements
public boolean contains(int values[], int target) Returns true if the array contains target

4.Combine this word file and your complete Netbeans project folder into a single zip archive file and name it Lastname_Firstname_Lab9.zip. Submit your file archive using the Bb Assignment link. Just resubmit the entire assignment again with the extra credit link if you did the extra credit.

Academic Honesty!
It is not our intention to break the school's academic policy. Projects posted are only used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly fill out the form. Please provide a valid email address and we'll get back to you in less than 24 hours. We will be sending an invoice through PayPal upon confirmation. We are a non profit organization however we need an amount to keep this organization running, and to be able to complete our research and development.