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
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...
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...
5) Create a Java program using Scanner: Write a program where you will enter the grade...
5) Create a Java program using Scanner: Write a program where you will enter the grade score for 5 classes, then it will display total points and average and it will display the grade based on percentage. Steps: 1) Declare variable integer for English, Chemistry, Java Programming, Physics and Math 2) Declare variable total and percentage 3) Create Scanner object and prompt the user to enter grades for each class and input grades after each class 4) Calculate total of...
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...
**JAVA LANGUAGE** Write a program that models an employee. An employee has an employee number, a...
**JAVA LANGUAGE** Write a program that models an employee. An employee has an employee number, a name, an address, and a hire date. A name consists of a first name and a last name. An address consists of a street, a city, a state (2 characters), and a 5-digit zip code. A date consists of an integer month, day and year. All fields are required to be non-blank. The Date fields should be reasonably valid values (ex. month 1-12, day...
JAVA (Don't make it too complicated) Write a program that prompts the user for the name...
JAVA (Don't make it too complicated) Write a program that prompts the user for the name of a text file. The file should consist of a sequence of integers, one integer per line. The program will read in each line (using nextLine()), parse it as an int (using Integer.parseInt()), and report the number of values and their average. The average should be computed as a double. Your program should do some basic exception handling: (1) If the file cannot be...
Write a java program that creates an integer array with 50 random values, prompts the user...
Write a java program that creates an integer array with 50 random values, prompts the user to enter the index of an element in the array between 0 and 49, then displays the corresponding element value. If the specified index is out of bounds, display an error message (e.g. “Out of Bounds”) and ask the user to enter another index. Use a while loop that will keep prompting the user until a valid input is received. To handle invalid inputs,...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT