java homework, I am stuck

Discussion in 'Mac Programming' started by letsdiscussit, Apr 5, 2008.

  1. macrumors newbie

    letsdiscussit

    Joined:
    Jun 26, 2007
    #1
    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;
    }
    }
    }
     
  2. macrumors 6502

    stevento

    Joined:
    Dec 10, 2006
    Location:
    Los Angeles
    #2
    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
     
  3. macrumors 6502

    Joined:
    Jun 16, 2004
    #3
  4. macrumors 68000

    Joined:
    Feb 23, 2006
    #4
    Code:
    Random generator = new Random();
    
    generator.nextInt();
    
    
     
  5. thread starter macrumors newbie

    letsdiscussit

    Joined:
    Jun 26, 2007
    #5
    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;
    }
    }
    }
     
  6. macrumors newbie

    Joined:
    Dec 27, 2007
    #6
    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];
    
    	/// ....
    
     
  7. thread starter macrumors newbie

    letsdiscussit

    Joined:
    Jun 26, 2007
    #7
    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);
     
  8. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #8
    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.
     
  9. thread starter macrumors newbie

    letsdiscussit

    Joined:
    Jun 26, 2007
    #9
    toddburch can u show me how? I thought I refernced it here already as a constant:



    private int MAX;

    int[] a = new int[MAX];
     
  10. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #10
    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;
    		}
    	}
    }
     
  11. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #11
    P.S.

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

    letsdiscussit

    Joined:
    Jun 26, 2007
    #12
    How I am going to call the fill () method? If I run the program, it generates only zeros. Please help!
     
  13. thread starter macrumors newbie

    letsdiscussit

    Joined:
    Jun 26, 2007
    #13
    Please help, don't understand where the problem is ...
     
  14. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #14
    You have to call the fill() method from paint(), otherwise, your "a" array will have no data in it.
     
  15. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #15
    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
     

Share This Page