Question

In Java: (Area of a convex polygon) A polygon is convex if it contains any line...

In Java:

(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

Homework Answers

Answer #1
import java.util.Scanner;

public class Exercise11_15 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the number of points: ");
        int numPoints = in.nextInt();
        double[] x = new double[numPoints];
        double[] y = new double[numPoints];
        System.out.println("Enter the coordinates of the points:");
        for (int i = 0; i < numPoints; i++) {
            x[i] = in.nextDouble();
            y[i] = in.nextDouble();
        }

        double area = 0;
        for (int i = 0; i < numPoints; i++) {
            area += x[i] * y[(i + 1) % numPoints] - y[i] * x[(i + 1) % numPoints];
        }
        area = Math.abs(area / 2);
        System.out.printf("The total area is %.3f\n", area);
    }
}

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
Design a Java class named Polygon that contains:  A private int data field named numSides...
Design a Java class named Polygon that contains:  A private int data field named numSides that defines the number of sides of the polygon. The default value should be 4.  A private double data field named sideLength that defines the length of each side. The default value should be 5.0.  A private double data field named xCoord that defines the x-coordinate of the center of the polygon. The default value should be 0.0.  A private double...
Write a program in Java that: 1. will prompt user with a menu that contains options...
Write a program in Java that: 1. will prompt user with a menu that contains options to: a. Add a To-Do Item to a todo list. A To-Do Item contains: i. An arbitrary reference number (4; 44,004; etc.) ii. A text description of the item ("Pick up dry cleaning", etc.) iii. A priority level (1, 2, 3, 4, or 5) b. View an item in the list (based on its reference number) c. Delete an item from the list (based...
A Shipping company uses the following function to display the flat rate cost (in dollars) of...
A Shipping company uses the following function to display the flat rate cost (in dollars) of shipping based on the weight of a package (in pounds). Cost function c(w) Cost Weight $3.5 0 < w < = 1 $5.5 1 < w <= 3 $8.5 3 < w <= 10 $10.5 10 < w <= 20 Can’t be shipped 20 < w Write an Assembly program that prompts the user to enter the weight of the package and display the...
(Use Java ) please write comment on every line I need to understand the code Problem...
(Use Java ) please write comment on every line I need to understand the code Problem Description: Write a program that prompts the user to enter a point (x, y) and checks whether the point is within the rectangle centered at (0, 0) with width 10 and height 5. For example, (2, 2) is inside the rectangle and (6, 4) is outside the rectangle, as shown in the Figure. (Hint: A point is in the rectangle if its horizontal distance...
This is in java and you are not allowed to use Java API classes for queues,...
This is in java and you are not allowed to use Java API classes for queues, stacks, arrays, arraylists and linkedlists. You have to write your own implementations for them. You should construct a BST by inserting node values starting with a null tree. You can re-use the code for the insert method given in the sample code from the textbook. -insert method is provided below Your code should have a menu driven user interface at the command line with...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer no greater than 15. The program should then display a square on the screen using the character ‘X’. The number entered by the user will be the length of each side of the square. For example, if the user enters 5, the program should display the following:       XXXXX       XXXXX       XXXXX       XXXXX       XXXXX INPUT and PROMPTS. The program prompts for an integer as follows: "Enter...
Java code Problem 1. Create a Point class to hold x and y values for a...
Java code Problem 1. Create a Point class to hold x and y values for a point. Create methods show(), add() and subtract() to display the Point x and y values, and add and subtract point coordinates. Tip: Keep x and y separate in the calculation. Create another class Shape, which will form the basis of a set of shapes. The Shape class will contain default functions to calculate area and circumference of the shape, and provide the coordinates (Points)...
Project 2 statement Please write this in JAVA. Please read this entire statement carefully before you...
Project 2 statement Please write this in JAVA. Please read this entire statement carefully before you start doing anything… This project involves implementing a simple university personnel management program. The program contains two different kinds of objects: students and faculty. For each object, the program stores relevant information such as university ID, name, etc. Different information is stored depending on the type of the object. For example, a student has a GPA, while a faculty has a title and department...
IN JAVA PLEASE Design and implement an ADT CreditCard that represents a credit card. The data...
IN JAVA PLEASE Design and implement an ADT CreditCard that represents a credit card. The data of the ADT should include Java variables for the customer name, the account number, the next due date, the reward points, and the account balance. The initialization operation should set the data to client-supplied values. Include operations for a credit card charge, a cash advance, a payment, the addition of interest to the balance, and the display of the statistics of the account. Be...
1) Write a java programming using a while loop where you will add numbers and when...
1) Write a java programming using a while loop where you will add numbers and when you press number 0 it will add all your numbers and display the results. Use Scanner object. Steps: 1) Declare variables integer called sum which is equal to zero   2) Declare Scanner object   3) Declare while condition where is true   4) Inside of that condition prompt the user to enter the numbers   5) Declare variable integer called number and input the number statement   6)...