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".
Point.java
public class Point {
private int x, y;
// default constructor
public Point()
{
setX(0);
setY(0);
}
// parameterized constructor
public Point(int x, int y) {
setX(x);
setY(y);
}
// Accessor/Getter methods
public int getX() {
return x;
}
public int getY() {
return y;
}
// Mutator/Setter methods
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
@Override
public String toString()
{
return("(" + getX() + ", " + getY() + ")");
}
}
Robot.java
public class Robot {
private Point location;
private String direction;
// default constructor, initializes location to (0, 0) and
direction to North (N)
public Robot()
{
this.location = new Point();
this.direction = "N";
}
// parameterized constructor
public Robot(Point location, String direction) {
setLocation(location);
setDirection(direction);
}
// Accessor/Getter methods
public Point getLocation() {
return location;
}
public String getDirection() {
return direction;
}
// Mutator/Setter methods
public void setLocation(Point location) {
this.location = location;
}
public void setDirection(String direction) {
// if the direction passed on as argument is something other than
(N/S/E/W), initialize the direction to "N"
if(!direction.equalsIgnoreCase("N") &&
!direction.equalsIgnoreCase("S") &&
!direction.equalsIgnoreCase("E") &&
!direction.equalsIgnoreCase("W"))
this.direction = "N";
else
this.direction = direction;
}
public void turnLeft()
{
if(direction.equalsIgnoreCase("N"))
direction = "W";
else if(direction.equalsIgnoreCase("W"))
direction = "S";
else if(direction.equalsIgnoreCase("S"))
direction = "E";
else if(direction.equalsIgnoreCase("E"))
direction = "N";
}
public void turnRight()
{
if(direction.equalsIgnoreCase("N"))
direction = "E";
else if(direction.equalsIgnoreCase("E"))
direction = "S";
else if(direction.equalsIgnoreCase("S"))
direction = "W";
else if(direction.equalsIgnoreCase("W"))
direction = "N";
}
public void move()
{
Point newLocation = new Point();
newLocation.setX(location.getX() + 1);
newLocation.setY(location.getY() + 1);
location = newLocation;
}
@Override
public String toString()
{
return("The Robot is at location " + location + " and is facing
towards " + direction);
}
}
RobotTest.java (Driver/Main class)
public class RobotTest {
public static void main(String[] args) {
System.out.println("Initializing an instance of Robot using the
default constructor..");
Robot r1 = new Robot();
System.out.println(r1);
System.out.println("\nInitializing another instance of Robot using
the parameterized constructor..");
Robot r2 = new Robot(new Point(3, 4), "E");
System.out.println(r2);
System.out.println("\nRobot r1 on turning left..");
r1.turnLeft();
System.out.println(r1);
System.out.println("\nRobot r2 on turning right..");
r2.turnRight();
System.out.println(r2);
System.out.println("\nMoving robots r1 & r2..");
r1.move();
r2.move();
System.out.println("Robot 1: " + r1);
System.out.println("Robot 2: " + r2);
System.out.println("\nRobot r1 on turning right..");
r1.turnRight();
System.out.println(r1);
System.out.println("\nRobot r2 on turning left..");
r2.turnLeft();
System.out.println(r2);
}
}
******************************************************* SCREENSHOT *********************************************************