Advertisement
seby_stephens

Lesson 4 Activity 4

Dec 16th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.37 KB | None | 0 0
  1. package Lesson4;
  2. import kareltherobot.World;
  3. import kareltherobot.Robot;
  4. //This program checks to see if the right is clear in order to determine whether or not to fill the potholes
  5.  
  6. public class Activity4Main extends Robot{
  7.     /**
  8.      * Constructor for Activity4Main class
  9.      * @param a
  10.      * @param s
  11.      * @param d
  12.      * @param b
  13.      * @ return - none
  14.      */
  15.     public Activity4Main (int a, int s, Direction d, int b){
  16.         super(a,s,d,b);
  17.     }
  18.  
  19.     public static void main(String[] args){
  20.         World.readWorld("Lesson4World3.kwld");
  21.         World.setDelay(80);
  22.         World.setVisible();
  23.         //Creates a new robot
  24.         Activity4Main gary = new Activity4Main(3,1,North, -1);
  25.         gary.turnRight();
  26.         //Runs overridden move method in for loop
  27.         for(int i = 0; i<9;i++) {
  28.             gary.move();
  29.         }
  30.     }
  31.  
  32.     /**
  33.      * Overrides the move method to take into account if the robot needs to fill pot hole
  34.      * @ param - none
  35.      * @ return - void
  36.      */
  37.     public void move(){
  38.         //If the right is not clear robot continues to move
  39.         if(!(rightIsClear())){
  40.             super.move();
  41.         }
  42.         //If the right is clear the robot fills the pothole and then continues to move
  43.         else{
  44.             super.move();
  45.             putBeeper();
  46.             turnBack();
  47.             super.move();
  48.             turnRight();
  49.             super.move();
  50.         }
  51.     }
  52.  
  53.     /**
  54.      * This checks to see if the right is clear
  55.      * @ param - none
  56.      * @return - true/false
  57.      */
  58.     public boolean rightIsClear(){
  59.         turnRight();
  60.         //If front is clear after the robot turns right, then the method returns true
  61.         if(frontIsClear()){
  62.             return true;
  63.         }
  64.         //If it is not clear the robot turns left again and the method returns false
  65.         turnLeft();
  66.         return false;
  67.     }
  68.  
  69.     /**
  70.      * Allows robot to turn right
  71.      * @ param - none
  72.      * @ return - void
  73.      */
  74.     public void turnRight(){
  75.         int delay = World.delay();
  76.         World.setDelay(0);
  77.         turnLeft();
  78.         turnLeft();
  79.         World.setDelay(delay);
  80.         turnLeft();
  81.     }
  82.  
  83.     /**
  84.      * Allows robot to turn back
  85.      * @ param - none
  86.      * @ return - void
  87.      */
  88.     public void turnBack(){
  89.         turnLeft();
  90.         turnLeft();
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement