tags:

views:

75

answers:

2

Ok so say I have a class

class Ghost {   
    private int x, y, direction;
    private Color color;

Now say I dont explicitly define a constructor, so it just uses the default one. If I were to create a new Ghost object, what value would direction have? 0 right? But I dont know why I keep getting a value of 1 for direction. Anything off the bat that you think could be wrong?

+1  A: 

Can you show us more code? Methinks something else is going on here.

int variables will default initialize to 0 in Java, and if I use your snippet, and instantiate a Ghost, all its int members are 0.

So from your comments, I think there might be an issue with your code, like you are accidentally setting direction when using Ghost.setX or Ghost.setY.

In my extremely simple class implementation, this testing code works exactly as intended.

Edited since code was posted

Okay so I have a class based on your code. I don't see anything wrong with the other functions, since very few classes actually alter direction. I removed those extra functions because they didn't modify direction except for one thing that would set it to 0.

You should try a very reduced test case to see if your code works. Try this:

import junit.framework.Assert;

class Ghost
{
    private int x, y, direction;

    public int getX() { return x; }
    public void setX(int x) { this.x = x; }

    public int getY() { return y; }
    public void setY(int y) { this.y = y; }

    public int getDirection() { return direction; }
    public void setDirection(int direction) { this.direction = direction; }

    public static void main (String [] args)
    {
        Ghost g = new Ghost();
        g.setX(10);
        g.setY(20);

        Assert.assertEquals(g.getDirection(), 0);

        System.out.println(g.getX() + " " + g.getY() + " " + g.getDirection());
    }   
}

Everything still works here, so try that code as is. This is purely your code, and if it works (and it should), then there's other code like a user of your class that is doing something.

Standalone, your code should work just fine.

Use the Debugger

Are you familiar with a debugger? If you are using Eclipse or NetBeans, then you should get friendly with using the debuggers integrated into their environments. This should really help you track down when direction changes values by placing a watch on it.

Using a debugger is way beyond the scope of this answer; it's something you really have to read up on and try yourself.

Here's a link for debugging with Eclipse:

birryree
Well theres no other code really, but my teacher uses this test: `Ghost g1 = new Ghost(); g1.setX(10); g1.setY(20); assertEquals(g1.getDirection(), 0);`
fprime
So its expecting a 0, but I get a assertequalsfailed cuz it had a 1 instead. I meant I have no other code that tampers with direction, so I dont know what it could be.
fprime
please show all these getters and setters, there could be a bug there.
Thilo
Hmm..weird.. I posted an edit with the getters and setters for direction..
fprime
how about setX and setY? does it work if you don't call those?
Thilo
Ya im getting 10,20,0 running your code. Dont know what the problem could be..
fprime
Ya I still gotta get acquainted with the debugger, havent got around to that yet. But I fixed the issue for now. I think I may have been reading the tests incorrectly, his test methods are confusing. Thanks for your help!
fprime
+1  A: 

There's something you aren't showing us. Does your getDirection() method simply return direction, or does it do something more exotic? Is there a setter method that does something strange? Does some other part of the class that you did not show us modify direction in some way?

In Java, instance variables of type int are initialized to 0, so you are either not showing us something or you are using some non-standard implementation (almost definitely not the latter).

Michael McGowan
See edit for getters and setters please..
fprime
The code you posted should work. Are you calling setDirection() from some other class or something?
Michael McGowan
On an unrelated note, you probably want to rethink your equals method. The point of the equals method is to check if "this" is equal to "other." That you don't reference "other" in the method should be a red flag...
Michael McGowan
Ya I got an error on that. But the teacher said "Returns true if the other ghost object is identical to this * one e.g. this.equals(this.copy()) should be true" And I just implemented that. What am I doing wrong?
fprime
The parameter of your equals method is other. The point is to check whether "this" and "other" are identical. There is no reason for copy to come into play here (and your copy method might not do what you think it does either). Furthermore, your call to this.equals calls the very method you are writing, so it will keep calling itself repeatedly.
Michael McGowan