0

I am trying to write a code to get the set of points (x,y) that are accessible to a monkey starting from (0,0) such that each point satisfies |x| + |y| < _limitSum. I have written the below code and have used static HashSet of members of Coordinate type (not shown here) and have written a recursive method AccessPositiveQuadrantCoordinates. But the problem is the members of the HashSet passed across the recursive calls is not reflecting the Coordinate members added in previous calls. Can anybody help me on how to pass Object references to make this possible? Is there some other way that this problem can be solved?

public class MonkeyCoordinates {
public static HashSet<Coordinate> _accessibleCoordinates = null;
private int _limitSum;
public MonkeyCoordinates(int limitSum) {
    _limitSum = limitSum;
    if (_accessibleCoordinates == null)
        _accessibleCoordinates = new HashSet<Coordinate>();
}
public int GetAccessibleCoordinateCount() {
    _accessibleCoordinates.clear();
    Coordinate start = new Coordinate(0,0);
    AccessPositiveQuadrantCoordinates(start);
    return (_accessibleCoordinates.size() * 4);
}
private void AccessPositiveQuadrantCoordinates(Coordinate current) {
    if (current.getCoordinateSum() > _limitSum) { return; }
    System.out.println("debug: The set _accessibleCoordinates is ");
    for (Coordinate c : _accessibleCoordinates) {
        System.out.println("debug:" + c.getXValue() + " " + c.getYValue());
    }

    if (!_accessibleCoordinates.contains(current)) { _accessibleCoordinates.add(current); }
    AccessPositiveQuadrantCoordinates(current.Move(Coordinate.Direction.East));
    AccessPositiveQuadrantCoordinates(current.Move(Coordinate.Direction.North));
}

I will give points to all acceptable answers.

Thanks ahead, Somnath

somnathchakrabarti
  • 3,026
  • 10
  • 69
  • 92

2 Answers2

2

But the problem is the members of the HashSet passed across the recursive calls is not reflecting the Coordinate members added in previous calls.

I think that's very unlikely. I think it's more likely that your Coordinate class doesn't override equals and hashCode appropriately, which is why the set can't "find" the values.

As an aside, using static variables like this seems like a very bad idea to me - why don't you create the set in GetAccessibleCoordinateCount() and pass the reference to AccessPositiveQuadrantCoordinates, which can in turn keep passing it down in the recursive calls?

(As another aside, I would strongly suggest that you start following Java naming conventions...)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks. But even if I add the HashSet in GetAccessibleCoordinateCount(), I still have to pass the HashSet _accessibleCoordinates as reference. What is the way to pass an Object as reference in Java ? – somnathchakrabarti Mar 19 '12 at 09:21
  • @somnathchakrabarti: The value of an expression in Java is *always* a reference or a primitive value. You can never pass the *actual object*. You can't pass an argument *by* reference, but if you pass a reference type variable by value, you're passing a reference anyway - so any modifications made to the object will be visible to the caller. (Modifications to the parameter variable itself, e.g. making it refer to another object, *won't* affect the argument.) – Jon Skeet Mar 19 '12 at 09:23
1

i don't see any problem with making the field _accessibleCoordinates non-static

and you should know that HashSet does not guarantee the same iteration order everytime, you could better use a LinkedList for that purpose...

and about pass by reference, i found this post very useful

java - pass by value - SO link

From what you are doing you would be updating _accessibleCoordinates in every recursive call correctly.

Community
  • 1
  • 1
redDevil
  • 1,909
  • 17
  • 25
  • Yes u r right. But I am printing all the elements in the HashSet and does the iteration order matters too much? Can you please brief on how non-static field will work? You can assume that we need the set of all points instead of just Count. – somnathchakrabarti Mar 19 '12 at 09:27