You have to write a program that will take as input from the command line two pa
ID: 3882503 • Letter: Y
Question
You have to write a program that will take as input from the command line two pairs of coordinates: the location of the robot and the location of the gold coin. These must be whole number
1. echo the input
2. output the number of step (i.e, squares )that the robot has to travel horizontally and verticallt to reach the gold coin;
3. specify the orientation for the robot to travel both horizontally (i.e left or right)or and vertically (i.e up and down);
4. calculate and output which direction travelled( horizontal or vertical) is greater.
5. Implement the program using arrays and loops to let user decides when to quit the program
You can assume that the grid squares are numbered like a Cartesian(X-Y) graph, with positive X going horizontally to the right and positive Y going vertically upwards Input coordinates can be negative, but they must be whole numbers. The output distance must be possitive and must be expressed in whole numbers.
Java language to wirte this program
Explanation / Answer
JAVA CODE:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);
while (true) {
//Read the coordinates of the robot and gold
System.out.println("Enter the positions of robot and gold");
int x1 = obj.nextInt();
int y1 = obj.nextInt();
int x2 = obj.nextInt();
int y2 = obj.nextInt();
System.out.println("Robot is at (" + x1 + "," + y1 + ") , Gold at (" + x2 + "," + y2 + ")");
int steps = Math.abs(x1 - x2) + Math.abs(y1 - y2);
System.out.println("Number of steps is "+steps);
//Calculating the distance
double d = Math.sqrt(Math.abs(x1 - x2)*Math.abs(x1 - x2) + Math.abs(y1 - y2)*Math.abs(y1 - y2));
System.out.println("Distance between Robot and Gold is "+d);
System.out.print("Orientation : ");
if (x2 > x1) {
System.out.print("Right ");
} else if (x2 < x1) {
System.out.print("Left ");
}
if (y2 > y1) {
System.out.print("Up ");
} else if (y2 < y1) {
System.out.print("Down ");
}
System.out.println();
if (Math.abs(x2 - x1) > Math.abs(y2 - y1)) {
System.out.println("Horizontal distance is greater");
} else if (Math.abs(x2 - x1) < Math.abs(y2 - y1)) {
System.out.println("Vertical distance is greater");
} else {
System.out.println("Both horizontal and vertical distances are equal");
}
System.out.println("Do you want to continue?(1/0)");
int ch = obj.nextInt();
if (ch == 0) {
break;
}
}
}
}
OUTPUT :
** Due to some technical reasons I'm unable to attach the image of the output
OUTPUT :
run:
Enter the positions of robot and gold
0 0 5 5
Robot is at (0,0) , Gold at (5,5)
Number of steps is 10
Distance between Robot and Gold is 7.0710678118654755
Orientation : Right Up
Both horizontal and vertical distances are equal
Do you want to continue?(1/0)
1
Enter the positions of robot and gold
1 2 -1 -7
Robot is at (1,2) , Gold at (-1,-7)
Number of steps is 11
Distance between Robot and Gold is 9.219544457292887
Orientation : Left Down
Vertical distance is greater
Do you want to continue?(1/0)
0
BUILD SUCCESSFUL (total time: 9 seconds)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.