Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help with a line of my code.

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with a line of my code.

    I am using this code to start a class ZigZagBug in a "game"
    known as GridWorld. We use this in our Programming class right now.

    Anyway, my code does what it is supposed to do, I know what the lines do, but
    how can I describe them with comments?

    Here is the code.

    import info.gridworld.actor.Bug;
     
    public class ZigZagBug extends Bug {
        int counter = 1;
     
        public ZigZagBug(){
            turn();
        }
     
        public void act(){
            if(canMove()){
                //This gets the adjacent location, of the direction that the bug is facing.
                //IE - Faces Right-North, the next location is the location directly in front of what the bug faces.
                moveTo(getLocation().getAdjacentLocation(getDirection()));
                setDirection(getDirection() + 90 + ((counter++)%2 * 180));
            }else{
                //If the bug cannot move, it turns 180 degrees and then moves again.
                setDirection(getDirection() + 180);
            }
        }
    }

    Now, what is..

    This for?
    int counter = 1;

    And this for?
    setDirection(getDirection() + 90 + ((counter++)%2 * 180));


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Help with a line of my code.

    I would hazard a guess that int counter = 1; is an integer variable called counter that's purpose is to keep count of something.

    If you look through the code you will see counter++. This means increment the value by 1.

    The second one is just as easy to work out.

    You are calling the setDirection method and passing to it the getDirection value plus 90 plus the incremented value of counter%2 times 180.

    When run it could look something like: setDirection(1420);
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. enum, value of: Why is this code line structured the way it is?
    By SPACE MONKEY in forum Java Theory & Questions
    Replies: 5
    Last Post: March 28th, 2011, 09:15 AM
  2. Missing a line or two of code,b ut dont know what it is.
    By backdown in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 20th, 2011, 09:13 AM
  3. Get a certain line in a JTextField
    By FlamingDrake in forum Java Theory & Questions
    Replies: 2
    Last Post: May 14th, 2010, 03:21 PM
  4. Reading a file line by line using the Scanner class
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM