Results 1 to 4 of 4
  1. #1
    marcosol is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default get a previous or following element with a indentifier

    I have a robot with an orientation ("up","down","left","right"). He must be able to turn clockwise and counter-clockwise.

    My first solution was to write an if-statement for every possible orientation and, depending on the current orientation(saved in a private variable), to simply use a setter to change orientation in both a turnClockwise method as a turnCounterClockwise method. This is not really elegant..

    Then I thought that it might be possible to put my elements in an arraylist or something like that and save them in a clockwise sequence like "up","right","down","left".
    Then I would use a method to get the next or previous element depending on how i'm turning. But I don't know how to retrieve the last element when my index is zero (or vice versa). This is the case when I turn counter-clockwise from "up" to "left". Is this even possible with an array list or should I use something else? Alternative, more elegant solutions are also welcome.

    Thanks!

  2. #2
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: get a previous or following element with a indentifier

    I think there's a way to do this with enums but I'm not sure.

    What I might do is, rather than have a variable to keep track of direction, have two int variables for x velocity and y velocity of the robot. Depending on the specifics of your problem, this may or may not be a good way of doing it, but here's how you'd do it:

    The four directions and their corresponding (xvel, yvel) pairs would be:
    right: (1, 0)
    down: (0, 1)
    left: (-1, 0)
    up: (0, -1)

    So, to rotate clockwise, you would do the following:
    Java Code:
    int temp=xvel;
    xvel=-yvel;
    yvel=temp;
    And, to rotate counterclockwise, you would do the following:
    Java Code:
    int temp=yvel;
    yvel=-xvel;
    xvel=temp;
    If it's necessary for you to have a String ("up", "down", "left", or "right") associated with each direction, then using an enum would probably be better solution, but I don't remember how to do that at the moment.

  3. #3
    marcosol is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default Re: get a previous or following element with a indentifier

    thanks man thats a really nice way to solve it!

  4. #4
    marcosol is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default Re: get a previous or following element with a indentifier

    Now I'm using int orientation (0,1,2,3) fo up right down left. I'm turning like this

    public void turnClockwise (){
    assert isValidEnergyLevel((energyLevel-100),getMAX_ENERGYLEVEL());
    setOrientation(this.orientation+1);
    withdrawEnergy(100);
    }
    public void turnCounterClockwise (){
    assert isValidEnergyLevel((energyLevel-100),getMAX_ENERGYLEVEL());
    setOrientation(this.orientation-1);
    withdrawEnergy(100);
    }

    public void setOrientation(int orientation) {
    this.orientation = Math.abs(orientation)%4;
    }
    It fits better with the GUI that I'm using.

Similar Threads

  1. How to get previous line rather than next?
    By Cylab in forum New To Java
    Replies: 5
    Last Post: 08-11-2010, 08:14 AM
  2. Replies: 1
    Last Post: 02-08-2010, 02:33 PM
  3. compare newly added Vector Element with previous elements
    By nidhirastogi in forum Advanced Java
    Replies: 10
    Last Post: 09-10-2008, 02:32 AM
  4. add previous jar library to new one
    By Farzaneh in forum Eclipse
    Replies: 1
    Last Post: 08-23-2008, 03:31 PM
  5. Next and Previous Buttons
    By JavaNewb in forum New To Java
    Replies: 1
    Last Post: 05-09-2008, 02:23 AM

Tags for this Thread

Posting Permissions

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