|
| 1 | +// Simple java program to find the sides of triangles. |
| 2 | +import java.util.Scanner; |
| 3 | + |
| 4 | +// Class or Blu print of the program. |
| 5 | +public class SOH_CAH_TOA { |
| 6 | + |
| 7 | + Scanner scanner = new Scanner(System.in); // Scanner to help accept user input. |
| 8 | + |
| 9 | + public void menu(){ // A function to display menu |
| 10 | + System.out.println("---Menu ---"); |
| 11 | + System.out.println("1. Find Sides"); |
| 12 | + System.out.println("2. Exit"); |
| 13 | + System.out.println("------------------------------------------------------------------------"); |
| 14 | + |
| 15 | + get_menu_option(); // Calling the get_menu_option method in the menu method to get the menu option. |
| 16 | + |
| 17 | + } |
| 18 | + public int get_menu_option(){ // A function to return user menu option. |
| 19 | + System.out.println("Enter a menu option (1 or 2): "); |
| 20 | + int option = scanner.nextInt(); |
| 21 | + System.out.println("------------------------------------------------------------------------"); |
| 22 | + |
| 23 | + if (option < 1 || option > 2){ // Checking if user menu option is appropriate. |
| 24 | + System.out.println("Please enter a valid option (1 or 2)."); |
| 25 | + } |
| 26 | + |
| 27 | + else { |
| 28 | + check_menu_option(option); // calling check_menu_option method after validating user option. |
| 29 | + } |
| 30 | + |
| 31 | + return option; // Returns the user option value |
| 32 | + } |
| 33 | + |
| 34 | + public int get_side(){ // A method to return the side to be calculated. |
| 35 | + System.out.println("1. Adjacent 2. Opposite 3. Hypotenuse"); |
| 36 | + System.out.println("------------------------------------------------------------------------"); |
| 37 | + |
| 38 | + System.out.println("Enter the side you want to find: "); |
| 39 | + int side = scanner.nextInt(); |
| 40 | + System.out.println("------------------------------------------------------------------------"); |
| 41 | + |
| 42 | + if (side < 1 || side > 3){ // Validating the sides. |
| 43 | + System.out.println("Enter a valid side(1-3): "); |
| 44 | + System.out.println("------------------------------------------------------------------------"); |
| 45 | + } |
| 46 | + else{ |
| 47 | + calculate_side(side); // calling the calculate_side method to calculate for the side. |
| 48 | + } |
| 49 | + |
| 50 | + return side; // returns the side. |
| 51 | + } |
| 52 | + |
| 53 | + public void calculate_side(int side){ // A method to perform side calculations. |
| 54 | + if (side == 1){ |
| 55 | + System.out.println("Finding the Adjacent Side"); |
| 56 | + System.out.println("------------------------------------------------------------------------"); |
| 57 | + |
| 58 | + System.out.println("Enter the opposite side: "); |
| 59 | + double opp = scanner.nextInt(); |
| 60 | + System.out.println("Enter the Hypotenuse: "); |
| 61 | + double hyp = scanner.nextInt(); |
| 62 | + System.out.println("------------------------------------------------------------------------"); |
| 63 | + |
| 64 | + double adj = Math.sqrt((hyp*hyp)+(opp*opp)); |
| 65 | + System.out.println("Adjacent = " + Math.round(adj)); |
| 66 | + System.out.println("------------------------------------------------------------------------"); |
| 67 | + } |
| 68 | + else if (side == 2){ |
| 69 | + |
| 70 | + System.out.println("Finding the Opposite Side"); |
| 71 | + System.out.println("------------------------------------------------------------------------"); |
| 72 | + System.out.println("Enter the Adjacent side: "); |
| 73 | + double adj = scanner.nextInt(); |
| 74 | + System.out.println("Enter the Hypotenuse: "); |
| 75 | + double hyp = scanner.nextInt(); |
| 76 | + System.out.println("------------------------------------------------------------------------"); |
| 77 | + |
| 78 | + double opp = Math.sqrt((hyp*hyp)+(adj*adj)); |
| 79 | + System.out.println("Opposite = " + Math.round(opp)); |
| 80 | + System.out.println("------------------------------------------------------------------------"); |
| 81 | + } |
| 82 | + |
| 83 | + else{ |
| 84 | + System.out.println("Finding the Hypotenuse Side"); |
| 85 | + System.out.println("------------------------------------------------------------------------"); |
| 86 | + System.out.println("Enter the opposite side: "); |
| 87 | + double opp = scanner.nextInt(); |
| 88 | + System.out.println("Enter the adjacent: "); |
| 89 | + double adj = scanner.nextInt(); |
| 90 | + System.out.println("------------------------------------------------------------------------"); |
| 91 | + double hyp = Math.sqrt((adj*adj)+(opp*opp)); |
| 92 | + System.out.println("Hypotenuse = " + Math.round(hyp)); |
| 93 | + System.out.println("------------------------------------------------------------------------"); |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | + public void check_menu_option(int option){ // A method to validate user menu options. |
| 100 | + |
| 101 | + |
| 102 | + if (option == 2){ |
| 103 | + System.err.println("Exiting...\nThanks for using the program..."); |
| 104 | + System.out.println("------------------------------------------------------------------------"); |
| 105 | + } |
| 106 | + else{ |
| 107 | + get_side(); |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + public void main(String[] args){ // main method |
| 118 | + SOH_CAH_TOA obj = new SOH_CAH_TOA(); // Creating an object for the class |
| 119 | + obj.menu(); // Calling the menu method to run the program. |
| 120 | + } |
| 121 | +} |
0 commit comments