Question: I created Java coding for this but encounter error. Please help. Implement a class Robot that simulates a robot wandering on an infinite plane. The
I created Java coding for this but encounter error. Please help.
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". Please base the program on the tester file provided.
My Coding:
import java.awt.Point;
public class Robot {
//private members of the class
private int xLocation;
private int yLocation;
private String direction;
public Robot(int xLocation,int yLocation,String direction)
{
this.xLocation=xLocation;
this.yLocation=yLocation;
this.direction=direction;
}
//change direction
public void turnRight()
{
String newDirection=getRightDirection(getDirection());
direction=newDirection;
}
//change direction
public void turnLeft()
{
String newDirection=getLeftDirection(getDirection());
direction=newDirection;
}
//Move robot by one unit
public void move()
{
xLocation=xLocation+1;
yLocation=yLocation+1;
}
//Return the current location of the robot
public Point getLocation()
{
return new Point(xLocation,yLocation);
}
// The method getPoint returns the current direction of the robot
public String getDirection()
{
return direction;
}
public String getRightDirection(String Direction)
{
String newDirection;
if(direction=="N")
newDirection="E";
else if(direction=="S")
newDirection="W";
else if(direction=="E")
newDirection="S";
else
newDirection="N";
return newDirection;
}
public String getLeftDirection(String direction)
{
String newDirection;
if(direction=="N")
newDirection="W";
else if(direction=="S")
newDirection="E";
else if(direction=="E")
newDirection="N";
else
newDirection="S";
return newDirection;
}
}
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); (ERROR HERE) 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
