Triangle

// This application focus on trigonometry calculation, so this file named Tr.java

import java.util.Scanner;

public class Tr {

    public static void main(String[] args) {
        try (Scanner scan = new Scanner(System.in)) {
            double As;//means adjacent side
            double Os;//means opposite side
            double Hs;//means hypotenuse side
            double Sin;//means Os / Hs
            double Cos;//means As / Hs
            double Tan;//means Os / As
            // begin to loop, this the first layer.
            while (true) {
                //this is second layer of loop; begin to deal with the first number
                while (true) {// for the adjacent number
                    System.out.println("--------------------------------------------------------------");
                    System.out.println("Please enter the length of the Adjacent side or 'bye' to quit!");
                    if (scan.hasNext("bye")) {
                        System.out.println("Program ended. Goodbye!");
                        return;// exit loop
                    }
                    //begin to check and confirm the first number
                    if (scan.hasNextDouble()) {
                        As = scan.nextDouble();
                        if (As > 0) {
                            break;
                        } else {
                            System.out.println("Invalid input, the length should be bigger than zero.");
                        }
                    } else {
                        System.out.println("Invalid input, please enter a valid number.");
                        scan.next();
                    }
                }

                //this is second layer of loop; begin to deal with the second number
                while (true) {// for the opposite number
                    System.out.println("--------------------------------------------------------------");
                    System.out.println("Please enter the length of the Opposite side or 'bye' to quit!");
                    if (scan.hasNext("bye")) {
                        System.out.println("Program ended. Goodbye!");
                        return;//exit loop
                    }
                    //begin to check and confirm the first number
                    if (scan.hasNextDouble()) {
                        Os = scan.nextDouble();
                        if (Os > 0) {
                            break;
                        } else {
                            System.out.println("Invalid input, the length should be bigger than zero.");
                        }
                    } else {
                        System.out.println("Invalid input, please enter a valid number.");
                        scan.next();
                    }
                }
                // begin to calculate sample numbers
                System.out.println("---------------------------Samples----------------------------");
                double sampleAs, sampleOs, sampleHs, sampleSin, sampleCos, sampleTan;
                double[][] samples = {{3, 4}, {0.6, 0.8}, {1, 1}, {2, 3}};
                for (double[] sample : samples) {
                    sampleAs = sample[0];
                    sampleOs = sample[1];
                    sampleHs = Math.sqrt(Math.pow(sampleAs, 2) + Math.pow(sampleOs, 2));
                    sampleSin = sampleOs / sampleHs;
                    sampleCos = sampleAs / sampleHs;
                    sampleTan = sampleOs / sampleAs;
                    //begin to display sample numbers
                    System.out.printf("Adjacent = %.4f, Opposite = %.4f, Hypotenuse = %.4f%n", sampleAs, sampleOs,sampleHs);
                    System.out.printf("sin = %.4f, cos = %.4f, tan = %.4f%n", sampleSin, sampleCos, sampleTan);
                }
                // begin to calculate user input
                Hs = Math.sqrt(Math.pow(As, 2) + Math.pow(Os, 2));
                Sin = Os / Hs;
                Cos = As / Hs;
                Tan = Os / As;
                //begin to display the results of user input
                System.out.println("---------------------------User Input-------------------------");
                System.out.printf("Adjacent = %.4f, Opposite = %.4f, Hypotenuse = %.4f%n", As, Os, Hs);
                System.out.printf("Sin = %.4f, Cos = %.4f, Tan = %.4f%n", Sin, Cos, Tan);
            }
        }
    }
}
Scroll to Top