IN JAVA In this project you will create a robot class. The robot objects move wi
ID: 3852631 • Letter: I
Question
IN JAVA
Explanation / Answer
Here is the complete code with output. Please post a comment in case of any issues, I shall respond. Please don't forget to rate the answer if it helped. Thank you very much.
Constants.java
public interface Constants {
public static final int RIGHTBOUNDARY = 26;
public static final int LEFTBOUNDARY = 0;
public static final int TOPBOUNDARY = 26;
public static final int BOTTOMBOUNDARY = 0;
}
Robot.java
public class Robot {
private int x;
private int y;
private char payload;
private char grid[][];
public Robot() {
x = 0;
y = 0;
payload = ' ';
}
public Robot(int x, int y, char payload) {
this.x = x;
this.y = y;
this.payload = payload;
}
public void setGrid(char grid[][])
{
this.grid = grid;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public char getPayload() {
return payload;
}
public void setPayload(char payload) {
this.payload = payload;
}
public void print() {
System.out.println("X: " + x + " Y: " + y + " Payload: " + payload + " ");
}
public boolean pickup(int lx, int ly) {
if(lx != x && ly != y) {
System.out.println("Not at (" + lx + "," + ly + ")");
return false;
}
char locationPayload = grid[lx][ly];
if(locationPayload == ' ') {
System.out.println("No load at this location");
return false;
} else {
if(payload == ' ') {
payload = locationPayload;
grid[lx][ly] = ' ';
return true;
}
}
return false;
}
public boolean dropOff(int lx, int ly) {
if(lx != x && ly != y) {
System.out.println("Not at (" + lx + "," + ly + ")");
return false;
}
if(payload != ' ') {
grid[lx][ly] = payload;
payload = ' ';
return true;
}
return false;
}
public void moveRight() {
if(x < Constants.RIGHTBOUNDARY)
x++;
if( x == Constants.RIGHTBOUNDARY)
System.out.println("Right boundary reached");
}
public void moveLeft() {
if( x > Constants.LEFTBOUNDARY)
x--;
if(x == Constants.LEFTBOUNDARY)
System.out.println("Left boundary reached");
}
public void moveUp() {
if( y < Constants.TOPBOUNDARY)
y++;
if(y == Constants.TOPBOUNDARY)
System.out.println("Top boundary reached");
}
public void moveDown() {
if( y > Constants.BOTTOMBOUNDARY)
y--;
if(y == Constants.BOTTOMBOUNDARY)
System.out.println("Bottom boundary reached");
}
public boolean moveTo(int lx, int ly) {
if(lx >= Constants.LEFTBOUNDARY && lx <= Constants.RIGHTBOUNDARY && //check if within boundaries
ly >= Constants.BOTTOMBOUNDARY && ly <= Constants.TOPBOUNDARY) {
while(x > lx)
moveLeft();
while(x < lx)
moveRight();
while(y > ly)
moveDown();
while(y < ly)
moveUp();
return true;
} else {
System.out.println("Can't moveTo (" + lx + "," + ly +") , out of boundary.");
return false;
}
}
}
TestRobot.java
public class TestRobot {
public static void print2D(char grid[][]) {
System.out.println("Displaying Grid ");
for(int row=Constants.TOPBOUNDARY - 1; row > Constants.BOTTOMBOUNDARY; row--)
{
System.out.println(" ");
for(int col= Constants.LEFTBOUNDARY + 1; col< Constants.RIGHTBOUNDARY; col++)
{
if(grid[row][col] == ' ')
System.out.print("---|");
else
System.out.print("-" + grid[row][col] + "-|");
}
}
System.out.println(" ");
}
public static void clear(char grid[][])
{
System.out.println("Clearing Grid ");
for(int row = Constants.TOPBOUNDARY -1 ; row > Constants.BOTTOMBOUNDARY; row--)
{
for(int col= Constants.LEFTBOUNDARY ; col< Constants.RIGHTBOUNDARY; col++)
{
if(grid[row][col] != ' ')
{
Robot r = new Robot(); //create a robot here
r.setGrid(grid);
r.moveTo(row, col);
r.pickup(row, col);
//after this line, the Robot object r is out of scope and gets deleted
}
}
}
System.out.println(" ");
}
private static void initialize(char grid[][])
{
for(int row=Constants.TOPBOUNDARY-1; row >= Constants.BOTTOMBOUNDARY; row--)
{
for(int col= Constants.LEFTBOUNDARY ; col< Constants.RIGHTBOUNDARY; col++)
{
grid[row][col] = ' ';
}
}
}
public static void main(String[] args) {
char grid[][] = new char[26][26];
initialize(grid);
//place 'B' and 'C' on grid
grid[10][8] = 'B';
grid[22][4] = 'C';
print2D(grid);
Robot r1 = new Robot();
Robot r2 = new Robot(0, 0 , ' ');
//set robots to use the grid
r1.setGrid(grid);
r2.setGrid(grid);
System.out.println("Printing location of R1: ");
r1.print();
System.out.println("Printing location of R2: ");
r2.print();
System.out.println("Moving R1 to (25,26) and R2 to (15,3)");
r1.moveTo(25, 26);
r2.moveTo(15, 3);
System.out.println("Printing location of R1: ");
r1.print();
System.out.println("Printing location of R2: ");
r2.print();
System.out.println("direct R1 to pickup B at 10,8 and drop at 20,20");
r1.moveTo(10, 8);
r1.pickup(10, 8);
System.out.println("Printing location of R1: ");
r1.print();
r1.moveTo(20, 20);
r1.dropOff(20, 20);
System.out.println("Printing location of R1: ");
r1.print();
print2D(grid);
System.out.println("direct R2 to pickup C at 22,4 and drop at 0,0");
r2.moveTo(22, 4);
r2.pickup(22, 4);
System.out.println("Printing location of R2: ");
r2.print();
r2.moveTo(0, 0);
r2.dropOff(0, 0);
System.out.println("Printing location of R2: ");
r2.print();
print2D(grid);
clear(grid);
print2D(grid);
}
}
output
Displaying Grid
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|-C-|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|-B-|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Printing location of R1:
X: 0 Y: 0 Payload:
Printing location of R2:
X: 0 Y: 0 Payload:
Moving R1 to (25,26) and R2 to (15,3)
Top boundary reached
Printing location of R1:
X: 25 Y: 26 Payload:
Printing location of R2:
X: 15 Y: 3 Payload:
direct R1 to pickup B at 10,8 and drop at 20,20
Printing location of R1:
X: 10 Y: 8 Payload: B
Printing location of R1:
X: 20 Y: 20 Payload:
Displaying Grid
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|-C-|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|-B-|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
direct R2 to pickup C at 22,4 and drop at 0,0
Printing location of R2:
X: 22 Y: 4 Payload: C
Left boundary reached
Bottom boundary reached
Printing location of R2:
X: 0 Y: 0 Payload:
Displaying Grid
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|-B-|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Clearing Grid
Displaying Grid
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|