Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program that accomplishes the following tasks: 1. Reads a positive

ID: 3529499 • Letter: W

Question

Write a Java program that accomplishes the following tasks: 1. Reads a positive integer n, followed by the (x, y) coordinates of n points, storing these values in a two-dimensional array named points with n rows and two columns. 2. Creates a two-dimensional array with n rows and n columns named distances, with distances[i][j] = distance from points[i] to points[j]. 3. Creates a one dimensional array of length n named averages, with averages[i] = avergae of row i of distances 4. Determines and prints which of the n points has the smallest average distance. NOTE: THIS IS NEEDING TO BE DONE IN jGRASP. WILL GIVE 5 STAR RATING TO WHOEVER GETS ANWSER CORRECT!!! Thanks in advance!

Explanation / Answer

import java.util.Scanner;

public class Coordinates {
public static void main(String[] args) {
int n;
Scanner input = new Scanner(System.in);
System.out.println("Enter the value of n");
n = input.nextInt();

int[][] points = new int[n][2];
for (int i = 0; i < n; i++) {
System.out.println("Enter the coordinates of point " + (i + 1));
for (int j = 0; j < 2; j++) {
points[i][j] = input.nextInt();

}
}

int[][] distance = new int[n][n];

for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
distance[i][j] = (int) Math.sqrt((Math.pow(points[j][0]
- points[i][0], 2))
+ (Math.pow(points[j][1] - points[i][1], 2)));
}

}

int[] average = new int[n];

int sum;

for (int i = 0; i < n; i++) {
sum = 0;
for (int j = 0; j < n; j++) {
sum = sum + distance[i][j];
}
average[i] = sum / n;
}
int smallest = average[0];

for (int i = 1; i < n; i++) {
if (average[i] < smallest)
smallest = average[i];
}
System.out.println("Smallest Average distance is : " + smallest);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote