Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

letsdiscussit

macrumors newbie
Original poster
Jun 26, 2007
27
0
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;
}
}
}
 

stevento

macrumors 6502
Dec 10, 2006
252
0
Los Angeles
if you want a random int i wouldn't bother with importing that i would do this


Code:
int r = (int) (10.0* Math.rand());

and that will give you a random number from 0 to 9
 

letsdiscussit

macrumors newbie
Original poster
Jun 26, 2007
27
0
Thanks for your replies. I am still doing something wrong, in bold I highlited the are, doesn't except the variable a. How I am going to do so?

package assignment2;

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



public class Assignment2 extends Applet {

public void fill( int max ) {
final int MAX = 6;

Random generator = new Random();

int[] a = new int[MAX];

for(int k = 0; k < MAX; k++){



a[k] = generator.nextInt(100) + 1;


}
}

// What am I doing wrong that it cannot find variable a??

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

eharley

macrumors newbie
Dec 27, 2007
20
16
Thanks for your replies. I am still doing something wrong, in bold I highlited the are, doesn't except the variable a. How I am going to do so?

You're defining a inside the fill() method. So it's only accessible to that method. If you want other methods in your class to be able to get to it, you need to declare it in your class like this:
Code:
public class Assignment2 extends Applet {
	
	final int MAX = 6;
	int[] a = new int[MAX];

	/// ....
 

letsdiscussit

macrumors newbie
Original poster
Jun 26, 2007
27
0
thanks Eharley, I almost got it. In two lines, it now says:local variable hides field (highlighted below), what's wrong there?


package assignment2;

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



public class Assignment2 extends Applet {
private int MAX;

int[] a = new int[MAX];
public void fill( int max ) {
//local variable hides field
final int MAX = 6;

Random generator = new Random();
//local variable hides field
int[] a = new int[MAX];

for(int k = 0; k < MAX; k++){



a[k] = generator.nextInt(100) + 1;


}
}


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

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
First, you declare MAX as a global private int.

Then, inside a method, you redefine it as a constant.

Then, in your method, you reference it as a constant.

Pick one.
 

letsdiscussit

macrumors newbie
Original poster
Jun 26, 2007
27
0
toddburch can u show me how? I thought I refernced it here already as a constant:



private int MAX;

int[] a = new int[MAX];
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
Your variable a was out of scope. Declaring it as a global, it is no longer out of scope.

Code:
package assignment2;

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



public class Assignment2 extends Applet {

[color=red]final int MAX = 6;
int[] a = new int[MAX];[/color]


public void fill( int max ) {

	Random generator = new Random();

	for(int k = 0; k < MAX; k++){
		a[k] = generator.nextInt(100) + 1;
		}
	}	

	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[i] > b[j]) {
					temp = b[i];
					b[i] = 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[i]), x, y);
			x += 20;
		}
	}
}
 

letsdiscussit

macrumors newbie
Original poster
Jun 26, 2007
27
0
How I am going to call the fill () method? If I run the program, it generates only zeros. Please help!
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Code:
package Assignment2;

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



public class Assignment2 extends Applet {

public int[] a = null;

public void fill( int max ) {

        Random generator = new Random();
        a = new int[max];

        for(int k = 0; k < max; k++){
                a[k] = generator.nextInt(100) + 1;
                }
        }

        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[i] > b[j]) {
                                        temp = b[i];
                                        b[i] = 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[i]), x, y);
                        x += 20;
                }
        }

        public void init() {
                this.fill(6);
        }

        public Assignment2() { System.out.println("Test!"); }
}

Either init() or the constructor needs to call fill. I have never dealt with applet programming before, but I was finally able to get this to run with appletviewer, displaying what I would expect.

-Lee
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.