ASAP
(Area of a convex polygon) A polygon is convex if it contains any line segment that connects two points of the polygon. Write a program that prompts the user to enter the number of points in a convex polygon, then enter the points clockwise, and display the area of the polygon. Sample Run Enter the number of points: 7 Enter the coordinates of the points: -12 0 -8.5 10 0 11.4 5.5 7.8 6 -5.5 0 -7 -3.5 -5.5 The total area is 244.575 Class Name: Exercise11_15 If you get a logical or runtime error, please refer https://liveexample.pearsoncmg.com/faq.html.
Code
import java.util.*;
class Exercise11_15{
public static double Area(double x[], double y[], int n){
double ans = 0.0;
for(int i = 0; i < n; i++){
ans += x[i] * y[(i + 1) % n] - y[i] * x[(i + 1) % n];
}
return Math.abs(ans / 2.0);
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of points : ");
int n = sc.nextInt();
double[] x = new double[n];
double[] y = new double[n];
for(int i = 0; i < n; i++){
x[i] = sc.nextDouble();
y[i] = sc.nextDouble();
}
System.out.println("The total area is " + Area(x, y, n));
}
}
Output
Get Answers For Free
Most questions answered within 1 hours.