Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java: I have to write a program that prompts the user for 3 numbers (length (how

ID: 3680268 • Letter: J

Question

Java: I have to write a program that prompts the user for 3 numbers (length (how many times/numbers), a min, and a max. Then randomly generates numbers (however many times the user specifies under length) between the min and max numbers that the user specifies. Then I am supposed to display the array and the average of the rand generated numbers. I am stuck and have been trying to figure out how to go about it. What am I doing wrong? I know it needs a loop, but can't figure out where to go next. Any suggestions would be greatly appreciated! This is what I have so far:

import java.util.Scanner; //Importing Java utility scanner so that it recognizes information to be entered by user
import java.lang.Math; //Importing Math language so it will understand and correctly use Math verbage
import java.util.Random; // Importing Java utility in order to utilize the random number generator

public class Assignment2 { //Giving a name to the class
public static void main(String[] args) { //Calling the main
    Scanner scnr = new Scanner(System.in); //Letting it know that information will be entered by user
    int length = 0; //These are the integers to be entered by the user
    int maxValue = 0;
    int minValue = 0;
  
    System.out.println("Enter size of sample: "); //The following 9 lines include the prompts for user to enter info
    length = scnr.nextInt(); //and also assigns the value the user enters into a variable
  
    System.out.println("Enter max possible value: ");
    maxValue = scnr.nextInt();
  
    System.out.println("Enter minimum possible value: ");
    minValue = scnr.nextInt();
  
    while (length >= 0); //As long as the number entered by user is > 0, it will go into a loop of genrating random #s
      Random randGen = new Random(); // Introducing the Random Number Generator! Yeaaaa! : )
      int randomNum = 0;
    
      randomNum = rand.nextInt((max - min) + 1) + min;   
      System.out.print("Random Numbers: " + randomNum);

Explanation / Answer

import java.util.Scanner; //Importing Java utility scanner so that it recognizes information to be entered by user
import java.lang.Math; //Importing Math language so it will understand and correctly use Math verbage
import java.util.Random; // Importing Java utility in order to utilize the random number generator

public class Assignment2 { //Giving a name to the class

public static void main(String[] args) { //Calling the main
  
Scanner scnr = new Scanner(System.in); //Letting it know that information will be entered by user
int length = 0; //These are the integers to be entered by the user
int maxValue = 0;
int minValue = 0;
  
System.out.println("Enter size of sample: ");
length = scnr.nextInt();
  
System.out.println("Enter max possible value: ");
maxValue = scnr.nextInt();
  
System.out.println("Enter minimum possible value: ");
minValue = scnr.nextInt();

int[] result = new int[length]; // defining the array with length entered by the user before
Double sum = 0.0; // sum holds the sum of all elements in array
  
for ( int i = 0; i < length ; i++ ) {
Random rand = new Random();
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNum = rand.nextInt((maxValue - minValue) + 1) + minValue;
result[i] = randomNum;

sum = sum + result[i]; // adding the elements of array one by one
}

System.out.println("Elements in array: ");
for ( int i = 0; i < length ; i++ ) {
System.out.println(result[i]); // printing the array
}

System.out.println("Average: "+ sum/length); // finding average
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote