answer in Java only. (Points nearest to each other) Listing 8.3 gives a program
ID: 3749116 • Letter: A
Question
answer in Java only.
(Points nearest to each other) Listing 8.3 gives a program that finds two points in a two-dimensional space nearest to each other. Revise the program so it finds two points in a three-dimensional space nearest to each other. Use a two-dimensional array to represent the points. Test the program using the following points: *8.7 double[]I points1, 0, 3), (1, -1, -1), (4, 1, 1, 12, 0.5, 9, 13.5, 2, 1 5.5, 4, -0.5] (3, 1.5, 3 (1.5, 4, 2), 332 Chapter 8 Multidimensional Arrays The formula for computing the distance between two points (x1, y1, z1) and (x2, y2, z2) is V(12) -y)(2 21)Explanation / Answer
public class ClosestPoints { public static double distance(double[] arr1, double[] arr2) { return Math.sqrt(Math.pow(arr2[0]-arr1[0], 2) + Math.pow(arr2[1]-arr1[1], 2) + Math.pow(arr2[2]-arr1[2], 2)); } public static void main(String[] args) { double[][] points = {{-1, 0, 3}, {-1, -1, -1}, {4, 1, 1}, {2, 0.5, 9}, {3.5, 2, -1}, {3, 1.5, 3}, {-1.5, 4, 2}, {5.5, 4, -0.5}}; int ind1 = 0, ind2 = 1; for(int i = 0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.