In: Computer Science
[20 marks] (TestRobot.java) Design a class named Robot. The Robot class has three private data fields: position x, position y, and the direction (east, south, west, and north). Assume that positive x points to the east and positive y points to the south, and initially x = 0, y = 0, direction = "east".
Create a no-arg constructor and another constructor which sets the three data fields. Create the accessors and mutators for the three data fields.
Create a method named forward that moves the robot along the current direction for the given distance. For example, if the robot is currently facing east, forward(10) will move the robot along the positive x direction for 10 pixels.
In the main method, create a Robot object, move it to the east for 20 pixels, then set the direction to north and move 30 pixels, and finally set the direction to west and move 40 pixels. Find and display the final x and y values and the direction of robot by calling its methods. For example, for the sample testing above, your program should display x = -20, y = -30, and direction = west.
//Robot class definition
class Robot{
//private members variables
private int posX; //posX for position x
private int posY; //posY for position y
private String direction; //direction for directions("north","south","east","west")
//Default constructor
Robot(){
posX = 0; //initially posX = 0
posY = 0; //initially posY = 0
direction = "east"; //initially direction ="east"
}
//Parameterized constructor
Robot(int posX, int posY, String direction){
this.posX = posX;
this.posY = posY;
this.direction = direction;
}
//mutators for setting postion x
public void setPosX(int posX){
this.posX = posX;
}
//mutators for setting postion y
public void setPosY(int posY){
this.posY = posY;
}
//mutators for setting direction
public void setDirection(String direction){
this.direction = direction;
}
//accessors for getting position x
public int getPosX(){
return this.posX;
}
//accessors for getting position y
public int getPosY(){
return this.posY;
}
//accessors for getting direction
public String getDirection(){
return this.direction;
}
//forward method for moving positions
public void forward(int pos){
if(this.direction=="east"){
posX+=pos; //if direction=="east" , move in positive x-direction
}
else if(this.direction=="west"){
posX-=pos; //if direction=="west" , move in negative x-direction
}
else if(this.direction=="south"){
posY+=pos; //if direction=="south" , move in positive y-direction
}
else if(this.direction=="north"){
posY-=pos; //if direction=="north" , move in negative y-direction
}
}
}
//TestRobot class definition
class TestRobot{
//program execution starts from here
public static void main(String [] args) {
//Robot class's object robot1 is declared
Robot robot1 = new Robot(); //initially no arguments are passed , thus default constructor will be called
//intial postion x = 0, y = 0, direction = "east"
//moving robot1 in positive x-direction
robot1.forward(20); //postion x = 20, y = 0, direction = "east"
robot1.setDirection("north"); //postion x = 20, y = 0, direction = "north"
//moving robot1 in negative y-direction
robot1.forward(30); //postion x = 20, y = -30, direction = "north"
robot1.setDirection("west"); //postion x = 20, y = -30, direction = "west"
//moving robot1 in negative x-direction
robot1.forward(40); //postion x = -20, y = -30, direction = "west"
//printing the required output
System.out.println("x = "+robot1.getPosX()+", y = "+robot1.getPosY()+", and direction = "+robot1.getDirection()+".");
}
}