Friday, September 30, 2016

// // Leave a Comment

Java Program To Calculate Area Of A Hexagon

The program below is the answer to Liang's Introduction to Java Programming (9th Edition) Chapter 2 Exercise 2.16.


/**
 *
 * @Author: Aghatise Osazuwa
 * Website: www.cscprogrammingtutorials.com
 *
 * Exercise 2.16 - Geometry: Area of a Hexagon
 *
 */ 

import java.util.Scanner;

public class Ex02_16 {

    public static void main(String[] args) {
        
        //Display Program Information
        System.out.println("This Program Calculates The Area Of A Hexagon.\n");

        //create Scanner 
        Scanner input = new Scanner(System.in);

        //prompt user to enter details
        System.out.println("Enter the length of a side of the hexagon:");
        double side = input.nextDouble();

        //calculate area using the formula ((3√3) ÷ 2) * the square of the length of a side
        double area = ((3 * (Math.pow(3, 0.5))) / 2) * side * side;
        //format area to two decimal places
        area = (int) (area * 10000) / 10000.0;

        //display the result
        System.out.println("The area of the hexagon is " + area + "\n");
    }
}

Click here to see other solutions to Introduction to Java Programming.

0 comments:

Post a Comment


DISCLAIMER: Opinions expressed in comments are those of the comment writers alone and does not reflect or represent the views of the post author. We reserve the right to delete any post deemed inappropriate or offensive and/or spammy. Please do not use abusive words/hate speech.

I Appreciate your valuable Feedback. So, Please DO NOT SPAM - Spam comments will be deleted immediately.

Don't use brand name in name field and you're not allowed to use links in comments unless it's necessary. Such comments will be removed immediately.

Thanks.