You can assume that the grid squares are numbered like a Cartesian (X-Y) graph,
ID: 3598119 • Letter: Y
Question
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 positive and must be expressed in whole numbers.PROGRAM MUST USE ARRAY LIST PLEASE WITH COMMENTS. sample run Below is a sample run for two cases. The command line is highlighted in bold font. ARRAYLIST RERnt uName$ java assignmenti yourName 8 917 robot is at position (8,9) coin is at position (1,7) robot travels 7 steps to the left robot travels 2 steps down robot travels a greater distance horizontally than vertically Do you want to continue? uname$ java assignmentl yourName 8 9 8 1 robot is at position (8,9 coin is at position (8,1) robot does not travel horizontally robot travels 8 steps down robot travels a greater distance vertically than horizontally Do you want to continue?Explanation / Answer
Implemented and tested the code. In the first iteration it will take the inputs from command line and second time onwards it will ask for continue message then enter 1 to contine 0 to quit. You have enter integers with space seperated.
code:
------------------
import java.util.ArrayList;
import java.util.Scanner;
public class Robot {
public static void main(String[] args) {
int c1, c2, r1, r2, d1,d2, count = 0;
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(0, Integer.parseInt(args[0]));
al.add(1, Integer.parseInt(args[1]));
al.add(2, Integer.parseInt(args[2]));
al.add(3, Integer.parseInt(args[3]));
boolean quit = false;
Scanner sc = new Scanner(System.in);
while(!quit) {
if(count!=0) {
al.add(0, sc.nextInt());
al.add(1, sc.nextInt());
al.add(2, sc.nextInt());
al.add(3, sc.nextInt());
}
count++;
System.out.println("Robot is at position ("+al.get(0)+","+al.get(1)+")");
System.out.println("Coin is at position ("+al.get(2)+","+al.get(3)+")");
if(al.get(2)==al.get(0) && al.get(3)==al.get(1)) {
System.out.println("Coin and robot are at same place!!");
}
else {
d1 = al.get(2) - al.get(0);
d2 = al.get(3) - al.get(1);
System.out.println(d1 + " "+d2);
if(d1 > 0) {
System.out.println("Robot travels "+d1+" steps to the right");
}
else if(d1<0) {
System.out.println("Robot travels "+d1*-1+" steps to the left");
}
else {
System.out.println("Robot does not travel horizontally");
}
if(d2 > 0) {
System.out.println("Robot travels "+d2+" steps up");
}
else if(d2<0) {
System.out.println("Robot travels "+d2*-1 +" steps down");
}
else {
System.out.println("Robot does not travel vertically");
}
if(d1>d2) {
System.out.println("Robot travels a greater distance horizontally than vertically");
}
else if(d1<d2) {
System.out.println("Robot travels a greater distance vertically than horizontally");
}
}
System.out.println("Do you want to continue? (1 - continue/0 - exit :)");
r1 = sc.nextInt();
if(r1==1) {
continue;
}
else {
break;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.