|
@@ -1,17 +1,188 @@
|
1
|
|
-
|
|
1
|
+import java.util.*;
|
2
|
2
|
|
3
|
3
|
/**
|
4
|
4
|
* Created by leon on 2/9/18.
|
5
|
5
|
*/
|
6
|
6
|
public class MainApplication {
|
7
|
7
|
public static void main(String[] args) {
|
|
8
|
+
|
|
9
|
+ double value = 0;//answer
|
|
10
|
+ double memoryValue = 0;
|
|
11
|
+
|
8
|
12
|
Console.println("Welcome to my calculator!");
|
|
13
|
+ /*
|
9
|
14
|
String s = Console.getStringInput("Enter a string");
|
10
|
15
|
Integer i = Console.getIntegerInput("Enter an integer");
|
11
|
16
|
Double d = Console.getDoubleInput("Enter a double.");
|
12
|
17
|
|
13
|
18
|
Console.println("The user input %s as a string", s);
|
14
|
19
|
Console.println("The user input %s as a integer", i);
|
15
|
|
- Console.println("The user input %s as a d", d);
|
|
20
|
+ Console.println("The user input %s as a double", d);
|
|
21
|
+ `*/
|
|
22
|
+ Scanner input = new Scanner(System.in);
|
|
23
|
+ // Calculator calc = new ....
|
|
24
|
+ while (true) {
|
|
25
|
+ System.out.print("Enter a command: ");
|
|
26
|
+ String command = input.next();
|
|
27
|
+ if (command.equalsIgnoreCase("exit")) {
|
|
28
|
+ Console.println("Thanks for calculating with us!");
|
|
29
|
+ break;
|
|
30
|
+ }
|
|
31
|
+ if (command.equalsIgnoreCase("add")) {
|
|
32
|
+ // read first number
|
|
33
|
+ // read second number
|
|
34
|
+ // call the add method on calc and display
|
|
35
|
+ double x = Console.getDoubleInput("Input your first number");
|
|
36
|
+ double y = Console.getDoubleInput("Input your second number");
|
|
37
|
+ double answer = BasicMath.add(x,y);
|
|
38
|
+ value = answer;
|
|
39
|
+ Console.println(String.valueOf(answer));
|
|
40
|
+ }
|
|
41
|
+ if(command.equalsIgnoreCase("Subtract")){
|
|
42
|
+ double x = Console.getDoubleInput("Input your first number");
|
|
43
|
+ double y = Console.getDoubleInput("Input your second number");
|
|
44
|
+ double answer = BasicMath.Subtract(x,y);
|
|
45
|
+ value = answer;
|
|
46
|
+ Console.println(String.valueOf(answer));
|
|
47
|
+ }
|
|
48
|
+ if (command.equalsIgnoreCase("multiply")) {
|
|
49
|
+ double x = Console.getDoubleInput("Input your first number");
|
|
50
|
+ double y = Console.getDoubleInput("Input your second number");
|
|
51
|
+ double answer = BasicMath.multiply(x,y);
|
|
52
|
+ value = answer;
|
|
53
|
+ Console.println(String.valueOf(answer));
|
|
54
|
+ }
|
|
55
|
+ if (command.equalsIgnoreCase("divide")) {
|
|
56
|
+ // call the square method on calc and display
|
|
57
|
+ double x = Console.getDoubleInput("Input your first number");
|
|
58
|
+ double y = Console.getDoubleInput("Input your second number");
|
|
59
|
+ double answer = BasicMath.divide(x,y);
|
|
60
|
+ value = answer;
|
|
61
|
+ Console.println(String.valueOf(answer));
|
|
62
|
+ }
|
|
63
|
+ if (command.equalsIgnoreCase("power")) {
|
|
64
|
+ // call the square method on calc and display
|
|
65
|
+ double x = Console.getDoubleInput("Input your first number");
|
|
66
|
+ double y = Console.getDoubleInput("Input your exponent");
|
|
67
|
+ double answer = BasicMath.power(x,y);
|
|
68
|
+ value = answer;
|
|
69
|
+ Console.println(String.valueOf(answer));
|
|
70
|
+ }
|
|
71
|
+ if (command.equalsIgnoreCase("inverse")) {
|
|
72
|
+ // call the square method on calc and display
|
|
73
|
+ double x = Console.getDoubleInput("Input your number");
|
|
74
|
+ double answer = BasicMath.inverse(x);
|
|
75
|
+ value = answer;
|
|
76
|
+ Console.println(String.valueOf(answer));
|
|
77
|
+ }
|
|
78
|
+ if (command.equalsIgnoreCase("square")) {
|
|
79
|
+ // read first number
|
|
80
|
+ // call the method on calc and display
|
|
81
|
+ double x = Console.getDoubleInput("Input your number");
|
|
82
|
+ double answer = BasicMath.squareRoot(x);
|
|
83
|
+ value = answer;
|
|
84
|
+ Console.println(String.valueOf(answer));
|
|
85
|
+ }
|
|
86
|
+ if (command.equalsIgnoreCase("hex")) {
|
|
87
|
+ int no = (int)value;
|
|
88
|
+ String hex = Integer.toHexString(no);
|
|
89
|
+ Console.println(hex);
|
|
90
|
+ }
|
|
91
|
+ if (command.equalsIgnoreCase("binary")) {
|
|
92
|
+ int no = (int)value;
|
|
93
|
+ String binary = Integer.toBinaryString(no);
|
|
94
|
+ Console.println(binary);
|
|
95
|
+ }
|
|
96
|
+ if(command.equalsIgnoreCase("octal")){
|
|
97
|
+ int no = (int)value;
|
|
98
|
+ String octal = Integer.toOctalString(no);
|
|
99
|
+ Console.println(octal);
|
|
100
|
+ }
|
|
101
|
+
|
|
102
|
+ if (command.equalsIgnoreCase("sin")) {
|
|
103
|
+ double x = Console.getDoubleInput("Input your number");
|
|
104
|
+ double answer = TrigFunctions.sin(x);
|
|
105
|
+ value = answer;
|
|
106
|
+ Console.println(String.valueOf(answer));
|
|
107
|
+ }
|
|
108
|
+
|
|
109
|
+ if (command.equalsIgnoreCase("cos")) {
|
|
110
|
+ double x = Console.getDoubleInput("Input your number");
|
|
111
|
+ double answer = TrigFunctions.cos(x);
|
|
112
|
+ value = answer;
|
|
113
|
+ Console.println(String.valueOf(answer));
|
|
114
|
+ }
|
|
115
|
+ if (command.equalsIgnoreCase("tan")) {
|
|
116
|
+ double x = Console.getDoubleInput("Input your number");
|
|
117
|
+ double answer = TrigFunctions.tan(x);
|
|
118
|
+ value = answer;
|
|
119
|
+ Console.println(String.valueOf(answer));
|
|
120
|
+ }
|
|
121
|
+ if (command.equalsIgnoreCase("invsin")) {
|
|
122
|
+ double x = Console.getDoubleInput("Input your number");
|
|
123
|
+ double answer = TrigFunctions.invsin(x);
|
|
124
|
+ value = answer;
|
|
125
|
+ Console.println(String.valueOf(answer));
|
|
126
|
+ }
|
|
127
|
+ if (command.equalsIgnoreCase("invcos")) {
|
|
128
|
+ double x = Console.getDoubleInput("Input your number");
|
|
129
|
+ double answer = TrigFunctions.invcos(x);
|
|
130
|
+ value = answer;
|
|
131
|
+ Console.println(String.valueOf(answer));
|
|
132
|
+ }
|
|
133
|
+ if (command.equalsIgnoreCase("invtan")) {
|
|
134
|
+ double x = Console.getDoubleInput("Input your number");
|
|
135
|
+ double answer = TrigFunctions.invtan(x);
|
|
136
|
+ value = answer;
|
|
137
|
+ Console.println(String.valueOf(answer));
|
|
138
|
+ }
|
|
139
|
+ if (command.equalsIgnoreCase("deg")) {
|
|
140
|
+ double x = Console.getDoubleInput("Input your number");
|
|
141
|
+ double answer = TrigFunctions.deg(x);
|
|
142
|
+ value = answer;
|
|
143
|
+ Console.println(String.valueOf(answer));
|
|
144
|
+ }
|
|
145
|
+ if (command.equalsIgnoreCase("rad")) {
|
|
146
|
+ double x = Console.getDoubleInput("Input your number");
|
|
147
|
+ double answer = TrigFunctions.rad(x);
|
|
148
|
+ value = answer;
|
|
149
|
+ Console.println(String.valueOf(answer));
|
|
150
|
+ }
|
|
151
|
+ if(command.equalsIgnoreCase("negpos")){
|
|
152
|
+ if(value > 0){
|
|
153
|
+ value = value * (-1);
|
|
154
|
+ Console.println(String.valueOf(value));
|
|
155
|
+ }
|
|
156
|
+ else{
|
|
157
|
+ Console.println("ERROR");
|
|
158
|
+ }
|
|
159
|
+ }
|
|
160
|
+ if(command.equalsIgnoreCase("clear")){
|
|
161
|
+ value = 0;
|
|
162
|
+ Console.println(String.valueOf(value));
|
|
163
|
+ }
|
|
164
|
+ if(command.equalsIgnoreCase("memory")){
|
|
165
|
+ memoryValue = Console.getDoubleInput("Input the number you want to save:");
|
|
166
|
+ Console.println("SAVED!!!");
|
|
167
|
+ Console.println(String.valueOf(0));
|
|
168
|
+ }// and on and on
|
|
169
|
+ if(command.equalsIgnoreCase("recall")){
|
|
170
|
+ Console.println(String.valueOf(memoryValue));
|
|
171
|
+ }
|
|
172
|
+ // Pythagorean theorem
|
|
173
|
+ if(command.equalsIgnoreCase("custom1")){
|
|
174
|
+ double x = Console.getDoubleInput("Input your first number");
|
|
175
|
+ double y = Console.getDoubleInput("Input your exponent");
|
|
176
|
+ double answer = Math.sqrt(Math.pow(x, 2)+ Math.pow(y, 2));
|
|
177
|
+ value = answer;
|
|
178
|
+ Console.println(String.valueOf(value));
|
|
179
|
+ }
|
|
180
|
+ if(command.equalsIgnoreCase("PI")){
|
|
181
|
+ value = Math.PI;
|
|
182
|
+ Console.println("PIEEEEEEEEEEEEEEEEEEEEE");
|
|
183
|
+ Console.println(String.valueOf(value));
|
|
184
|
+ }
|
|
185
|
+ }
|
|
186
|
+
|
16
|
187
|
}
|
17
|
188
|
}
|