Problem in Java In this projectyou will create a robot class. The robot objects
ID: 3853592 • Letter: P
Question
Problem in Java
Explanation / Answer
Here you go... This question should have been partitioned into 2 questions actually. please chk the code and let me know if you have any difficulty..
/**
*
* @author Sam
*/
public class Robot {
final static int RIGHTBOUNDARY = 26;
final static int LEFTBOUNDARY = 0;
final static int TOPBOUNDARY = 26;
final static int BOTTOMBOUNDARY = 0;
private static char[][] grid;
private int x;
private int y;
private char payload;
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 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("The robot is on grid ("+x+","+y+") with payload: '" + payload + "'");
}
public boolean pickup(int lx, int ly) {
if (lx != x || ly != y ) {
System.out.println("Robot not at given location.");
return false;
}
if (payload != ' ') {
System.out.println("Robot already has a payload.");
return false;
}
if (grid[lx][ly] == ' ') {
System.out.println("Location has no payload.");
return false;
}
payload = grid[lx][ly];
grid[lx][ly] = ' ';
return true;
}
public boolean dropOff(int lx, int ly) {
if (lx != x || ly != y ) {
System.out.println("Robot not at given location.");
return false;
}
if (payload == ' ') {
System.out.println("Robot has no payload.");
return false;
}
grid[lx][ly] = payload;
payload = ' ';
return true;
}
public void moveRight() {
if (x < RIGHTBOUNDARY - 1)
x++;
else
System.out.println("Right boundary reached");
}
public void moveLeft() {
if (x > LEFTBOUNDARY)
x--;
else
System.out.println("Left boundary reached");
}
public void moveUp() {
if (y < TOPBOUNDARY - 1)
y++;
else
System.out.println("Top boundary reached");
}
public void moveDown() {
if (y > BOTTOMBOUNDARY)
y--;
else
System.out.println("Bottom boundary reached");
}
public boolean moveTo(int lx, int ly) {
if (lx >= LEFTBOUNDARY && lx < RIGHTBOUNDARY && y >= BOTTOMBOUNDARY && y < TOPBOUNDARY) {
int dx = lx - x;
int dy = ly - y;
if (dx > 0)
for (int i = 0; i < dx; i++)
moveRight();
else
for (int i = 0; i > dx; i--)
moveLeft();
if (dy > 0)
for (int i = 0; i < dy; i++)
moveUp();
else
for (int i = 0; i > dy; i--)
moveDown();
return true;
}
return false;
}
public static void print2D() {
for(int j = TOPBOUNDARY-1; j >= BOTTOMBOUNDARY; j--) {
for (int i = LEFTBOUNDARY; i < RIGHTBOUNDARY; i++)
System.out.print(grid[j][i] + "|");
System.out.println();
}
}
public static void main(String[] args) {
grid = new char[26][26];
for(int j = TOPBOUNDARY-1; j >= BOTTOMBOUNDARY; j--)
for (int i = LEFTBOUNDARY; i < RIGHTBOUNDARY; i++)
grid[j][i] = ' ';
grid[10][8] = 'B';
grid[22][4] = 'C';
print2D();
System.out.println("");
Robot R1 = new Robot();
R1.print();
Robot R2 = new Robot();
R2.print();
System.out.println("");
R1.moveTo(10, 8);
R1.pickup(10, 8);
R1.moveTo(20, 20);
R1.dropOff(20, 20);
print2D();
System.out.println("");
R2.moveTo(22, 4);
R2.pickup(22, 4);
R2.moveTo(0, 0);
R2.dropOff(0, 0);
print2D();
System.out.println("");
clear();
print2D();
}
public static void clear(){
for (int i = LEFTBOUNDARY; i < RIGHTBOUNDARY; i++)
for(int j = TOPBOUNDARY-1; j >= BOTTOMBOUNDARY; j--)
if (grid[i][j] != ' '){
Robot r = new Robot(i, j, ' ');
r.pickup(i, j);
r = null;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.