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

Lab Exercise 06.1 ATC Utility USE JAVA Your textbook lists a class named FindNea

ID: 3905205 • Letter: L

Question

Lab Exercise 06.1

ATC Utility

USE JAVA

Your textbook lists a class named FindNearestPoints on page 296 the purpose of which is to find the two points closest to each other in a 2-dimensional space. For example, a map of a country will have a number of cities, each represented by an X-coordinate and a Y-coordinate and the program will find the two cities which are closest to each other.

The purpose of this exercise is to generalize this algorithm to finding the closest two points in a 3-dimensional space. For example, an air traffic controller has responsibility for a number of aircraft and each aircraft is represented by an X-coord, a Y-coord and a Z-coord.

This exercise will deal with a 3-dimensional array of coordinates, representing aircraft and will, for each aircraft in the list, find its 3 nearest neighboring aircraft. The result will be a list of 12 aircraft, each followed by the distances to each of its 3 nearest neighbors.

The fundamental declaration of the array will reflect the fact that the area around an airport is divided into geographically-based sectors in order to keep the number of aircraft assigned to any one controller to a minimum. So each aircraft will have 3 levels of identifying values, a sector number from 0 to 4, an aircraft ID number from 0 to 11 and 3 coordinates representing its latitude, longitude and altitude.

The array declaration will resemble the following:

int [] [] [] ac = new int [4][12][3];

where the first subscript (0..3) is a sector number, the second subscript is the aircraft ID number (0..11) per sector and the third subscript values are the location identifiers 0-latitude, 1-longitude and 2-altitude of the ac. Lat and long will be integer values between 1 and 128 while altitude will be an integer value between 1 and 350 (representing hundreds of feet). Thus a variable such as ac[2][7][2] will contain the number-2 value (altitude) for the number-7 aircraft in the number-2 sector.

The values of each subscript will be generated randomly since there will be 4 sectors of 12 aircraft each and 3 location values for each. Thus there will be 144 location values (48 latitudes, 48 longitudes and 48 altitudes) and 144 subscript values to locate the data in the array.

Since there are 4 sectors, no single controller will care about the aircraft in neighboring sectors so the program will prompt the user for a sector number between 1 and 4 (which will be converted to a subscript between 0 and 3). The result should be a list of text-lines, resembling the following:

Sector 2

Ac – 1          Lat – 17, Long – 46, Alt – 140

          Near01        Ac – 6    Lat – 17, Long – 42, Alt – 140

          Near02        Ac – 11  

          Near03        Ac – 4    

Ac – 2

p.296 FindNearestPoints

Explanation / Answer

Dear Student,

The code for the above program goes as:

package cheggy;

import java.util.Scanner;

public class Distance{

private static Scanner sc;

public static void main(String[] args) {

int [][][] arr = new int [4][12][3];

int sector;

sc = new Scanner(System.in);

while(true){

System.out.print("Enter the sector number(1 to 4):");

sector=sc.nextInt();

if(sector>=1&&sector<=4)

break;

else

System.out.println("Invalid Input! Please enter an integer between 1 and 4!");

}

int i=0,j=0,k=0;

//This nested loop is formed to fill random values in the array according to given constraints

while(i<4)

{

j=0;

while(j<12) {

k=0;

while(k<3){

//latitude and longitude

if(k==0||k==1)

arr[i][j][k]=(int) Math.ceil(Math.random() * 128);//random value from 1 to 128

//altitude

else if(k==2) {

arr[i][j][k]=(int) Math.ceil(Math.random() * 350);//random value from 1 to 350

}

k++;

}

j++;

}

i++;

}

//Now below we are finding the distances of all and with those

//distances calcuating the 3 nearest neighbours

i=0;j=0;k=0;

int closest=0,closer=0,close=0;//neighbours

double sqDistance=0,distance=0,minDistance=Double.MAX_VALUE;

while(i<12) {

j=0;

//for nearest neighbour

while(j<12)

{

sqDistance=Math.pow((arr[sector][i][0]-arr[sector][j][0]),2)

+Math.pow((arr[sector][i][1]-arr[sector][j][1]),2)

+Math.pow((arr[sector][i][2]-arr[sector][j][2]),2);//square of distance

distance=Math.pow(sqDistance, .5);//distance

if(distance!=0&&distance<minDistance)//to calculate min distance

{minDistance=distance;

closest=j;

}

j++;

}

//for second neighbour

j=0;minDistance=Double.MAX_VALUE;

while(j<12)

{

sqDistance=Math.pow((arr[sector][i][0]-arr[sector][j][0]),2)+Math.pow((arr[sector][i][1]-arr[sector][j][1]),2)+Math.pow((arr[sector][i][2]-arr[sector][j][2]),2);

distance=Math.pow(sqDistance, .5);

if(distance!=0&&distance<minDistance&&j!=closest)

{minDistance=distance;

closer=j;

}

j++;

}

//for third neighbour

j=0;minDistance=Double.MAX_VALUE;

while(j<12)

{

sqDistance=Math.pow((arr[sector][i][0]-arr[sector][j][0]),2)+Math.pow((arr[sector][i][1]-arr[sector][j][1]),2)+Math.pow((arr[sector][i][2]-arr[sector][j][2]),2);

distance=Math.pow(sqDistance, .5);

if(distance!=0&&distance<minDistance&&j!=closest&&j!=closer)

{minDistance=distance;

close=j;

}

j++;

}

//printing and formatting

System.out.println("Ac-"+(i+1)+": "+"Latitude:"+arr[sector][i][0]+" Longitude:"+arr[sector][i][1]+" Altitude:"+arr[sector][i][2]);

System.out.println("Near 1 "+": "+"Ac-"+(closest)+" Latitude:"+arr[sector][closest][0]+" Longitude:"+arr[sector][closest][1]+" Altitude:"+arr[sector][closest][2]);

System.out.println("Near 2 "+": "+"Ac-"+(closer)+" Latitude:"+arr[sector][closer][0]+" Longitude:"+arr[sector][closer][1]+" Altitude:"+arr[sector][closer][2]);

System.out.println("Near 3 "+": "+"Ac-"+(close)+" Latitude:"+arr[sector][close][0]+" Longitude:"+arr[sector][close][1]+" Altitude:"+arr[sector][close][2]);

System.out.println(" ");

i++;

}

}

}

Thank You.

I have tried to make the code self-explanatory.

In case of you face any problem or have any doubt feel free to ak.I would be glad to help :)