Question

In: Computer Science

I am using NetBeans IDE Java for coding. I would like the code to be commented...

I am using NetBeans IDE Java for coding. I would like the code to be commented for a better understanding.

1. Implement a class Robot that simulates a robot wandering on an infinite plane. The robot is located at a point with integer coordinates and faces north, east, south, or west. Supply methods:

public void turnLeft()

public void turnRight()

public void move()

public Point getLocation()

public String getDirection()

The turnLeft and turnRight methods change the direction but not the location. The move method moves the robot by one unit in the direction it is facing. The getDirection method returns a string "N", "E", "S", or "W".

Solutions

Expert Solution

The code for the probem is provided below. code comments are provided for better understanding of the code.

Robot.java


public class Robot {
        /*instance variables to store xCoordinate info
         * yCoordinate info 
         * and direction of robot
         * */
        
        private double xCoordinate;
        private double yCoordinate;
        private String direction;
        
        
        
        
        /** COnstructor to initilise x and y coordinates with 0,0
         * @param direction
         */
        public Robot(String direction) {
                super();
                this.xCoordinate = 0;
                this.yCoordinate = 0;
                this.direction = direction;
        }


        /**
         * @param xCoordinate
         * @param yCoordinate
         * @param direction
         */
        public Robot(double xCoordinate, double yCoordinate, String direction) {
                super();
                this.xCoordinate = xCoordinate;
                this.yCoordinate = yCoordinate;
                this.direction = direction;
        }
        

        
        /**
         * @return the xCoordinate
         */
        public double getxCoordinate() {
                return xCoordinate;
        }


        /**
         * @param xCoordinate the xCoordinate to set
         */
        public void setxCoordinate(double xCoordinate) {
                this.xCoordinate = xCoordinate;
        }


        /**
         * @return the yCoordinate
         */
        public double getyCoordinate() {
                return yCoordinate;
        }


        /**
         * @param yCoordinate the yCoordinate to set
         */
        public void setyCoordinate(double yCoordinate) {
                this.yCoordinate = yCoordinate;
        }


        /**
         * @return the direction
         */
        public String getDirection() {
                return direction;
        }


        /**
         * @param direction the direction to set
         */
        public void setDirection(String direction) {
                this.direction = direction;
        }
        
        public void getLocation() {
                System.out.println("Direction of Robot: "+this.direction);
                System.out.println("X-Coordinates of Robot: "+this.xCoordinate);
                System.out.println("y-Coordinates of Robot: "+this.yCoordinate);
                
        }

/* Direction to keep in mind
 *                                                                 NORTH
 *                                                                       |
 *                                                WEST----------------EAST
 *                                                                       |
 *                                                                 SOUTH
 * 
 * */
public void turnLeft() {

        if (direction == "E") {
                this.direction = "N";
        }
        if (direction == "N") {
                this.direction = "W";
        }
        if (direction == "W") {
                this.direction = "S";
        }
        if (direction == "S") {
                this.direction = "E";
        }
}

public void turnRight() {

        if (direction == "E") {
                this.direction = "S";
        }
        if (direction == "S") {
                this.direction = "W";
        }
        if (direction == "W") {
                this.direction = "N";
        }
        if (direction == "N") {
                this.direction = "E";
        }
}

public void move() {
        if (direction == "E") {
                this.xCoordinate = this.xCoordinate + 1;
        }
        if (direction == "S") {
                this.yCoordinate = this.yCoordinate - 1;
        }
        if (direction == "W") {
                this.xCoordinate = this.xCoordinate - 1;
        }
        if (direction == "N") {
                this.yCoordinate = this.yCoordinate + 1;
        }
}



        
        
        

}

Main.java


public class Main {
        public static void main(String[] args) {
                //Creating new instance of robot
                Robot jerryRobot = new Robot("E");
                // printing location of robot
                jerryRobot.getLocation();
                //moving robot
                jerryRobot.move();
                //printing new location of robot
                jerryRobot.getLocation();
                
                System.out.println("\n");
                // new robot
                Robot tomRobot = new Robot(10,10,"S");
                tomRobot.move();
                tomRobot.getLocation();
                
        }

}

