The program below is the answer to Liang’s Introduction to Java Programming (9th Edition) Chapter 2 Exercise 2.16.
1 2 |
/**<br /> *<br /> * @Author: Aghatise Osazuwa<br /> * Website: www.cscprogrammingtutorials.com<br /> *<br /> * Exercise 2.16 - Geometry: Area of a Hexagon<br /> *<br /> */ <br /><br />import java.util.Scanner;<br /><br />public class Ex02_16 {<br /><br /> public static void main(String[] args) {<br /> <br /> //Display Program Information<br /> System.out.println("This Program Calculates The Area Of A Hexagon.n");<br /><br /> //create Scanner <br /> Scanner input = new Scanner(System.in);<br /><br /> //prompt user to enter details<br /> System.out.println("Enter the length of a side of the hexagon:");<br /> double side = input.nextDouble();<br /><br /> //calculate area using the formula ((3√3) ÷ 2) * the square of the length of a side<br /> double area = ((3 * (Math.pow(3, 0.5))) / 2) * side * side;<br /> //format area to two decimal places<br /> area = (int) (area * 10000) / 10000.0;<br /><br /> //display the result<br /> System.out.println("The area of the hexagon is " + area + "n");<br /> }<br />}<br /> |
Click here to…