Advertisement
calcpage

GWP2_BoxBug.java

Mar 26th, 2012
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.45 KB | None | 0 0
  1. /*
  2.  * AP(r) Computer Science GridWorld Case Study:
  3.  * Copyright(c) 2005-2006 Cay S. Horstmann (http://horstmann.com)
  4.  *
  5.  * This code is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation.
  8.  *
  9.  * This code is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * @author Cay Horstmann
  15.  * @author Chris Nevison
  16.  * @author Barbara Cloud Wells
  17.  */
  18.  
  19. import info.gridworld.actor.Bug;
  20.  
  21. /**
  22.  * A <code>BoxBug</code> traces out a square "box" of a given size. <br />
  23.  * The implementation of this class is testable on the AP CS A and AB exams.
  24.  */
  25. public class BoxBug extends Bug
  26. {
  27.     private int steps;
  28.     private int sideLength;
  29.  
  30.     /**
  31.      * Constructs a box bug that traces a square of a given side length
  32.      * @param length the side length
  33.      */
  34.     public BoxBug(int length)
  35.     {
  36.         steps = 0;
  37.         sideLength = length;
  38.     }
  39.  
  40.     /**
  41.      * Moves to the next location of the square.
  42.      */
  43.     public void act()
  44.     {
  45.         if (steps < sideLength && canMove())
  46.         {
  47.             move();
  48.             steps++;
  49.         }
  50.         else
  51.         {
  52.             turn();
  53.             turn();
  54.             steps = 0;
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement