BIT115: LECTURE 10 SOLUTIONS // LECTURE 10 - IN

advertisement
1
BIT115: LECTURE 10 SOLUTIONS
// LECTURE 10 - IN-CLASS EXERCISE 10 - PART 2
import becker.robots.*;
class RobotSmarter extends Robot
{
RobotSmarter(City c, int st, int ave, Direction dir, int num)
{
super(c, st, ave, dir, num);
}
public void turnRight()
{
this.turnLeft();
this.turnLeft();
this.turnLeft();
}
public void moveMultiple(int numberOfIntersections)
{
int counter = 0;
while( counter < numberOfIntersections)
{
this.move();
counter = counter + 1; // value of counter plus 1)
}
}
}
public class ICE_06_Error_1_SOLUTION extends Object
{
public static void main(String[] args)
{
City wallville = new City();
RobotSmarter rob = new RobotSmarter(wallville, 4, 0, Direction.EAST, 0);
new Thing(wallville, 4, 0);
new Thing(wallville, 4, 3);
new Thing(wallville, 2, 3);
// Instead of giving rob 3 separate "move" commands,
// simply tell it that you want it to move 3 times
rob.moveMultiple(3);
rob.turnLeft();
rob.moveMultiple(2);
}
}
2
// LECTURE 10 - IN-CLASS EXERCISE 10- PART 3, 4
import becker.robots.*;
class RobotSmarter extends Robot
{
RobotSmarter(City c, int st, int ave, Direction dir)
{
super(c, st, ave, dir);
}
public void turnRight()
{
this.turnLeft();
this.turnLeft();
this.turnLeft();
}
public void moveMultiple(int numberOfIntersections)
{
int counter = 0;
while( counter < numberOfIntersections)
{
this.move();
counter = counter + 1;
}
}
}
public class ICE_06_Errors_Param extends Object
{
public static void main(String[] args)
{
City wallville = new City();
RobotSmarter rob = new RobotSmarter (wallville, 4, 0, Direction.EAST);
new Wall(wallville, 3, 8, Direction.WEST);
new Wall(wallville, 4, 6, Direction.NORTH);
new Wall(wallville, 7, 4, Direction.EAST);
rob.moveMultiple(5); // on this side, called an argument
rob.turnLeft();
rob.moveMultiple(5); // move two spaces forwards
rob.turnRight();
rob.moveMultiple(5); // move two spaces forwards
}
}
3
// LECTURE 10 - IN-CLASS EXERCISE 10 - PART 5, 6
import becker.robots.*;
import java.util.*;
class RobotSmarter extends Robot
{
RobotSmarter(City c, int st, int ave, Direction dir)
{
super(c, st, ave, dir);
}
public void turnRight()
{
this.turnLeft();
this.turnLeft();
this.turnLeft();
}
public void moveMultiple(int numberOfIntersections)
{
int counter = 0;
while( counter < numberOfIntersections)
{
if(this.frontIsClear())
{
this.move();
counter = counter + 1;
}
else
{
System.out.println("Arrg! Encountered unpassable obstacle at (Street " + this.getStreet() + ", Avenue "
+ this.getAvenue() + "). Altering directional coordinates to bypass.\n|");
this.turnLeft();
this.move();
this.turnRight();
this.move();
this.turnRight();
this.move();
this.turnLeft();
counter = counter + 1;
System.out.println("|__Woot! Circumnavigation successfull. Intersection count reset to (" + counter
+ "). Continuing forward to original coordinates.\n");
}
}
}
}
public class ICE_06_Errors_User_1_Solution extends Object
{
public static void main(String[] args)
4
{
Scanner keyboard = new Scanner(System.in);
City wallville = new City();
RobotSmarter rob = new RobotSmarter (wallville, 4, 0, Direction.EAST);
new Wall(wallville, 3, 8, Direction.WEST);
// new Wall(wallville, 4, 5, Direction.WEST);
new Wall(wallville, 4, 6, Direction.NORTH);
new Wall(wallville, 7, 4, Direction.EAST);
new Wall(wallville, 3, 6, Direction.NORTH);
new Wall(wallville, 1, 6, Direction.NORTH);
new Thing(wallville, -2, 12);
new Thing(wallville, -1, 10);
new Thing(wallville, 0, 8);
System.out.println("How many intersections forward would you like the robot to go?");
if( keyboard.hasNextInt() )
{
int numMoves = keyboard.nextInt();
System.out.println ("You entered a " + numMoves + ".");
rob.moveMultiple(numMoves);
rob.turnLeft();
rob.moveMultiple(numMoves);
rob.turnRight();
rob.moveMultiple(numMoves);
System.out.println("Final coordinates successfully reached at (Street " + rob.getStreet()
+ ", Avenue " + rob.getAvenue() + ").\n\nMAY THE FARCE BE WITH YOU!");
}
else
{
System.out.println ("You id-ee-yot! You did NOT type in a whole number!");
}
}
}
Download