Compute the area of a triangle using Heron's Formula : Algorithms « Collections Data Structure « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Collections Data Structure » AlgorithmsScreenshots 
Compute the area of a triangle using Heron's Formula
Compute the area of a triangle using Heron's Formula
 

/** 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);
  }
}


           
         
  
Related examples in the same category
1.AnagramsAnagrams
2.Hanoi puzzleHanoi puzzle
3.FibonacciFibonacci
4.Sieve Sieve
5.Find connections using a depth-first searchFind connections using a depth-first search
6.Find connections using hill climbing.
7.Find optimal solution using least-cost
8.Find the lost keysFind the lost keys
9.Compute prime numbers
10.Print a table of fahrenheit and celsius temperatures 1
11.Print a table of fahrenheit and celsius temperatures 2
12.Print a table of Fahrenheit and Celsius temperatures 3Print a table of Fahrenheit and Celsius temperatures 3
13.Soundex - the Soundex Algorithm, as described by KnuthSoundex - the Soundex Algorithm, as described by Knuth
14.A programmable Finite State Machine implementation.
15.An extendable Graph datastructure.
16.Utilities for flop (floating-point operation) counting.
17.LU Decomposition
18.Reverse Polish Notation
19.Permutator test
20.implements the LZF lossless data compression algorithm
21.Linear Interpolation
22.Utility class for generating the k-subsets of the numbers 0 to n
23.VersionVersion
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.