Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 69eccbe

Browse files
authored
Upload Triangle_Sides_finder program
This program uses the Logic of SOH CAH TOA to find either the hypotenuse or adjacent or the opposite side of a triangle Signed-off-by: Boateng Prince Agyenim <163312213+Mmabiaa@users.noreply.github.com>
1 parent 50ec84d commit 69eccbe

File tree

2 files changed

+173
-0
lines changed

2 files changed

+173
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package Triangle_sides_finder;
2+
import java.util.Scanner;
3+
import Triangle_sides_finder.SOH_CAH_TOA;
4+
5+
6+
public class Main {
7+
SOH_CAH_TOA object = new SOH_CAH_TOA();
8+
Scanner scanner = new Scanner(System.in); // Scanner to help accept user input.
9+
10+
public void menu(){ // A function to display menu
11+
System.out.println("---Menu ---");
12+
System.out.println("1. Find Sides");
13+
System.out.println("2. Exit");
14+
System.out.println("------------------------------------------------------------------------");
15+
16+
get_menu_option(); // Calling the get_menu_option method in the menu method to get the menu option.
17+
18+
}
19+
20+
public int get_menu_option(){ // A function to return user menu option.
21+
System.out.println("Enter a menu option (1 or 2): ");
22+
int option = scanner.nextInt();
23+
System.out.println("------------------------------------------------------------------------");
24+
25+
if (option < 1 || option > 2){ // Checking if user menu option is appropriate.
26+
System.out.println("Please enter a valid option (1 or 2).");
27+
}
28+
29+
else {
30+
check_menu_option(option); // calling check_menu_option method after validating user option.
31+
}
32+
33+
return option; // Returns the user option value
34+
}
35+
36+
public void check_menu_option(int option){ // A method to validate user menu options.
37+
38+
39+
if (option == 2){
40+
System.err.println("Exiting...\nThanks for using the program...");
41+
System.out.println("------------------------------------------------------------------------");
42+
}
43+
else{
44+
object.get_side();
45+
}
46+
47+
48+
}
49+
public void main(String[] args) {
50+
menu();
51+
}
52+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /