PDA

View Full Version : java homework, I am stuck




letsdiscussit
Apr 5, 2008, 08:04 PM
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[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;
}
}
}



stevento
Apr 5, 2008, 08:37 PM
if you want a random int i wouldn't bother with importing that i would do this


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


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

sord
Apr 5, 2008, 08:38 PM
You need to create a Random object, and then look at the documentation here:
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Random.html

This shows you the methods you can call for a Random object.

SC68Cal
Apr 5, 2008, 09:12 PM
Random generator = new Random();

generator.nextInt();

letsdiscussit
Apr 6, 2008, 11:52 AM
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[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;
}
}
}

eharley
Apr 6, 2008, 12:34 PM
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:

public class Assignment2 extends Applet {

final int MAX = 6;
int[] a = new int[MAX];

/// ....

letsdiscussit
Apr 6, 2008, 12:45 PM
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
Apr 6, 2008, 01:16 PM
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
Apr 6, 2008, 02:56 PM
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
Apr 6, 2008, 03:17 PM
Your variable a was out of scope. Declaring it as a global, it is no longer out of scope.


package assignment2;

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



public class Assignment2 extends Applet {

final int MAX = 6;
int[] a = new int[MAX];


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

toddburch
Apr 6, 2008, 03:39 PM
P.S.

You still need to call the fill() method. Once you do that, it looks good!

letsdiscussit
Apr 6, 2008, 05:10 PM
How I am going to call the fill () method? If I run the program, it generates only zeros. Please help!

letsdiscussit
Apr 7, 2008, 09:00 PM
Please help, don't understand where the problem is ...

toddburch
Apr 7, 2008, 09:26 PM
You have to call the fill() method from paint(), otherwise, your "a" array will have no data in it.

lee1210
Apr 7, 2008, 09:59 PM
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