You are to develop a model for robots to save people in a Cartesian plane in cas
ID: 3856382 • 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.
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]
----------------------------------
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;
}
}
Explanation / Answer
package codeforces;
import java.util.*;
import java.io.*;
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(2, 3));
System.out.println("----------------------------------");
}
}
}
class Robot {
private String name;
private double x;
private double y;
public ArrayList<double[]> explore(double x, double y)
{
ArrayList<double[]> path = new ArrayList<double[]>();
exploreHelper(this.x, this.y, x, y, path);// Call helper
return path;
}
// Recursively find all the possible paths
public void exploreHelper(double xStart, double yStart, double xEnd, double yEnd, ArrayList<double[]> path)
{
double[] a = {xStart, yStart};
path.add(a);// Add to path
//Reached destination
if (xStart == xEnd && yStart == yEnd)
{
for(double[] ar: path)
{
System.out.println(ar[0] + " " + ar[1]);
}
System.out.println("*******************");
}
if (xStart < xEnd && yStart < yEnd)
{
exploreHelper(xStart + 1, yStart, xEnd, yEnd, path);// Move right
path.remove(path.size() - 1);// This path not required remove from list
exploreHelper(xStart, yStart + 1, xEnd, yEnd, path);// Move Up
path.remove(path.size() - 1);//This path not required remove from list
exploreHelper(xStart + 1, yStart + 1, xEnd, yEnd, path);// 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;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.