Advertisement
fiveriverflow

BoxBug

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