Advertisement
seby_stephens

Lesson 4 Activity 1

Dec 10th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.17 KB | None | 0 0
  1. package Lesson4;
  2. import kareltherobot.Robot;
  3. import kareltherobot.World;
  4. //This program causes the robot to move until the left side is clear, once it is clear it does a back flip
  5.  
  6. public class Activity1Main extends Robot {
  7.     /**
  8.      * Constructor for Activity1Main class
  9.      * @param a
  10.      * @param s
  11.      * @param d
  12.      * @param b
  13.      * @ return - none
  14.      */
  15.     public Activity1Main(int a, int s, Direction d, int b){
  16.         super(a,s,d,b);
  17.     }
  18.     public static void main(String[] args){
  19.         World.readWorld("Lesson4World1.kwld");
  20.         World.setDelay(50);
  21.         World.setVisible();
  22.         //New super robot
  23.         Activity1Main gary = new Activity1Main(1,3,North,0);
  24.         //Calls on move method four times, but it is overridden
  25.         gary.move();
  26.         gary.move();
  27.         gary.move();
  28.         gary.move();
  29.         //Does a back flip
  30.         gary.backFlip();
  31.  
  32.     }
  33.  
  34.     /**
  35.      * Overrides move method to make the robot move only when the left is not clear
  36.      * @ param - none
  37.      * @ return - void
  38.      */
  39.     public void move(){
  40.         if(!(leftIsClear())){
  41.             super.move();
  42.         }
  43.     }
  44.  
  45.     /**
  46.      * Checks to see if left is clear by turning left and using frontIsClear method from Robot
  47.      * @ param - none
  48.      * @return - true if front is clear and false if it is not
  49.      */
  50.     public boolean leftIsClear(){
  51.         turnLeft();
  52.         if(frontIsClear()){
  53.             turnRight();
  54.             return true;
  55.         }
  56.         turnRight();
  57.         return false;
  58.     }
  59.  
  60.     /**
  61.      * Allows robot to do back flip
  62.      * @ param - none
  63.      * @ return - void
  64.      */
  65.     public void backFlip(){
  66.         turnLeft();
  67.         turnLeft();
  68.         turnLeft();
  69.         turnLeft();
  70.     }
  71.  
  72.     /**
  73.      * Allows robot to turn right
  74.      * @ param - none
  75.      * @ return - void
  76.      */
  77.     public void turnRight(){
  78.         int delay = World.delay();
  79.         //Sets delay to 0 so it can instantaneously turn right
  80.         World.setDelay(0);
  81.         turnLeft();
  82.         turnLeft();
  83.         //Delay set back to normal
  84.         World.setDelay(delay);
  85.         turnLeft();
  86.     }
  87.  
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement