Question: Implement a class Robot that simulates a robot wandering on an infinite plane. The robot is located at a point with integer coordinates and faces
Implement a class Robot that simulates a robot wandering on an infinite plane. The robot is located at a point with integer coordinates and faces north, east, south, or west. Supple methods
public void turnLeft()
public void turnRight()
public void move()
public Point getLocation()
public String getDirection()
The turnLeft and turnRight methods change the direction but not the location. The move method moves the robot by one unit in the direction it is facing. The getDirection method returns a string "N", "E", "S", or "W".
Here is the tester file
import java.awt.Point; /** A class to test the Robot class. */ public class RobotTester { /** Tests the methods of the Robot class. @param args not used */ public static void main(String[] args) { // The direction of the robot. 0 - north, 1 - east, 2 - south, 3 - west // Creates a robot at Point x=5, y=5 facing east Robot robot = new Robot(new Point(5, 5), 1); robot.move(); // x=6, y=5, direction=1 robot.turnRight(); // 6, 5, 2 robot.move(); // 6, 4, 2 robot.move(); // 6, 3, 2 robot.turnRight(); // 6, 3, 3 robot.move(); // 5, 3, 3 robot.move(); // 4, 3, 3 robot.turnLeft(); // 4, 3, 2 robot.move(); // 4, 2, 2 robot.turnRight(); // 4, 2, 3 robot.turnRight(); // 4, 2, 0 robot.move(); // 4, 3, 0 Point location = robot.getLocation(); System.out.println("Location: " + location.x + ", " + location.y); System.out.println("Expected: 4, 3"); System.out.println("Direction: " + robot.getDirection()); System.out.println("Expected: N"); } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
