CaptainDabs
CaptainDabs

Reputation: 45

Error passing array as parameter - Java

A program I'm modifying is supposed to use a drawing panel to randomly move a square, starting from the center, either left or right and use an array to tally the position it moves to while the square stays on screen (the panel is 400 x 400 and the square is 10 x 10, so there are only 40 possible positions it can move to) After the square goes off screen, I have to print a histogram that shows how many times the square moved to that index (i.e if the square moved from the x coordinate of 200 to 190, index 19 would get a tally) Here is my code:

import java.awt.*;
import java.util.*;

public class RandomWalkCountSteps {
// DrawingPanel will have dimensions HEIGHT by WIDTH
public static final int HEIGHT = 100;
public static final int WIDTH = 400;

public static final int CENTER_X = WIDTH / 2;
public static final int CENTER_Y = HEIGHT / 2;

public static final int CURSOR_DIM = 10;

public static final int SLEEP_TIME = 25; // milliseconds

public static void main( String[] args ) {
    DrawingPanel panel = new DrawingPanel( WIDTH, HEIGHT );

    Random rand = new Random();

    walkRandomly( panel, rand );


}

public static void walkRandomly( DrawingPanel panel, Random rand ) {
    Graphics g = panel.getGraphics();
    int[] positionCounts = new int[ WIDTH / CURSOR_DIM ];
    // start in center of panel
    int x = CENTER_X;
    int y = CENTER_Y;

    // Draw the cursor in BLACK
        g.fillRect(x, y, CURSOR_DIM, CURSOR_DIM);

    // randomly step left, right, up, or down
    while ( onScreen( x, y ) ) {

        panel.sleep( SLEEP_TIME );
        // Show a shadow version of the cursor
        g.setColor(Color.GRAY);
        g.fillRect(x, y, CURSOR_DIM, CURSOR_DIM);

        if ( rand.nextBoolean() ) { // go left
                x -= CURSOR_DIM;
        }
        else {  // go right
            x += CURSOR_DIM;
        }
        positionCounts[ x / CURSOR_DIM ]++;
        histogram(positionCounts, x, y);
        // draw the cursor at its new location
        g.setColor(Color.BLACK);
        g.fillRect(x, y, CURSOR_DIM, CURSOR_DIM);
    }
}

public static boolean onScreen( int x, int y ) {
    return 0 <= x && x < WIDTH
        && 0 <= y && y < HEIGHT;
}

   public static void histogram(int[] positionCounts, int x, int y) {
      if (onScreen(x, y) == false) {
         for (int i = 0; i < WIDTH / CURSOR_DIM; i++) {
            System.out.print(i + ": ");
            for (int j = 1; j <= positionCounts[i]; j++) {
               System.out.print("*");
            }
            System.out.println();
         }
      }
   }
}

My problem was that I couldn't find a good place to initialize the array so that it wouldn't re-initialize every time I passed the x coordinate to the histogram method. Now that I thought I had it in the right place, I get this error message on both calls to histogram in the method walkRandomly "error: method histogram in class RandomWalkCountSteps cannot be applied to given types;" I'm fairly new to java and programming in general, so there's probably something I'm missing regarding arrays as parameters. Thanks in advance.

Upvotes: 2

Views: 126

Answers (1)

thomasd
thomasd

Reputation: 2612

histogram takes two parameters, positionCounts of type int[] and x of type int. In walkRandomly, you call histogram twice: once with an argument positionCounts of type int[] and once with an argument x of type int. That’s why the compiler complains that the method ”cannot be applied to given types”: the method histogram(int[], int) can’t be applied to (called with) the given types, i.e., histogram(int[]) and histogram(int).

I’m not sure what you’re trying to do with this code, but I’d guess that you want remove the first call and change the second call (inside of the while loop) to histogram(positionCounts, x).

(You’ve edited your code, so my answer doesn’t make much sense.)

Upvotes: 2

Related Questions