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

Point.java

public class Point {
private int x, y;
  
// default constructor
public Point()
{
setX(0);
setY(0);
}

// parameterized constructor
public Point(int x, int y) {
setX(x);
setY(y);
}
  
// Accessor/Getter methods
public int getX() {
return x;
}

public int getY() {
return y;
}
  
// Mutator/Setter methods
public void setX(int x) {
this.x = x;
}

public void setY(int y) {
this.y = y;
}
  
@Override
public String toString()
{
return("(" + getX() + ", " + getY() + ")");
}
}

Robot.java

public class Robot {
private Point location;
private String direction;
  
// default constructor, initializes location to (0, 0) and direction to North (N)
public Robot()
{
this.location = new Point();
this.direction = "N";
}
  
// parameterized constructor
public Robot(Point location, String direction) {
setLocation(location);
setDirection(direction);
}
  
// Accessor/Getter methods
public Point getLocation() {
return location;
}

public String getDirection() {
return direction;
}
  
// Mutator/Setter methods
public void setLocation(Point location) {
this.location = location;
}

public void setDirection(String direction) {
// if the direction passed on as argument is something other than (N/S/E/W), initialize the direction to "N"
if(!direction.equalsIgnoreCase("N") && !direction.equalsIgnoreCase("S") && !direction.equalsIgnoreCase("E") && !direction.equalsIgnoreCase("W"))
this.direction = "N";
else
this.direction = direction;
}
  
public void turnLeft()
{
if(direction.equalsIgnoreCase("N"))
direction = "W";
else if(direction.equalsIgnoreCase("W"))
direction = "S";
else if(direction.equalsIgnoreCase("S"))
direction = "E";
else if(direction.equalsIgnoreCase("E"))
direction = "N";
}
  
public void turnRight()
{
if(direction.equalsIgnoreCase("N"))
direction = "E";
else if(direction.equalsIgnoreCase("E"))
direction = "S";
else if(direction.equalsIgnoreCase("S"))
direction = "W";
else if(direction.equalsIgnoreCase("W"))
direction = "N";
}
  
public void move()
{
Point newLocation = new Point();
newLocation.setX(location.getX() + 1);
newLocation.setY(location.getY() + 1);
location = newLocation;
}
  
@Override
public String toString()
{
return("The Robot is at location " + location + " and is facing towards " + direction);
}
}

RobotTest.java (Driver/Main class)

public class RobotTest {
  
public static void main(String[] args) {
System.out.println("Initializing an instance of Robot using the default constructor..");
Robot r1 = new Robot();
System.out.println(r1);
System.out.println("\nInitializing another instance of Robot using the parameterized constructor..");
Robot r2 = new Robot(new Point(3, 4), "E");
System.out.println(r2);
  
System.out.println("\nRobot r1 on turning left..");
r1.turnLeft();
System.out.println(r1);
System.out.println("\nRobot r2 on turning right..");
r2.turnRight();
System.out.println(r2);
  
System.out.println("\nMoving robots r1 & r2..");
r1.move();
r2.move();
System.out.println("Robot 1: " + r1);
System.out.println("Robot 2: " + r2);
  
System.out.println("\nRobot r1 on turning right..");
r1.turnRight();
System.out.println(r1);
System.out.println("\nRobot r2 on turning left..");
r2.turnLeft();
System.out.println(r2);
}
}

******************************************************* SCREENSHOT *********************************************************


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