-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyMethodDemo.java
More file actions
174 lines (138 loc) · 5.64 KB
/
MyMethodDemo.java
File metadata and controls
174 lines (138 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package Lab6Q;
import java.util.Scanner;
public class MyMethodDemo {//public class cont
public static void main(String[] args) {
char choice, a, b, c, d, e;
Scanner input = new Scanner(System.in);
while (true) {
System.out.println("==================\n" +
"a: Power Function\n" +
"b: Factorial Function\n" +
"c: Sine Function\n" +
"d: Cosine Function\n" +
"e: exit\n" +
"==================\n" +
"Please enter an option from the menu: ");
choice = input.next().charAt(0);
choice = Character.toLowerCase(choice);
if (choice == 'e')
break;
switch (choice) {
case 'a':
double base, power;
System.out.println("Please enter the base and exponent:" );
System.out.print("Base: ");
base = input.nextDouble();
System.out.print("Exponent: ");
power = input.nextDouble();
System.out.println("The result of "+base+" to the power of " + power + " is " +myPow(base, power) );
break;
case 'b':
int n1;
System.out.println("Please enter the number you would like to find the factorial of :" );
n1 = input.nextInt();
System.out.println("The factorial of " + n1 +" is " + myFactorial(n1) );
break;
case 'c':
double angle;
char angleChoice;
System.out.print("Enter r if your angle is in radians. Enter d if your angle is in degrees: ");
angleChoice = input.next().charAt(0);
switch (angleChoice){
case 'r':
System.out.print("Please enter your angle in radians : ");
angle = input.nextDouble();
System.out.println("The sin of the angle entered is " +mySin(angle) );
break;
case 'd':
System.out.print("Please enter your angle in degrees : ");
angle = input.nextDouble();
angle = myDegreeToRadian(angle);
System.out.println("The sin of the angle entered is " +mySin(angle) );
break;
}
break;
case 'd':
double angle2;
System.out.print("Enter r if your angle is in radians. Enter d if your angle is in degrees: ");
char angleChoice2 = input.next().charAt(0);
switch (angleChoice2){
case 'r':
System.out.print("Please enter your angle in radians : ");
angle2 = input.nextDouble();
System.out.println("The cos of the angle entered is " +myCos(angle2) );
break;
case 'd':
System.out.print("Please enter your angle in degrees : ");
angle2 = input.nextDouble();
angle2 = myDegreeToRadian(angle2);
System.out.println("The cos of the angle entered is " +myCos(angle2) );
break;
}
break;
default:
System.out.println("Invalid Entry");
break;
}
}
}
public static double myDegreeToRadian ( double degrees){
double x = degrees;
degrees = x * Math.PI / 180;
return degrees;
}
//COSINE METHOD **************************************************
public static double myCos ( double x){
double numerator, denominator, exponent, n = 0;
double result = 0;
int N = 20;
for (n = 0; n <= N; n++) {
numerator = myPow(-1, n);
denominator = myFactorial((2 * n));
exponent = myPow(x, (2 * n));
result += (numerator * exponent) / denominator;
}
return result;
}
//SIN METHOD **************************************************
public static double mySin ( double x){
double numerator, denominator, exponent, n = 0;
double result = 0;
int N = 20;
for (n = 0; n <= N; n++) {
numerator = myPow(-1, n);
denominator = myFactorial((2 * n + 1));
exponent = myPow(x, (2 * n + 1));
result += (numerator * exponent) / denominator;
}
return result;
}
//FACTORIAL METHOD **************************************************
public static double myFactorial ( double n){
int i;
double result = 1;
for (i = 1; i <= n; i++)
result = result * i;
return result;
}
//POWER METHOD **************************************************
public static double myPow ( double base, double power){
double result = 1;
if (power == 0) {
return 1;
}
if (power < 0) {
while (power < 0) {
result = result / base;
power++;
}
}
if (power > 0) {
while (power > 0) {
result = result * base;
--power;
}
}
return result;
}
}