Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Lesson4;
- import kareltherobot.World;
- import kareltherobot.Robot;
- //This program checks to see if the right is clear in order to determine whether or not to fill the potholes
- public class Activity4Main extends Robot{
- /**
- * Constructor for Activity4Main class
- * @param a
- * @param s
- * @param d
- * @param b
- * @ return - none
- */
- public Activity4Main (int a, int s, Direction d, int b){
- super(a,s,d,b);
- }
- public static void main(String[] args){
- World.readWorld("Lesson4World3.kwld");
- World.setDelay(80);
- World.setVisible();
- //Creates a new robot
- Activity4Main gary = new Activity4Main(3,1,North, -1);
- gary.turnRight();
- //Runs overridden move method in for loop
- for(int i = 0; i<9;i++) {
- gary.move();
- }
- }
- /**
- * Overrides the move method to take into account if the robot needs to fill pot hole
- * @ param - none
- * @ return - void
- */
- public void move(){
- //If the right is not clear robot continues to move
- if(!(rightIsClear())){
- super.move();
- }
- //If the right is clear the robot fills the pothole and then continues to move
- else{
- super.move();
- putBeeper();
- turnBack();
- super.move();
- turnRight();
- super.move();
- }
- }
- /**
- * This checks to see if the right is clear
- * @ param - none
- * @return - true/false
- */
- public boolean rightIsClear(){
- turnRight();
- //If front is clear after the robot turns right, then the method returns true
- if(frontIsClear()){
- return true;
- }
- //If it is not clear the robot turns left again and the method returns false
- turnLeft();
- return false;
- }
- /**
- * Allows robot to turn right
- * @ param - none
- * @ return - void
- */
- public void turnRight(){
- int delay = World.delay();
- World.setDelay(0);
- turnLeft();
- turnLeft();
- World.setDelay(delay);
- turnLeft();
- }
- /**
- * Allows robot to turn back
- * @ param - none
- * @ return - void
- */
- public void turnBack(){
- turnLeft();
- turnLeft();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement