Question

ASAP (Area of a convex polygon) A polygon is convex if it contains any line segment...

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.

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT