Create a new Java project/class called ChangeUp. Create an empty int array of size 6. Create a method called populateArray that will pass an array, use random class to choose a random index and prompt the user to enter a value, store the value in the array. Note: Generate 6 random index numbers in an attempt to fill the array. Create a method called printArray that prints the contents of the array using the for each enhanced loop. Submit code. Note: It is possible that your array won't be filled and some values will still have 0 stored. That is acceptable.

Respuesta :

Answer:

//import the Random and Scanner classes

import java.util.Random;

import java.util.Scanner;

//Begin class definition

public class ChangeUp {

   

   //Begin the main method

  public static void main(String args[]) {

       //Initialize the empty array of 6 elements

      int [] numbers = new int [6];

       

       //Call to the populateArray method

       populateArray(numbers);

       

       

   } //End of main method

   

   

   //Method to populate the array and print out the populated array

   //Parameter arr is the array to be populated

   public static void populateArray(int [] arr){

       

       //Create object of the Random class to generate the random index

       Random rand = new Random();

       

       //Create object of the Scanner class to read user's inputs

       Scanner input = new Scanner(System.in);

       

       //Initialize a counter variable to control the while loop

       int i = 0;

       

       //Begin the while loop. This loop runs as many times as the number of elements in the array - 6 in this case.

       while(i < arr.length){

           

           //generate random number using the Random object (rand) created above, and store in an int variable (randomNumber)

           int randomNumber = rand.nextInt(6);

       

           //(Optional) Print out a line for formatting purposes

           System.out.println();

           

           //prompt user to enter a value

           System.out.println("Enter a value " + (i + 1));

           

           //Receive the user input using the Scanner object(input) created earlier.

           //Store input in an int variable (inputNumber)

           int inputNumber = input.nextInt();

           

           

           //Store the value entered by the user in the array at the index specified by the random number

           arr[randomNumber] = inputNumber;

           

           

           //increment the counter by 1

          i++;

           

       }

       

       

       //(Optional) Print out a line for formatting purposes

       System.out.println();

       

       //(Optional) Print out a description text

      System.out.println("The populated array is : ");

       

       //Print out the array content using enhanced for loop

       //separating each element by a space

       for(int x: arr){

           System.out.print(x + " ");

       }

   

       

  } //End of populateArray method

   

   

   

} //End of class definition

Explanation:

The code above is written in Java. It contains comments explaining the lines of the code.

This source code together with a sample output has been attached to this response.

Ver imagen stigawithfun
Ver imagen stigawithfun
ACCESS MORE
EDU ACCESS