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

Utilizing the following Java Program, modify the code so that everytime the prog

ID: 3816363 • Letter: U

Question

Utilizing the following Java Program, modify the code so that everytime the program runs, a new set of random integers is generated and sorted. Also, please prove that the program works by running the program. (This program implements insertion sort)

import java.awt.Graphics;
import java.applet.Applet;

public class SortingProg extends Applet
{
int a[] = { 55, 25, 66, 45, 8, 10, 12, 89, 68, 37 };

public void paint(Graphics g)
    {
      print(g,"Data items in original order",a,25,25);

      sort();

      print(g,"Data items in ascending order",a,25,55);
    }


public void sort() {

        int n = a.length;

for (int i=1; i

{

int key = a[i];

int j = i-1;

/* Move the elements of the array that are
   greater than the key, to one position ahead
   of their current position, thus properly
   sorting through the elements. */

while (j>=0 && a[j] > key)

{

a[j+1] = a[j];

j = j-1;

}

a[j+1] = key;

}

}

public void print(Graphics g, String head, int b[], int x, int y)

{
      g.drawString(head,x,y);
      x+=15;
      y+=15;
      for(int i=0;i     {
      g.drawString(String.valueOf(b[i]),x,y);
      x+=20;
    }
    }
}

Explanation / Answer

Hi, Please find implementation.

Please let me know in the case of any issue.

import java.awt.Graphics;

import java.util.Random;

import java.applet.Applet;

public class SortingProg extends Applet

{

   /**

   *

   */

   private static final long serialVersionUID = 1L;

   int a[] ;

   public void paint(Graphics g)

   {

       a = new int[10]; // allocating an array of size 10

       // filling array with 10 random numbers in range 1-100

       Random random = new Random();

       for(int i=0; i<10; i++){

           a[i] = random.nextInt(100)+1; // random number in range 1-100

       }

      

       print(g,"Data items in original order",a,25,25);

       sort();

       print(g,"Data items in ascending order",a,25,55);

   }

   public void sort() {

       int n = a.length;

       for (int i=1; i<n; i++)

       {

           int key = a[i];

           int j = i-1;

           /* Move the elements of the array that are

   greater than the key, to one position ahead

   of their current position, thus properly

   sorting through the elements. */

           while (j>=0 && a[j] > key)

           {

               a[j+1] = a[j];

               j = j-1;

           }

           a[j+1] = key;

       }

   }

   public void print(Graphics g, String head, int b[], int x, int y)

   {

       g.drawString(head,x,y);

       x+=15;

       y+=15;

       for(int i=0;i<b.length; i++){

           g.drawString(String.valueOf(b[i]),x,y);

           x+=20;

       }

   }

}