Results 1 to 3 of 3

Thread: "-" problem

  1. #1
    FOX427 is offline Member
    Join Date
    Jun 2011
    Posts
    54
    Rep Power
    0

    Default "-" problem

    Hi! Created class Bug which moving either to the left or right on horizontal line. Initially bug moves right but it can turn to change diretion. in each move its position changes by one unit in the current direction. So we have:

    public class Bug {
    public Bug(int initialPosition)
    {
    position=initialPosition;
    }
    public void turn()
    {
    position=-position*(-1);
    }
    public void move()
    {
    position=position+1;
    }
    public int getPosition()
    {
    return position;

    }
    private int position;
    }


    and tester:



    public class BugTester {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Bug bugsy = new Bug(10);
    bugsy.move(); // now the position is 11
    bugsy.turn();
    bugsy.move(); // now the position is 10
    System.out.println(bugsy.getPosition());
    }

    }
    of course it gives us incorrect answer: -10 instead of 10. The problem is I cant construct right algorithm(maybe i am weak in math) that gives me right sign every time when I turn the bug. hope you know smth guys and thanks in advance.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: "-" problem

    ?!?!
    Your description is incorrect!
    a) bugsy.move(); // now the position is 10 - thats wrong! 10+1 = 11 , -11*-1 = 11 + 1 = 12
    b) of course it gives us incorrect answer: -10 instead of 10. ?????
    c) there is another thread with the exactly same question : Creating a 'turn' method
    d) as JoshAs said, notice a direction (int direction = 1) and in your turn method multiply with -1. At your move method simple add the direction to your position (So then you automaticcaly add either +1 or -1)

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default Re: "-" problem

    Quote Originally Posted by eRaaaa View Post
    c) there is another thread with the exactly same question : Creating a 'turn' method
    Agreed. Closing this.

    FOX427, don't double post and do remember to use code tags for posting code.

    db

    THREAD CLOSED
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 1
    Last Post: 02-01-2012, 08:37 PM
  2. Replies: 5
    Last Post: 12-21-2011, 06:44 PM
  3. problem with argument list and precedence "(" and ")"
    By helpisontheway in forum Advanced Java
    Replies: 6
    Last Post: 12-24-2009, 07:50 AM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •