I take a course at univ in Java for beginners and we got an assignment and I just don't know how to solve it.
I programed an array of integers that get sorted and than printed. Now I want to produce the same thing with random numbers using "import java.util.Random". How I am going to do that?
package Assignment1.sorting;
import java.awt.Graphics;
import java.applet.Applet;
public class Assignment1 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(a);
print(g, "Data items in ascending order", a, 25, 55);
}
public void sort(int b[]) {
int temp;
for (int i = 0; i < b.length - 1; i++) {
for (int j = i + 1; j < b.length; j++) {
if (b > b[j]) {
temp = b;
b = b[j];
b[j] = temp;
}
}
}
}
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), x, y);
x += 20;
}
}
}
I programed an array of integers that get sorted and than printed. Now I want to produce the same thing with random numbers using "import java.util.Random". How I am going to do that?
package Assignment1.sorting;
import java.awt.Graphics;
import java.applet.Applet;
public class Assignment1 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(a);
print(g, "Data items in ascending order", a, 25, 55);
}
public void sort(int b[]) {
int temp;
for (int i = 0; i < b.length - 1; i++) {
for (int j = i + 1; j < b.length; j++) {
if (b > b[j]) {
temp = b;
b = b[j];
b[j] = temp;
}
}
}
}
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), x, y);
x += 20;
}
}
}