Output of the solution:

End of Solution


Related Solutions

Steps to making your Netbeans IDE (Interactive Development Environment): Download and install Netbeans 8.2 IDE (Java...
Steps to making your Netbeans IDE (Interactive Development Environment): Download and install Netbeans 8.2 IDE (Java SE version only). If you do not have the latest Java installation to match, Netbeans will direct you to download the needed version of Java. If needed, download and install the latest Oracle Official Java Software Development Kit Standard Edition (Java SDK SE), and JUnit. I suggest you do this via the bundle referred to as The Java Development Kit (JDK). Download the architecture...
Java Program using Netbeans IDE Create class Date with the following capabilities: a. Output the date...
Java Program using Netbeans IDE Create class Date with the following capabilities: a. Output the date in multiple formats, such as MM/DD/YYYY June 14, 1992 DDD YYYY b. Use overloaded constructors to create Date objects initialized with dates of the formats in part (a). In the first case the constructor should receive three integer values. In the second case it should receive a String and two integer values. In the third case it should receive two integer values, the first...
Based on the scenario above, implement the Huffman coding algorithm using Java NetBeans. Assume the characters...
Based on the scenario above, implement the Huffman coding algorithm using Java NetBeans. Assume the characters and frequencies as listed below. The total number of nodes is n = 6.
Hi, I would like to test a java program. I am learning linked list and going...
Hi, I would like to test a java program. I am learning linked list and going to make a linked lists for integer nodes. For instance, I am going to add the numbers 12, 13, and 16 to the list and then display the list contents and add 15 to the list again and display the list contents and delete 13 from the list and display the list contents and lastly delete 12 from the list and display the list...
Hello, I am very new to 64-bit ARM assembly and would like the code to this...
Hello, I am very new to 64-bit ARM assembly and would like the code to this following question: The Fibonacci Sequence is a series of integers. The first two numbers in the sequence are both 1; after that, each number is the sum of the preceding two numbers. 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... For example, 1+1=2, 1+2=3, 2+3=5, 3+5=8, etc. The nth Fibonacci number is the nth number in this sequence, so...
Using JAVA and NETBEANS Assignment Content For this assignment, you will develop "starter" code. After you...
Using JAVA and NETBEANS Assignment Content For this assignment, you will develop "starter" code. After you finish, your code should access an existing text file that you have created, create an input stream, read the contents of the text file, sort and store the contents of the text file into an ArrayList, then write the sorted contents via an output stream to a separate output text file. Copy and paste the following Java™ code into a JAVA source file in...
Using NetBeans IDE, write a JavaFX application that allows theuser to choose insurance options. Use...
Using NetBeans IDE, write a JavaFX application that allows the user to choose insurance options. Use a ToggleGroup to allow the user to select only one of two insurance types—HMO (health maintenance organization) or PPO (preferred provider organization). Use CheckBoxes for dental insurance and vision insurance options; the user can select one option, both options, or neither option. As the user selects each option, display its name and price in a text field; the HMO costs $200 per month, the...
Write assembler code to print even numbers from 1-20. Submit code and screenshot. I am coding...
Write assembler code to print even numbers from 1-20. Submit code and screenshot. I am coding in NASM on a SASM system.
I would like to create a Memory Game using JCreator. But am unsure where to start....
I would like to create a Memory Game using JCreator. But am unsure where to start. Please help. I would like to start with a basic code so I can add and change designs and colors ect... Any help would be greatly appreciated. I do want to use pics such as .jpg in my code.
I have the following code for my java class assignment but i am having an issue...
I have the following code for my java class assignment but i am having an issue with this error i keep getting. On the following lines: return new Circle(color, radius); return new Rectangle(color, length, width); I am getting the following error for each line: "non-static variable this cannot be referenced from a static context" Here is the code I have: /* * ShapeDemo - simple inheritance hierarchy and dynamic binding. * * The Shape class must be compiled before the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT