Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

  1. Declare an array of type int named dataPoints.
  2. dataPoints should have a length of 100. (i.e. it should hold 100 int values.)
  3. 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.
  4. (See notes above for using java.util.Random. Please do not use Math.Random!)
  5. 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):
  6. 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions