User Tools

Site Tools


your_initials_in_beepers

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
your_initials_in_beepers [2019/08/17 12:11] frchrisyour_initials_in_beepers [2019/08/18 12:40] frchris
Line 4: Line 4:
 {{http://danshuster.com/apcs/karel25.jpg|}} {{http://danshuster.com/apcs/karel25.jpg|}}
  
-Here is some starter code for a new class.  Feel free to rename the class or the robot.<code java>mport kareltherobot.Directions; +Here is some starter code for a new class.  Feel free to rename the class or the robot. 
-import kareltherobot.World;+ 
 + 
 +[[http://csis.pace.edu/%7Ebergin/KarelJava2ed/KJRdocs/index.html|API]] (Application Programming Interface} 
 + 
 +<code java> 
 +import kareltherobot.*;
 /** /**
    
- * @author Chris Thiel+ * @author Your Name Here
  * @version Aug 19, 2019  * @version Aug 19, 2019
   A Class with a main method to make your initials in beepers   A Class with a main method to make your initials in beepers
  *    *  
  */  */
- +public class DrawInitials implements Directions  
-public class DrawInitials implements Directions { +
- + public static void main(String[] args)  
- public static void main(String[] args) { +
- World.setDelay(50); + World.setDelay(30); 
-                World.setSize(8,15);+ World.setSize(8,15);
  World.setVisible();  World.setVisible();
  task();  task();
  }  }
-    public static void task(){ +  
-      + public static void task() 
-     DrawInitials lisa = new MileWalker(1, 9, North, infinity); +
-       + 
-       lisa.putBeeper(); + Robot lisa = new Robot(5, 1, South, infinity); 
-       lisa.turnLeft(); + 
-       lisa.move(); + lisa.putBeeper();  
-     lisa.turnOff(); + lisa.move(); 
-    }+ lisa.putBeeper(); 
 + lisa.move(); 
 + lisa.putBeeper();  
 + lisa.move(); 
 + lisa.putBeeper(); 
 + lisa.move(); 
 + lisa.turnLeft(); 
 + lisa.putBeeper(); 
 + lisa.move(); 
 +                lisa.putBeeper(); 
 + lisa.move(); 
 + lisa.putBeeper();  
 + lisa.move(); 
 + lisa.turnOff(); 
 + }
 } }
 +</code>
 +
 +====== Making a method ======
 +With all those ''move()'' and ''putBeeper()'' statements it is hard to read the code.  You can make  a method in your class called ''placeSomeBeepers'':<code java>/**
 + * Places n beepers from Robot r ina striaght line
 + * @param r the Robot placing the beepers 
 + * (precondtion: Robot must have enough beepers)
 + * @param n the number of beepers to place
 + */
 + public static void placeSomeBeepers(Robot r, int n)
 + {
 + for (int i=0; i < n; i++)
 + {
 + r.putBeeper();
 + r.move();
 + }
 + }</code>
 +
 +Now all your ''task()'' method can look like this:<code java>public static void task()
 + {
  
 + Robot lisa = new Robot(5, 1, South, infinity);
  
 + placeSomeBeepers(lisa, 4);
 + lisa.turnLeft();
 + placeSomeBeepers(lisa, 3);
 + lisa.move();
 + lisa.turnOff();
 + }</code>
your_initials_in_beepers.txt · Last modified: 2019/08/18 12:40 by frchris

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki