Solve right triangles in Java

Description

The following code shows how to solve right triangles.

Example


/* ww  w  .  j  a  v a  2s  .  c  om*/
public class Main {
  public static void main(String[] args) {

    double a, b, c, angleA, radA;

    // Apllying Pythagoras' Theorem
    // Obtain sides from user
    System.out.println("Side c in terms of sides a and b");
    a = 10d;
    b = 20d;
    c = Math.sqrt((a * a) + (b * b));
    System.out.println("Side c = " + c);

    // Using side/angle formula
    System.out.println("Side c in terms of side b and angle A");
    b = 30d;
    angleA = 20d;
    radA = Math.toRadians(angleA);
    c = b * Math.tan(radA);
    System.out.println("Side c = " + c);
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Development »




Java Algorithms
Java Clipboard
Java Compiler
Java Desktop
Java Virtual Machine
Java Math
OS
Random
Java Robot
Java RuntimeMXBean
Java Timer
Java UUID
Java Internationalization