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