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

Write/Create some code that calculates the perimeter of a polygon. The points of

ID: 642769 • Letter: W

Question

Write/Create some code that calculates the perimeter of a polygon.

The points of the polygon will be stored in a file and this file will be passed on the command line arguments

The file itself will be a binary file containing integers

   The first integer in the file is the number of points contained in the file

   The remaining integers are the points, with the first integer being the x coordinate and the    second integer being the y coordinate

   There is an edge between each adjacent point and between the first point and the last point

   Each file contains at least 3 points

The perimeter of a polygon is the sum of the lengths of all of its edges

Use a double to store your perimeter and report the perimeter to the nearest 2 decimal points.

You MUST store your points in a struct.

The example tests do not form actual polygons but assume that they do.

Example. Assume that there is a file called polygon.txt. It will store the following information but in binary form. This example is just to give you a visualization of the data.

3

287 422

283 -981

781 647

./perimeter polygon.txt

  OUTPUT

The perimeter is 3648.30

Explanation / Answer

answer

import java.io.IOException;

public class polyper {

public static void main(String[] args) {
  

String strFilePath = args[0];

try
{
  
FileInputStream fin = new FileInputStream(strFilePath);


DataInputStream din = new DataInputStream(fin);


int i=0;
int count[]= new int[6];
int c=0,peri=0;
while(i<=6)
{
i = din.readInt();
count[c]=i;

System.out.println("int : " + i);
}

for(c=1;c<=6;c++)
{
peri=peri+count[c];
}
System.out.println(" The perimeter is"+peri);
din.close();

}
catch(FileNotFoundException fe)
{
System.out.println("FileNotFoundException : " + fe);
}
catch(IOException ioe)
{
System.out.println("IOException : " + ioe);
}
}
}