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

Java: I need help editing my code: i currently have it so that the program sorts

ID: 3674190 • Letter: J

Question

Java: I need help editing my code: i currently have it so that the program sorts the list given into ascending order. I need the numbers to be randomily generated using either the import java.long.math or import java.util.random. Any help will be much appreciated.

My code:

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


public class Sort extends Applet{
  
int a[] = { 55, 25, 66, 45, 8, 10, 12, 89, 68, 37 };
int b[] = { 55, 25, 66, 45, 8, 10, 12, 89, 68, 37 };
  
public void paint(Graphics g){
print(g,"Data items in original orer",a,25,25);
sort(b);
  
print(g,"Data items in ascending order",b,25,55);
  
}
  
//Bubble Sort Function: Sets an array into ascending order
public void sort(int a[]){
int i,j,t;
for(i=0; i< b.length; i++){
for(j=0; j< b.length ; j++){
if (b[i] < b[j]){
t = b[i];
a[i] = a[j];
a[j] = t;
}
}
}
}

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;
}
}
}

Explanation / Answer

hi if you want to randomily generated array:

ADD THIS CODE:

g.drawString("Generating 10 random integers in range 0..99.",10,10);
a=new int[10];
b=new int[10];
Random randomGenerator = new Random();
for(int idx = 0; idx <10; ++idx)
{
a[idx]= randomGenerator.nextInt(100);
b[idx]=a[idx];
}

NOW ACTUAL PROGRAM IS :

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

public class Sort extends Applet{
  
int a[],b[];
  
//note a single Random object is reused here


public void paint(Graphics g){

//Generating 10 random integers in range 0..99
g.drawString("Generating 10 random integers in range 0..99.",10,10);
a=new int[10];
b=new int[10];
Random randomGenerator = new Random();
for(int idx = 0; idx <10; ++idx)
{
a[idx]= randomGenerator.nextInt(100);
b[idx]=a[idx];
}


print(g,"Data items in original orer",a,25,25);
sort(b);
  
print(g,"Data items in ascending order",b,25,55);
  
}
  
//Bubble Sort Function: Sets an array into ascending order
public void sort(int a[]){
int i,j,t;
for(i=0; i< b.length; i++){
for(j=0; j< b.length ; j++){
if (b[i] < b[j]){
t = b[i];
a[i] = a[j];
a[j] = t;
}
}
}
}

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;
}
}
}

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