0% found this document useful (0 votes)
65 views6 pages

Robot Java 1

The document provides the code for two Java classes - Robot.java and Test.java - that simulate robot movement and direction. Robot.java defines a Robot class with fields to track location and direction, and methods to change direction, move, and report status. Test.java contains a main method that creates Robot objects, calls their methods, and prints output to test functionality. Comments are included in the code to explain how it works.

Uploaded by

maitham100
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views6 pages

Robot Java 1

The document provides the code for two Java classes - Robot.java and Test.java - that simulate robot movement and direction. Robot.java defines a Robot class with fields to track location and direction, and methods to change direction, move, and report status. Test.java contains a main method that creates Robot objects, calls their methods, and prints output to test functionality. Comments are included in the code to explain how it works.

Uploaded by

maitham100
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Here is the completed code for Robot.java and a tester program Test.java.

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

            //turning right and moving 2 steps


            robot.turnRight();
            robot.move();
            robot.move();
            //displaying current position
            System.out.println("After turning right and moving two units:");
            System.out.println(robot);
           

            //turning left and moving 3 steps


            robot.turnLeft();
            robot.move();
            robot.move();
            robot.move();
            //displaying current position
            System.out.println("After turning left and moving three units:");
            System.out.println(robot);
           

            //creating another robot and testing


            System.out.println("\nCreating a second Robot");
            Robot robot2=new Robot();
            System.out.println(robot2);
           

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

      }
}

You might also like