|
@@ -1,5 +1,4 @@
|
1
|
1
|
|
2
|
|
-import java.util.Scanner;
|
3
|
2
|
|
4
|
3
|
/**
|
5
|
4
|
* Trig class to perform trig function for our Graphing Calculator
|
|
@@ -11,76 +10,109 @@ public class Trig
|
11
|
10
|
{
|
12
|
11
|
int input = 0;
|
13
|
12
|
double answer;
|
|
13
|
+ String operation = "";
|
14
|
14
|
|
15
|
15
|
public Trig()
|
16
|
16
|
{
|
|
17
|
+ //a check to make sure user input is valid
|
17
|
18
|
boolean validInput = false;
|
18
|
19
|
|
19
|
20
|
while(!validInput) {
|
20
|
21
|
//Get input of type of function
|
21
|
|
- Scanner in1 = new Scanner(System.in);
|
22
|
|
- System.out.println("Which trignometric function would you like to run? \n sin, cos, tan, arcsin, arccos, arctan");
|
23
|
|
- String operation = in1.nextLine();
|
|
22
|
+ operation = Console.getStringInput("Which trignometric function would you like to run? \n sin, cos, tan, arcsin, arccos, arctan");
|
24
|
23
|
operation = operation.toLowerCase();
|
25
|
24
|
|
26
|
25
|
//Get input of number
|
27
|
|
- Scanner in2 = new Scanner(System.in);
|
28
|
|
- System.out.println("What number would you like to find the " + operation + " of?");
|
29
|
|
- int input = in2.nextInt();
|
|
26
|
+ input = Console.getIntegerInput("What number would you like to find the " + operation + " of?");
|
30
|
27
|
|
31
|
28
|
//Do the math depending on the input.
|
|
29
|
+ //SIN
|
32
|
30
|
if (operation.equals("sin")) {
|
33
|
|
- answer = Math.sin(input);
|
34
|
|
- System.out.println(answer);
|
|
31
|
+ calcSin(input);
|
35
|
32
|
validInput = true;
|
36
|
33
|
|
|
34
|
+ //COS
|
37
|
35
|
} else if (operation.equals("cos")) {
|
38
|
|
- answer = Math.cos(input);
|
39
|
|
- System.out.println(answer);
|
|
36
|
+ calcCos(input);
|
40
|
37
|
validInput = true;
|
41
|
38
|
|
|
39
|
+ //TAN
|
42
|
40
|
} else if (operation.equals("tan")) {
|
43
|
|
- answer = Math.tan(input);
|
44
|
|
- System.out.println(answer);
|
|
41
|
+ calcTan(input);
|
45
|
42
|
validInput = true;
|
46
|
43
|
|
|
44
|
+ //ARCSIN
|
47
|
45
|
} else if (operation.equals("arcsin")) {
|
48
|
|
- answer = Math.asin(input);
|
49
|
|
- System.out.println(answer);
|
|
46
|
+ calcArcsin(input);
|
50
|
47
|
validInput = true;
|
51
|
48
|
|
|
49
|
+ //ARCCOS
|
52
|
50
|
} else if (operation.equals("arccos")) {
|
53
|
|
- answer = Math.acos(input);
|
54
|
|
- System.out.println(answer);
|
|
51
|
+ calcArccos(input);
|
55
|
52
|
validInput = true;
|
56
|
53
|
|
|
54
|
+ //ARCTAN
|
57
|
55
|
} else if (operation.equals("arctan")) {
|
58
|
|
- answer = Math.atan(input);
|
59
|
|
- System.out.println(answer);
|
|
56
|
+ calcArctan(input);
|
60
|
57
|
validInput = true;
|
61
|
|
-
|
|
58
|
+
|
|
59
|
+ //Error message if input does not match options, loops back to the top so they can try again.
|
62
|
60
|
} else {
|
63
|
|
- System.out.println("Error, try again \n");
|
|
61
|
+ Console.println("Error, try again \n");
|
64
|
62
|
|
65
|
63
|
}
|
66
|
64
|
}
|
67
|
65
|
//Ask if they would like to convert.
|
68
|
|
- System.out.println("Would you like to convert your answer from radian to degrees? Y or N");
|
69
|
|
- Scanner in3 = new Scanner(System.in);
|
70
|
|
- String response = in3.nextLine();
|
|
66
|
+ String response = Console.getStringInput("Would you like to convert your answer from radian to degrees? Y or N");
|
71
|
67
|
response = response.toLowerCase();
|
72
|
68
|
if (response.equals("y")) {
|
73
|
69
|
toDegrees(answer);
|
74
|
70
|
} else {
|
75
|
|
- System.out.println("Have a nice day!");
|
|
71
|
+ Console.println("Have a nice day!");
|
76
|
72
|
//Insert Return to Main Menu
|
77
|
73
|
}
|
78
|
74
|
}
|
79
|
|
-
|
|
75
|
+ //Sin method
|
|
76
|
+ public void calcSin(int x) {
|
|
77
|
+ answer = Math.sin(x);
|
|
78
|
+ Console.println(Double.toString(answer));
|
|
79
|
+ }
|
|
80
|
+
|
|
81
|
+ //Cos method
|
|
82
|
+ public void calcCos(int x) {
|
|
83
|
+ answer = Math.cos(x);
|
|
84
|
+ Console.println(Double.toString(answer));
|
|
85
|
+ }
|
|
86
|
+
|
|
87
|
+ //Tan method
|
|
88
|
+ public void calcTan(int x) {
|
|
89
|
+ answer = Math.tan(x);
|
|
90
|
+ Console.println(Double.toString(answer));
|
|
91
|
+ }
|
|
92
|
+
|
|
93
|
+ //Arcsin method
|
|
94
|
+ public void calcArcsin(int x) {
|
|
95
|
+ answer = Math.asin(x);
|
|
96
|
+ Console.println(Double.toString(answer));
|
|
97
|
+ }
|
|
98
|
+
|
|
99
|
+ //Arccos method
|
|
100
|
+ public void calcArccos(int x) {
|
|
101
|
+ answer = Math.acos(x);
|
|
102
|
+ Console.println(Double.toString(answer));
|
|
103
|
+ }
|
|
104
|
+
|
|
105
|
+ //Arctan method
|
|
106
|
+ public void calcArctan(int x) {
|
|
107
|
+ answer = Math.atan(x);
|
|
108
|
+ Console.println(Double.toString(answer));
|
|
109
|
+ }
|
|
110
|
+
|
|
111
|
+ //Converting from Radians to Degrees
|
80
|
112
|
public void toDegrees(double answer)
|
81
|
113
|
{
|
82
|
114
|
double conversion = Math.toDegrees(answer);
|
83
|
|
- System.out.println(conversion);
|
|
115
|
+ Console.println(Double.toString(conversion));
|
84
|
116
|
//Insert Return to Main Menu
|
85
|
117
|
}
|
86
|
118
|
}
|