Calculate Triangle Area with Heron's Formula
From Java Example Source Code
Contents |
[edit] Overview - Calculate Triangle Area with Heron's Formula
This is a example of Java program.
[edit] Java Source Code
- Package: edu.berkeley.cs
- File: Heron.java
package edu.berkeley.cs; /** * Compute the area of a triangle using Heron's Formula. Code and values from Prof W. Kahan and Joseph D. Darcy. See http://www.cs.berkeley.edu/~wkahan/JAVAhurt.pdf. Derived from listing in Rick * Grehan's Java Pro article (October 1999). Simplified and reformatted by Ian Darwin. */ public class Heron { public static void main(String[] args) { // Sides for triangle in float float af, bf, cf; float sf, areaf; // Ditto in double double ad, bd, cd; double sd, aread; // Area of triangle in float af = 12345679.0f; bf = 12345678.0f; cf = 1.01233995f; sf = (af + bf + cf) / 2.0f; areaf = (float) Math.sqrt(sf * (sf - af) * (sf - bf) * (sf - cf)); System.out.println("Single precision: " + areaf); // Area of triangle in double ad = 12345679.0; bd = 12345678.0; cd = 1.01233995; sd = (ad + bd + cd) / 2.0d; aread = Math.sqrt(sd * (sd - ad) * (sd - bd) * (sd - cd)); System.out.println("Double precision: " + aread); } }
[edit] What Result You Can Get
Run the program, you will get:
Single precision: 0.0 Double precision: 972730.0557076167
[edit] Required External Library for this Java Example
Need nothing.
[edit] How to Run this Java Example Program
We recommend running this Java example program with Eclipse.
For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.
It's fairly easy.
[edit] Question & Answer
Any question?
Click edit and post your question or answer here.
