Robot Java 1
Robot Java 1
Make sure you copy below two classes into separate files as mentioned. Do not
copy everything to a single file.
Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied
with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
// Robot.java
import java.awt.Point;
public class Robot {
// fields
private Point location;
private int direction;
// constants for representing each direction. it is a good practice to do
// like this.
public static final int NORTH = 0;
public static final int EAST = 1;
public static final int SOUTH = 2;
public static final int WEST = 3;
/**
* constructor taking initial location and direction
*
* @param location
* initial location
* @param direction
* initial direction
*/
public Robot(Point location, int direction) {
this.location = location;
// validating direction before setting
if (direction == NORTH || direction == EAST || direction == SOUTH
|| direction == WEST)
this.direction = direction;
}
/**
* constructor taking no parameters
*/
public Robot() {
// positioning robot at origin
location = new Point(0, 0);
// setting direction to north
direction = NORTH;
}
/**
* method to turn left
*/
public void turnLeft() {
direction--;
// wrapping around if necessary
if (direction < 0) {
direction = 3;
}
}
/**
* method to turn right
*/
public void turnRight() {
direction++;
// wrapping around if necessary
if (direction > 3) {
direction = 0;
}
}
/**
* method to move 1 unit distance in current direction
*/
public void move() {
// based on direction, updating x or y coordinate of location
/**
* Note: I'm using the standard coordinate system, in which y values
* increase as going up, and decrease when going down, so if you want
* this in opposite order (decrement when going up (north) and increment
* when going down (south)), then change location.y++; in north case
* below to location.y--; and location.y--; in south case to
* location.y++;
*/
switch (direction) {
case NORTH:
location.y++; // north
break;
case EAST:
location.x++; // east
break;
case SOUTH:
location.y--; // south
break;
case WEST:
location.x--; // west
break;
}
}
/**
* method returns the location
*
* @return the location Point object
*/
public Point getLocation() {
return location;
}
/**
* returns the direction as single character String
*
* @return N, E S of W indicating current direction
*/
public String getDirection() {
switch (direction) {
case NORTH:
return "N";
case EAST:
return "E";
case SOUTH:
return "S";
default:
return "W";
}
}
/**
* compare two Robots to see if they are same
*
* @param other
* other Robot
* @return true if two Robots have same location and same direction, else
* false
*/
public boolean equals(Robot other) {
return location.equals(other.location) && direction == other.direction;
}
/**
* returns a String representing robot's position and direction
*/
public String toString() {
return "Robot at (" + location.x + ", " + location.y + ") facing "
+ getDirection();
}
}
// Test.java
public class Test {
public static void main(String[] args) {
//creating a Robot
Robot robot=new Robot();
//displaying initial position
System.out.println(robot);
//moving north two units
robot.move();
robot.move();
//displaying current position
System.out.println("After moving two units to the north:");
System.out.println(robot);
robot2.turnLeft();
robot2.move();
robot2.move();
System.out.println("After turning left and moving two units:");
System.out.println(robot2);
robot2.turnLeft();
robot2.move();
robot2.move();
System.out.println("After turning left and moving two units:");
System.out.println(robot2);
}
}