Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem in Java In this projectyou will create a robot class. The robot objects

ID: 3853592 • Letter: P

Question

Problem in Java




In this projectyou will create a robot class. The robot objects move within a grid similar to the 3x3 grid shown below to pick up and carry the letters on the grid to different locations The grid size should be dedared as constants, use a 25x25 grid for our program. const int RIGHTBOUNDARY 26; const int LEFTBOUNDARY-0; const int TOPBOUNDARY-26; const int BOTTOMBOUNDARY-0; The Robot class must have the following data members and methods: a) The Robot class has three private data members a. b. c. An integertype variable for the x component of the location of the robot on the grid An integertype variable for the y component of the location of the robot on the grid A char type variable "payload" to hold letters as load b) The Robot class must have the following public member functions A default constructor places the robot at location (0,0) and payload to character space: "(empty) a. b. A constructor that receives three parameters to initialize the private data members. c. Include a set and get method for each of the private data members. d. Include a member function print()that prints the location of the robot on the grid as well as its load. e. Method pickup[int lx, int ly) with boolean return type. This method examines the current location of the robot and if not at location (Ix, y). it should display a

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;
                }
                  
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote