Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So I am trying to implement a groupSize method and a helper functions. Here is one idea: set up a local Set of Spots to hold spots you’ve found so far in the cluster. From the current spot-position, greedily add as many direct neighbor spots as possible. For each neighbor spot that is actually newly-found, call recursively from that position.

I am completely lost about how to go and implement that method and would like a better explanation what a helper functions is for.

public class Grid {

 private boolean[][] grid = null;

/**
 * Very simple constructor
 * 
 * @param ingrid
 *            a two-dimensional array of boolean to be used as the grid to
 *            search
 */
public Grid(boolean[][] ingrid) {
    grid = ingrid;
}

/**
 * Main method, creates a Grid, then asks it for the size of the group
 * containing the given point.
 */
public static void main(String[] args) {
    int i = 0;
    int j = 0;

    // Make sure we've got the right number of arguments
    if (args.length != 2) {
        System.err.println("Incorrect arguments.");
        printUsage();
        return;
    } else {
        i = Integer.parseInt(args[0]);
        j = Integer.parseInt(args[1]);
    }

    // Usage: java Grid 3 7 to search from (3, 7), top occupied square
    // of L-shaped group of Figure 7.30, pg. 281

    boolean[][] gridData = {
            { false, false, false, false, false, false, false, false,
                    false, true },
            { false, false, false, true, true, false, false, false, false,
                    true },
            { false, false, false, false, false, false, false, false,
                    false, false },
            { false, false, false, false, true, false, false, true, false,
                    false },
            { false, false, false, true, false, false, false, true, false,
                    false },
            { false, false, false, false, false, false, false, true, true,
                    false },
            { false, false, false, false, true, true, false, false, false,
                    false },
            { false, false, false, false, false, false, false, false,
                    false, false },
            { false, false, false, false, false, false, false, false,
                    false, false },
            { false, false, false, false, false, false, false, false,
                    false, false } };

    Grid mygrid = new Grid(gridData);

    int size = mygrid.groupSize(i, j);
    System.out.println("Group size: " + size);
}

/**
 * Prints out a usage message
 */
private static void printUsage() {
    System.out.println("Usage: java Grid <i> <j>");
    System.out
            .println("Find the size of the cluster of spots including position i,j");
}

/**
 * This calls the recursive method to find the group size
 * 
 * @param i
 *            the first index into grid (i.e. the row number)
 * @param j
 *            the second index into grid (i.e. the col number)
 * @return the size of the group
 */
public int groupSize(int i, int j) {
    // Implement this function which, among other calls a helper recursive function 
    // to find the group size.


}


/**
 * Nested class to represent a filled spot in the grid
 */
private static class Spot {
    int i;
    int j;

    /**
     * Constructor for a Spot
     * 
     * @param i
     *            the i-coordinate of this Spot
     * @param j
     *            the j-coordinate of this Spot
     */
    public Spot(int i, int j) {
        this.i = i;
        this.j = j;
    }

    /**
     * Tests whether this Spot is equal (i.e. has the same coordinates) to
     * another
     * 
     * @param o
     *            an Object
     * @return true if o is a Spot with the same coordinates
     */
    public boolean equals(Object o) {
        if (o == null)
            return false;
        if (o.getClass() != getClass())
            return false;
        Spot other = (Spot) o;
        return (other.i == i) && (other.j == j);

    }

    /**
     * Returns an int based on Spot's contents
     * another way: (new Integer(i)).hashCode()^(new Integer(j)).hashCode();
     */
    public int hashCode() {
        return i << 16 + j; // combine i and j two halves of int
    }

    /**
     * Returns a String representing this Spot
     */
    public String toString() {
        return "(" + i + " , " + j + ")";
    }
}

}

share|improve this question

closed as off-topic by Jamal Nov 6 at 20:04

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." – Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.