In: Computer Science
I am using NetBeans IDE Java for coding. I would like the code to be commented for a better understanding.
1. 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".
The code for the probem is provided below. code comments are provided for better understanding of the code.
Robot.java
public class Robot {
/*instance variables to store xCoordinate info
* yCoordinate info
* and direction of robot
* */
private double xCoordinate;
private double yCoordinate;
private String direction;
/** COnstructor to initilise x and y coordinates with 0,0
* @param direction
*/
public Robot(String direction) {
super();
this.xCoordinate = 0;
this.yCoordinate = 0;
this.direction = direction;
}
/**
* @param xCoordinate
* @param yCoordinate
* @param direction
*/
public Robot(double xCoordinate, double yCoordinate, String direction) {
super();
this.xCoordinate = xCoordinate;
this.yCoordinate = yCoordinate;
this.direction = direction;
}
/**
* @return the xCoordinate
*/
public double getxCoordinate() {
return xCoordinate;
}
/**
* @param xCoordinate the xCoordinate to set
*/
public void setxCoordinate(double xCoordinate) {
this.xCoordinate = xCoordinate;
}
/**
* @return the yCoordinate
*/
public double getyCoordinate() {
return yCoordinate;
}
/**
* @param yCoordinate the yCoordinate to set
*/
public void setyCoordinate(double yCoordinate) {
this.yCoordinate = yCoordinate;
}
/**
* @return the direction
*/
public String getDirection() {
return direction;
}
/**
* @param direction the direction to set
*/
public void setDirection(String direction) {
this.direction = direction;
}
public void getLocation() {
System.out.println("Direction of Robot: "+this.direction);
System.out.println("X-Coordinates of Robot: "+this.xCoordinate);
System.out.println("y-Coordinates of Robot: "+this.yCoordinate);
}
/* Direction to keep in mind
* NORTH
* |
* WEST----------------EAST
* |
* SOUTH
*
* */
public void turnLeft() {
if (direction == "E") {
this.direction = "N";
}
if (direction == "N") {
this.direction = "W";
}
if (direction == "W") {
this.direction = "S";
}
if (direction == "S") {
this.direction = "E";
}
}
public void turnRight() {
if (direction == "E") {
this.direction = "S";
}
if (direction == "S") {
this.direction = "W";
}
if (direction == "W") {
this.direction = "N";
}
if (direction == "N") {
this.direction = "E";
}
}
public void move() {
if (direction == "E") {
this.xCoordinate = this.xCoordinate + 1;
}
if (direction == "S") {
this.yCoordinate = this.yCoordinate - 1;
}
if (direction == "W") {
this.xCoordinate = this.xCoordinate - 1;
}
if (direction == "N") {
this.yCoordinate = this.yCoordinate + 1;
}
}
}
Main.java
public class Main {
public static void main(String[] args) {
//Creating new instance of robot
Robot jerryRobot = new Robot("E");
// printing location of robot
jerryRobot.getLocation();
//moving robot
jerryRobot.move();
//printing new location of robot
jerryRobot.getLocation();
System.out.println("\n");
// new robot
Robot tomRobot = new Robot(10,10,"S");
tomRobot.move();
tomRobot.getLocation();
}
}
Output of the solution:
End of Solution