Jump to content

Basic Java "Walker" Progam

magicman1358
//*****************************************************//Walker.java////Class that has two feet, and provides walking functionality//////***************************************************** import java.awt.Image;import java.awt.Graphics;public class Walker{  public static final int PIXELS_PER_INCH = 6;  private Foot leftFoot, rightFoot;  private int stepLength;  private int stepsCount;  // Constructor  public Walker(int x, int y, Image leftPic, Image rightPic)  {    leftFoot =  new Foot(x, y - PIXELS_PER_INCH * 4, leftPic);    rightFoot = new Foot(x, y + PIXELS_PER_INCH * 4, rightPic);    stepLength = PIXELS_PER_INCH * 12;  }  // Returns the left foot  public Foot getLeftFoot()  {    return leftFoot;  }  // Returns the right foot  public Foot getRightFoot()  {    return rightFoot;  }  // Makes first step, starting with the left foot  public void firstStep()  {    leftFoot.moveForward(stepLength);    stepsCount = 1;  }  // Makes next step  public void nextStep()  {    if (stepsCount % 2 == 0)  // if stepsCount is even      leftFoot.moveForward(2 * stepLength);    else      rightFoot.moveForward(2 * stepLength);    stepsCount++;  // increment by 1  }  // Stops this walker (brings its feet together)  public void stop()  {    if(stepsCount % 2 == 0)    	leftFoot.moveForward(stepLength);    else    	rightFoot.moveForward(stepLength);  }  // Returns the distance walked  public int distanceTraveled()  {    return stepsCount * stepLength;  }  // Draws this walker  public void draw(Graphics g)  {		leftFoot.draw(g);		rightFoot.draw(g);  }} 

Hey guys I'm a high school student taking a AP Computer Science class and I'm kinda confused on some things. What I'm confused on is what the % is used for in lines 43 and 54?

Link to comment
Share on other sites

Link to post
Share on other sites

Many languages (such as Java) use the percentage symbol as the modulo operator: the remainder from the division of the first number by the second.

In this case it is used to figure out witch foot needs to be moved: if stepsCount is even (remainder of the division by 2 equals 0) move the leftFoot, otherwise move the right one.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×