In: Computer Science
JAVA
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. Supply 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".
1). ANSWER :
GIVENTHAT :
JAVA PROGRAM :
import java.awt.Point; public class Robot { private Point point; private String direction; public Robot() { point = new Point(0, 0); direction = "N"; } public void turnLeft() { if (direction.equals("N")) direction = "W"; else if (direction.equals("W")) direction = "S"; else if (direction.equals("S")) direction = "E"; else if (direction.equals("E")) direction = "N"; } public void turnRight() { if (direction.equals("N")) direction = "E"; else if (direction.equals("E")) direction = "S"; else if (direction.equals("S")) direction = "W"; else if (direction.equals("W")) direction = "N"; } public void move() { if (direction.equals("N")) point.y += 1; else if (direction.equals("E")) point.x += 1; else if (direction.equals("S")) point.y -= 1; else if (direction.equals("W")) point.x -= 1; } public Point getLocation() { return point; } public String getDirection() { return direction; } }