86.0k views
4 votes
Darth Vader wants to check that his TIE fighter pilots are patrolling adequately-sized regions of the galaxy. He has files of data that contain the patrol coordinates for each of his pilots. With the fear of being force choked, you have agreed to write a program that analyzes the data and determines the size of the area patrolled by the pilots in addition to providing the ability to search and sort the data.

User Alexvassel
by
5.2k points

1 Answer

7 votes

Answer:

Step-by-step explanation:

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class PilotAreaTest {

public static void main(String[] args) throws FileNotFoundException {

File file=new File("pilot_routes.txt");

Scanner obj=new Scanner(file);

double coor[][][]=new double[16][20][2];//at most 16 pilots

String pilot[]=new String[16];//store pilot names

int indices[]=new int[16];//to store the number coordinates entered

int index=0;

while(obj.hasNext())

{

//read a complete pilot information

String s[]=getStringArray(obj.nextLine());

//store the coordinates and pilot name

storeInArray(s, pilot, coor, indices, index);

index++;

}

//close the file input stream

obj.close();

//calculations

for(int i=0;i<index;i++)

{

//for each pilot

String pilot_name=pilot[i];

double area=calculateArea(coor, indices, i);

//print the information

System.out.println(pilot_name+"\t"+area);

}

}

static double calculateArea(double coor[][][], int indices[], int index)

{

double area=0;

for(int i=1; i<indices[index]; i++)

{

area+=((coor[index][i][0]+coor[index][i-1][0])*(coor[index][i][1]-coor[index][i-1][1]));

}

area=Math.abs(area)/2;

return area;

}

static String[] getStringArray(String s)

{

return s.split("[,\\s]");//regex expression to seperate s into string array on comma and space character

}

static void storeInArray(String ar[], String pilot[], double coor[][][], int indices[], int index)

{

pilot[index]=ar[0];

int row=0;

for(int i=1; i<ar.length; i+=2)

{

//i'th value is x coordinate and i+1 'th value is y coordinate

coor[index][row][0]=Double.parseDouble(ar[i]);

coor[index][row][1]=Double.parseDouble(ar[i+1]);

row++;//increment number of rows

}

indices[index]=row;//assign these row numbers to indices array

}

}

INPUT FILE:

You can check the attched image below for futher understanding

Darth Vader wants to check that his TIE fighter pilots are patrolling adequately-sized-example-1
Darth Vader wants to check that his TIE fighter pilots are patrolling adequately-sized-example-2
Darth Vader wants to check that his TIE fighter pilots are patrolling adequately-sized-example-3
Darth Vader wants to check that his TIE fighter pilots are patrolling adequately-sized-example-4
Darth Vader wants to check that his TIE fighter pilots are patrolling adequately-sized-example-5
User Jeremy DeGroot
by
5.1k points