// Which of these coordinates is the farthest from the origin [-2,6], [5,0], [2,4],[6,-4]
package abc;
import java.util.ArrayList;
import java.util.Arrays;
public class FarthestCoordinate{
public static void main(String[] args) {
//add all characters to single string
String[]
coordinates = new String[] {"[-2,6]", "[5,0]", "[2,4]","[6,-4]"};
int lengthCoordinates = coordinates.length;
String
addAllChars = " ";
for(int i=0;i<lengthCoordinates;i++) {
System.out.println("The values at coordinates "+i+" are "+coordinates[i]);
addAllChars = addAllChars.concat(coordinates[i]);
}
String
data = addAllChars.trim();
System.out.println("All characters added to single string = "+ data);
//Create ArrayList of the integers only removing [,],-
int lengtOftheString = data.length();
int[] newArray = new int[lengtOftheString]; //this is useless as shown below
ArrayList<Integer>
newArrayList = new ArrayList<>(); //use this one
int count = 0;
for(int n=0;n<lengtOftheString;n++) {
if(!(((Character.toString(data.charAt(n))).equals("["))||((Character.toString(data.charAt(n))).equals("]"))
||((Character.toString(data.charAt(n))).equals("-"))||((Character.toString(data.charAt(n))).equals(",")))){
newArray[count]=Integer.parseInt(Character.toString(data.charAt(n)));
newArrayList.add(newArray[count]);
System.out.println("The numerical values in the array list at index "+count+" = "+newArray[count]);
count++;
}
}
System.out.println("Array = "+Arrays.toString(newArray)); // [2,
6, 5, 0, 2, 4, 6, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
System.out.println("Array List = "+newArrayList); //[2, 6, 5, 0, 2, 4, 6, 4]
//Find the distance of each coordinate
int count1 = 1;
int lengthFinal = newArrayList.size();
double[] distance = new double[lengthFinal];
for(int x=1;x<lengthFinal;x=x+2) {
distance[count1] = Math.sqrt(((newArrayList.get(x))*(newArrayList.get(x)))+((newArrayList.get(x-1))*(newArrayList.get(x-1))));
System.out.println("distance "+count1+" =
"+distance[count1]);
count1++;
}
//finding max distance
int lengthFinaldistance = distance.length; //this is 22, do not use
double finalmaxDistance = 0;
System.out.println("Final Distance length= "+lengthFinaldistance);
for(int y = 1; y<=count1;y++) {
System.out.println("Final Distance"+y+"---
"+distance[y]);
if(distance[y]>finalmaxDistance) {
finalmaxDistance=distance[y];
}
}
System.out.println("Final MAX Distance = "+finalmaxDistance);
//To find the coordinate with max distance
int[] result = new int[2]; //Use this array to display the
final result in coordinate form, this is necessary though
System.out.println("lengthFinal = "+lengthFinal);
int count2 = 1;
for(int x=1;x<lengthFinal;x=x+2) {
distance[count2] = Math.sqrt(((newArrayList.get(x))*(newArrayList.get(x)))+((newArrayList.get(x-1))*(newArrayList.get(x-1))));
System.out.println("to find the coordinate with max distance = "+x+" "+count2+" "+distance[count2]);
if(distance[count2]==finalmaxDistance) {
System.out.println("Final Distance at last step = "+finalmaxDistance+" "+distance[count2]);
System.out.println("The Coordinates are "+ ((newArrayList.get(x-1))+"
, "+((newArrayList.get(x)))));
//To display the final result in coordinate form
result[0]= newArrayList.get(x-1);
result[1]= newArrayList.get(x);
}
else {
}
count2++;
}
System.out.println("The final result as coordinates = "+ Arrays.toString(result)); //This returns [6, 4]
//Final Result
System.out.println("The final coordinates could be = ["+ result[0]+","+result[1]+"]");
System.out.println("The final coordinates could be = ["+"-"+ result[0]+","+result[1]+"]");
System.out.println("The final coordinates could be = ["+ result[0]+","+"-"+result[1]+"]");
System.out.println("The final coordinates could be = ["+"-"+ result[0]+","+"-"+result[1]+"]");
}
}
OutPut:
The values at coordinates 0 are [-2,6]
The values at coordinates 1 are [5,0]
The values at coordinates 2 are [2,4]
The values at coordinates 3 are [6,-4]
All characters added to single string = [-2,6][5,0][2,4][6,-4]
The numerical values in the array list at index 0 = 2
The numerical values in the array list at index 1 = 6
The numerical values in the array list at index 2 = 5
The numerical values in the array list at index 3 = 0
The numerical values in the array list at index 4 = 2
The numerical values in the array list at index 5 = 4
The numerical values in the array list at index 6 = 6
The numerical values in the array list at index 7 = 4
Array = [2, 6, 5, 0, 2, 4, 6, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Array List = [2, 6, 5, 0, 2, 4, 6, 4]
distance 1 = 6.324555320336759
distance 2 = 5.0
distance 3 = 4.47213595499958
distance 4 = 7.211102550927978
Final Distance length= 8
Final Distance1--- 6.324555320336759
Final Distance2--- 5.0
Final Distance3--- 4.47213595499958
Final Distance4--- 7.211102550927978
Final Distance5--- 0.0
Final MAX Distance = 7.211102550927978
lengthFinal = 8
to find the coordinate with max distance = 1 1 6.324555320336759
to find the coordinate with max distance = 3 2 5.0
to find the coordinate with max distance = 5 3 4.47213595499958
to find the coordinate with max distance = 7 4 7.211102550927978
Final Distance at last step = 7.211102550927978 7.211102550927978
The Coordinates are 6 , 4
The final result as coordinates = [6, 4]
The final coordinates could be = [6,4]
The final coordinates could be = [-6,4]
The final coordinates could be = [6,-4]
The final coordinates could be = [-6,-4]
No comments:
Post a Comment