Pages

Sunday, 3 July 2016

Chapter 4 Exercise 1, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

4.1 (Geometry: area of a pentagon) Write a program that prompts the user to enter the length from the center of a pentagon to a vertex and computes the area of the pentagon, as shown in the following figure. The formula for computing the area of a pentagon is
, where s is the length of a side. The side can be computed using the formula  where r is the length from the center of a pentagon to a vertex. Round up two digits after the decimal point. Here is a sample run:

Enter the length from the center to a vertex: 5.5
The area of the pentagon is 71.92




import java.util.Scanner;
 
public class ProgrammingEx4_1 {
 
 public static void main(String[] args) {
 
  Scanner input = new Scanner(System.in);
  System.out.print("Enter the length from the center to a vertex:");
 
  double r = input.nextDouble();
  double s = 2 * r * Math.sin(Math.PI / 5);
 
  double area = 5 * Math.pow(s, 2) / (4 * Math.tan(Math.PI / 5));
 
  area = Math.round(area * 100) / 100.0;
  System.out.println("The area of the pentagon is " + area);
 
 }
 
}

No comments:

Post a Comment