Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Lesson4;
- import kareltherobot.Robot;
- import kareltherobot.World;
- //This program causes the robot to move until the left side is clear, once it is clear it does a back flip
- public class Activity1Main extends Robot {
- /**
- * Constructor for Activity1Main class
- * @param a
- * @param s
- * @param d
- * @param b
- * @ return - none
- */
- public Activity1Main(int a, int s, Direction d, int b){
- super(a,s,d,b);
- }
- public static void main(String[] args){
- World.readWorld("Lesson4World1.kwld");
- World.setDelay(50);
- World.setVisible();
- //New super robot
- Activity1Main gary = new Activity1Main(1,3,North,0);
- //Calls on move method four times, but it is overridden
- gary.move();
- gary.move();
- gary.move();
- gary.move();
- //Does a back flip
- gary.backFlip();
- }
- /**
- * Overrides move method to make the robot move only when the left is not clear
- * @ param - none
- * @ return - void
- */
- public void move(){
- if(!(leftIsClear())){
- super.move();
- }
- }
- /**
- * Checks to see if left is clear by turning left and using frontIsClear method from Robot
- * @ param - none
- * @return - true if front is clear and false if it is not
- */
- public boolean leftIsClear(){
- turnLeft();
- if(frontIsClear()){
- turnRight();
- return true;
- }
- turnRight();
- return false;
- }
- /**
- * Allows robot to do back flip
- * @ param - none
- * @ return - void
- */
- public void backFlip(){
- turnLeft();
- turnLeft();
- turnLeft();
- turnLeft();
- }
- /**
- * Allows robot to turn right
- * @ param - none
- * @ return - void
- */
- public void turnRight(){
- int delay = World.delay();
- //Sets delay to 0 so it can instantaneously turn right
- World.setDelay(0);
- turnLeft();
- turnLeft();
- //Delay set back to normal
- World.setDelay(delay);
- turnLeft();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement