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

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....
I am using NetBeans IDE Java to code and I am seeking comment to the code...
I am using NetBeans IDE Java to code and I am seeking comment to the code as well (for better understanding). Magic squares. An n x n matrix that is filled with the numbers 1, 2, 3, …, n2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value. Write a program that reads in 16 values from the keyboard, displays them in a 4...
I am coding with NetBeans LDE Java. Write a program that reads a positive integer n...
I am coding with NetBeans LDE Java. Write a program that reads a positive integer n , prints all sums from 1 to any integer m 1≤m≤n . For example, if n=100, the output of your program should be The sum of positive integers from 1 to 1 is 1;       The sum of positive integers from 1 to 2 is 3;       The sum of positive integers from 1 to 3 is 6;       The sum of positive integers...
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...
I am trying to get this code to work but I am having difficulties, would like...
I am trying to get this code to work but I am having difficulties, would like to see if some one can solve it. I tried to start it but im not sure what im doing wrong. please explain if possible package edu.hfcc; /* * Create Java application that will create Fruit class and Bread class * * Fruit class will have 3 data fields name and quantity which you can change. * The third data field price should always...
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.
I WANT THIS CODE TO BE SIMPLE BECAUSE I AM NEW TO CODING AND I WANT...
I WANT THIS CODE TO BE SIMPLE BECAUSE I AM NEW TO CODING AND I WANT TO KNOW THE DETAILS! straight C Program. write a quiz game where a number of questions are asked and the user will win each time he answers right. and he loses each time he answers wrong. the type of questions asked can be about sports or anything else. You can put 5 quiz questions. It should only include the following: #include <stdio.h> and #include...
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...
Code using JAVA: must include "Main Method" for IDE testing! /** * Definition for a binary...
Code using JAVA: must include "Main Method" for IDE testing! /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public TreeNode searchBST(TreeNode root, int val) {    }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT