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

i need help implementing this with my code the task: Use the random number gener

ID: 670094 • Letter: I

Question

i need help implementing this with my code

the task:
Use the random number generator from java.util.Random to generate the data you'll be sorting.

You'll need the import statement. Put it up front, before the "class" definition. It tells Java to make the Random library available to your program.

You'll not need another main, you have one of those already. Nor do you need a new class, or a new "run"; this code goes in your existing class and functions.

DO ONLY CREATE A "NEW RANDOM()" ONCE in your program execution.

"new Random()" creates and initializes a new "generator" of random numbers, an object of class Random.

Calling function "nextInt()" in a class Random object causes the generator to calculate new "random" value and return that value. Here we set the variable "newNum" to that value.

Random numbers can come in all kinds of shapes. "nextInt()" returns integer values (not floating point, for instance).   "nextInt" guarantees that it return value will be non-negative, and less that the value given as an argument to the call -- in this case, < 37. You can choose anything you want for the bound. I chose 37 (i.e., 0..36) just to illustrate the point with moderately small values.

my code :

/**Java program that demonstrates the insertion sort algorithm to sort unsorted elements into
* sorted in ascending order*/

//InsertionSortProgram.java
public class InsertionSortProgram
{
     public static void main(String[] args)
     {
    
       //array of un-sorted elements
        int[] b=new int[9];

        //insert elements into array
        b[0]=9;
        b[1]=5;
        b[2]=4;
        b[3]=2;
        b[4]=3;
        b[5]=10;
        b[6]=15;
        b[7]=7;
        b[8]=1;
    
    
    
    
        System.out.println("UNSORTED ARRAY");
        System.out.println("ARRAY ELEMENTS : ");
        print(b);
        System.out.println("SELECTION SORT");
        //calling the method insertionSort method
        int[] a=insertionSort(b);
        System.out.println("SORTED ARRAY");
        System.out.println("ARRAY ELEMENTS : ");
        //print sorted elements
        print(a);
    
    }

    /**The method insertionSort that takes the elements
     * and sorts the elements in ascending order*/
    public static int[] insertionSort(int elements[])
    {
       //loop through the array elements and sort the elements
        for (int index = 1; index < elements.length; index++)
        {
           //get element at index position
            int current = elements[index];
            //set index position to pos
            int pos = index;
        
            /*compare the starting element,pos-1 with the next element at current
            If starting element is greater than current then
            move the startig element to right side by pos value until pos is greater than
            zero.*/
            while ( (pos > 0) && ( elements [pos-1] > current ) )
            {
               elements [pos] = elements [pos-1];
                pos=pos-1;
            }
            //Now set the current element into its sorted position in the list.
            elements[pos] = current;
        
       
        }   
        //returns the sorted list
        return elements;
    }

    //Print the elements in the array
    private static void print(int[] array)
    {    
        for (int element : array)
           System.out.println(element+" ");
    
        System.out.println();
    
    }
}

Explanation / Answer

import java.util.Random;


public class InsertionSortProgram
{
public static void main(String[] args)
{
Random r=new Random();
//array of un-sorted elements
int[] b=new int[9];
//insert elements into array
b[0]=r.nextInt(36);
b[1]=r.nextInt(36);
b[2]=r.nextInt(36);
b[3]=r.nextInt(36);
b[4]=r.nextInt(36);
b[5]=r.nextInt(36);
b[6]=r.nextInt(36);
b[7]=r.nextInt(36);
b[8]=r.nextInt(36);
  
  
  
  
System.out.println("UNSORTED ARRAY");
System.out.println("ARRAY ELEMENTS : ");
print(b);
System.out.println("SELECTION SORT");
//calling the method insertionSort method
int[] a=insertionSort(b);
System.out.println("SORTED ARRAY");
System.out.println("ARRAY ELEMENTS : ");
//print sorted elements
print(a);
  
}
public static int[] insertionSort(int elements[])
{
//loop through the array elements and sort the elements
for (int index = 1; index < elements.length; index++)
{
//get element at index position
int current = elements[index];
//set index position to pos
int pos = index;

/*compare the starting element,pos-1 with the next element at current
If starting element is greater than current then
move the startig element to right side by pos value until pos is greater than
zero.*/
while ( (pos > 0) && ( elements [pos-1] > current ) )
{
elements [pos] = elements [pos-1];
pos=pos-1;
}
//Now set the current element into its sorted position in the list.
elements[pos] = current;

  
}   
//returns the sorted list
return elements;
}
  
//Print the elements in the array
private static void print(int[] array)
{
for (int element : array)
System.out.println(element+" ");

System.out.println();

}
}