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

You are to develop a model for robots to save people in a Cartesian plane in cas

ID: 3857468 • Letter: Y

Question

You are to develop a model for robots to save people in a Cartesian plane in case of fire. A robot has a name and (x, y) coordinates, and two robots are considered identical if they have the same name. In order to save people, a robot has to support a method explore. This method takes as parameter a destination (destX, destY) coordinates; and it returns all the possible sequences of moves that will get the robot to that destination starting from its initial coordinates. Suppose that a robot is allowed to repeatedly make one of these three moves:

-move North (abbreviated "N"), which will increase y-coordinate by 1.

-move East (abbreviated "E"), which will increase x-coordinate by 1.

-move Northeast (abbreviated "NE"), which will increase both x-coordinate and y-coordinate by 1.

For example, starting from the origin (0, 0), these three different moves N, E and NE, would leave you in the following locations (0, 1), (1, 0) and (1, 1), respectively.

Write a class Robots that consists of a set of Robot objects. Robots’ class must support: addRobot method that takes (x, y) coordinates and a name. This method throws a checked exception RobotExistException, if there already exists a robot with the given name; otherwise a robot will be created with the given input and added to the set of robots. Moreover, Robots’ class must support for-each primitive, where the list of robots is iterated in an increasing alphabetical order with respect to their names.

Note that, performance is a key point in this problem. Your program should run in the most efficient way.

Feel free to use any existing data structure provided by Java.

Provided Robot class as follows:

public class Robot {

private String name;

private double x;

private double y;

public Robot(String name, double x, double y) {

this.name = name;

this.x = x;

this.y = y;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public double getX() {

return x;

}

public void setX(double x) {

this.x = x;

}

public double getY() {

return y;

}

public void setY(double y) {

this.y = y;

}

}

Below is a sample driver with its corresponding output.

Program Input:

public static void main(String[] args) { Robots robots = new Robots();

try {
robots.addRobot(1, 2, "r2"); robots.addRobot(2, 2, "r1"); robots.addRobot(3, 2, "r2");

}
catch(RobotExistException e) {

System.out.println("Exception: " + e.getMessage()); }

for(Robot r: robots) {
System.out.println("Robot " + r + " starts exploring:"); System.out.println(r.explore(2, 3)); System.out.println("----------------------------------");

} }

Output:

Exception: Robot r2 Already Exists Robot Robot r1 starts exploring:
[ N]

----------------------------------

Robot Robot r2 starts exploring:

[NE, EN, NE]

----------------------------------

Explanation / Answer

import java.util.*;
import java.io.*;


public class Robots
{
ArrayList<Robot> list = new ArrayList<Robot>();// Store all robots
  
// addRobot method as mentioned in problem
public void addRobot (double x, double y, String name) throws Exception
{
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getName() == name)
{
throw new Exception("RobotExistException");// throws exection if name exists
}
}
list.add(new Robot(name, x, y));// Add to list
}
  
public static void main(String[] args)
{
Robots robots = new Robots();
try {
robots.addRobot(1, 2, "r2"); robots.addRobot(2, 2, "r1"); robots.addRobot(3, 2, "r2");
}
catch(Exception e) {
System.out.println("Exception: " + e.getMessage()); }
for(Robot r: robots.list) {
System.out.println("Robot " + r + " starts exploring:");
System.out.println(r.explore(5, 5));
}
}
}

class Robot {

private String name;
private double x;
private double y;

public String explore(double x, double y)
{
ArrayList<double[]> path = new ArrayList<double[]>();
String[] route = new String[1];
  
exploreHelper(this.x, this.y, x, y, path, route);// Call helper
return route[0];
}

// Recursively find all the possible paths
public void exploreHelper(double xStart, double yStart, double xEnd, double yEnd, ArrayList<double[]> path, String[] route)
{
double[] a = {xStart, yStart};
path.add(a);// Add to path
//Reached destination
if (xStart == xEnd && yStart == yEnd)
{
int count = 0;
for(double[] ar: path)
{
//Formatting string
if (count != 0)
route[0] += "->";
if (route[0] != null)
route[0] += "(" + String.valueOf(ar[0]) + "," + String.valueOf(ar[1]) + ")";
else
route[0] = "(" + String.valueOf(ar[0]) + "," + String.valueOf(ar[1]) + ")";
count++;
}
route[0] += " ******************* ";
}
if (xStart < xEnd && yStart < yEnd)// Path is safe
{
  
exploreHelper(xStart + 1, yStart, xEnd, yEnd, path, route);// Move East
  
path.remove(path.size() - 1);// This path not required remove from list
exploreHelper(xStart, yStart + 1, xEnd, yEnd, path, route);// Move North
path.remove(path.size() - 1);//This path not required remove from list
exploreHelper(xStart + 1, yStart + 1, xEnd, yEnd, path, route);// Move NE
path.remove(path.size() - 1);//This path not required remove from list
}
  
}

public Robot(String name, double x, double y) {
this.name = name;
this.x = x;
this.y = y;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
}

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