Question
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
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 ArrayStuff with a single java main class also called ArrayStuff. 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.
- 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.
- (See notes above for using java.util.Random. Please do not use Math.Random!)
- 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.Don't just print out the average and sum as a raw numbers.
After the first step I am confused on how to set up the different for loops
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started