This document contains code to calculate the area of a polygon given a list of points. It defines a Points class to store x and y coordinates, and a PolygonArea class with a getArea method that calculates the area by summing the x and y coordinates of neighboring points. The main method gets user input of points, calls getArea to calculate the total, and prints the result.
1. // Points.java
import java.util.ArrayList;
import java.util.Scanner;
public class Points{
double x;
double y;
Points(double xco, double yco){
x = xco;
y = yco;
}
}
// PolygonArea.java
import java.util.ArrayList;
import java.util.Scanner;
public class PolygonArea {
// funtion to determine area of polygon
public static double getArea(ArrayList coordinates) {
double sumxy = 0;
double sumyx = 0;
for (int i = 0; i < coordinates.size(); i++) {
int index = (i + 1) % coordinates.size();
Points point1 = coordinates.get(i);
Points point2 = coordinates.get(index);
sumxy += (point1.x * point2.y);
sumyx += (point1.y * point2.x);
}
double areaPolygon = 0.5 * (sumxy - sumyx);
if (areaPolygon > 0)
return areaPolygon;
else
return -areaPolygon;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter the number of the points: ");
2. int totalPoints = scan.nextInt();
System.out.print("Enter the coordinates of the points: ");
ArrayList coordinates = new ArrayList<>();
for (int i = 0; i < totalPoints; i++) {
coordinates.add(new Points(scan.nextDouble(), scan.nextDouble()));
}
System.out.println("The total area is " + getArea(coordinates));
}
}
/*
output:
Enter the number of the points: 3
Enter the coordinates of the points: 0 0 0 3 4 0
The total area is 6.0
*/
Solution
// Points.java
import java.util.ArrayList;
import java.util.Scanner;
public class Points{
double x;
double y;
Points(double xco, double yco){
x = xco;
y = yco;
}
}
// PolygonArea.java
import java.util.ArrayList;
import java.util.Scanner;
public class PolygonArea {
3. // funtion to determine area of polygon
public static double getArea(ArrayList coordinates) {
double sumxy = 0;
double sumyx = 0;
for (int i = 0; i < coordinates.size(); i++) {
int index = (i + 1) % coordinates.size();
Points point1 = coordinates.get(i);
Points point2 = coordinates.get(index);
sumxy += (point1.x * point2.y);
sumyx += (point1.y * point2.x);
}
double areaPolygon = 0.5 * (sumxy - sumyx);
if (areaPolygon > 0)
return areaPolygon;
else
return -areaPolygon;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter the number of the points: ");
int totalPoints = scan.nextInt();
System.out.print("Enter the coordinates of the points: ");
ArrayList coordinates = new ArrayList<>();
for (int i = 0; i < totalPoints; i++) {
coordinates.add(new Points(scan.nextDouble(), scan.nextDouble()));
}
System.out.println("The total area is " + getArea(coordinates));
}
}
/*
output:
Enter the number of the points: 3
Enter the coordinates of the points: 0 0 0 3 4 0
The total area is 6.0