|
@@ -1,33 +1,133 @@
|
|
1
|
+import java.util.*;
|
1
|
2
|
|
2
|
|
-/**
|
3
|
|
- * Write a description of class calculator here.
|
4
|
|
- *
|
5
|
|
- * @author (your name)
|
6
|
|
- * @version (a version number or a date)
|
7
|
|
- */
|
8
|
|
-public class calculator
|
9
|
|
-{
|
10
|
|
- // instance variables - replace the example below with your own
|
11
|
|
- private int x;
|
12
|
|
-
|
13
|
|
- /**
|
14
|
|
- * Constructor for objects of class calculator
|
15
|
|
- */
|
16
|
|
- public calculator()
|
17
|
|
- {
|
18
|
|
- // initialise instance variables
|
19
|
|
- x = 0;
|
20
|
|
- }
|
21
|
3
|
|
22
|
|
- /**
|
23
|
|
- * An example of a method - replace this comment with your own
|
24
|
|
- *
|
25
|
|
- * @param y a sample parameter for a method
|
26
|
|
- * @return the sum of x and y
|
27
|
|
- */
|
28
|
|
- public int add(int y)
|
29
|
|
- {
|
30
|
|
- // put your code here
|
31
|
|
- return x + y;
|
|
4
|
+public class Calculator {
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+ public static void main(String[] args) {
|
|
8
|
+
|
|
9
|
+ double displayNum = 0;
|
|
10
|
+ boolean calcOn = true;
|
|
11
|
+ double num;
|
|
12
|
+
|
|
13
|
+ Scanner sc = new Scanner(System.in);
|
|
14
|
+
|
|
15
|
+ while (calcOn) {
|
|
16
|
+
|
|
17
|
+ System.out.println(displayNum);
|
|
18
|
+
|
|
19
|
+ System.out.println("Enter an option: ");
|
|
20
|
+ System.out.println("0 - ENTER NUMBER");
|
|
21
|
+ System.out.println("1 - ADD ");
|
|
22
|
+ System.out.println("2 - SUBTRACT");
|
|
23
|
+ System.out.println("3 - MULTIPLY");
|
|
24
|
+ System.out.println("4 - DIVIDE");
|
|
25
|
+ System.out.println("5 - SQUARE");
|
|
26
|
+ System.out.println("6 - SQUARE ROOT");
|
|
27
|
+ System.out.println("7 - EXPONENT");
|
|
28
|
+ System.out.println("8 - Invert +/-");
|
|
29
|
+ System.out.println("9 - INVERSE");
|
|
30
|
+ System.out.println("10 - SIN");
|
|
31
|
+ System.out.println("11 - COS");
|
|
32
|
+ System.out.println("12 - TAN");
|
|
33
|
+ System.out.println("13 - INVERSE SIN");
|
|
34
|
+ System.out.println("14 - INVERSE COS");
|
|
35
|
+ System.out.println("15 - INVERSE TAN");
|
|
36
|
+ System.out.println("16 - QUIT");
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+ int userInput = sc.nextInt();
|
|
42
|
+ //displayNum = userInput;
|
|
43
|
+ System.out.println(displayNum);
|
|
44
|
+
|
|
45
|
+ switch(userInput) {
|
|
46
|
+ case 0: System.out.println("enter number");
|
|
47
|
+ num = sc.nextDouble();
|
|
48
|
+ displayNum = num;
|
|
49
|
+ break;
|
|
50
|
+ case 1: System.out.println("enter number to ADD to " + displayNum);
|
|
51
|
+ num = sc.nextDouble();
|
|
52
|
+ displayNum = num + displayNum;
|
|
53
|
+ break;
|
|
54
|
+ case 2: System.out.println("enter number to SUBTRACT from " + displayNum);
|
|
55
|
+ num = sc.nextDouble();
|
|
56
|
+ displayNum = displayNum - num;
|
|
57
|
+ break;
|
|
58
|
+ case 3: System.out.println("enter number to MULTIPLY to" + displayNum);
|
|
59
|
+ num = sc.nextDouble();
|
|
60
|
+ displayNum = num * displayNum;
|
|
61
|
+ break;
|
|
62
|
+ case 4: System.out.println("enter number to DIVIDE from " + displayNum);
|
|
63
|
+ num = sc.nextDouble();
|
|
64
|
+ displayNum = displayNum / num;
|
|
65
|
+ break;
|
|
66
|
+ case 5: System.out.println("square result: " + displayNum * displayNum);
|
|
67
|
+ num = sc.nextDouble();
|
|
68
|
+ // displayNum = displayNum * displayNum;
|
|
69
|
+ break;
|
|
70
|
+ case 6: System.out.println("square root result: " + Math.sqrt(displayNum));
|
|
71
|
+ num = sc.nextDouble();
|
|
72
|
+ break;
|
|
73
|
+ case 7: System.out.println("enter an exponent to the number: " + displayNum);
|
|
74
|
+ num = sc.nextDouble();
|
|
75
|
+ displayNum = Math.pow(displayNum, num);
|
|
76
|
+ break;
|
|
77
|
+ case 8: System.out.println("result: " + displayNum * -1);
|
|
78
|
+ num = sc.nextDouble();
|
|
79
|
+ break;
|
|
80
|
+ case 9: System.out.println("result: " + Math.sqrt(displayNum));
|
|
81
|
+ num = sc.nextDouble();
|
|
82
|
+ // displayNum = displayNum * displayNum;
|
|
83
|
+ break;
|
|
84
|
+ case 10: System.out.println("result: " + Math.sin(displayNum));
|
|
85
|
+ num = sc.nextDouble();
|
|
86
|
+ // displayNum = displayNum * displayNum;
|
|
87
|
+ break;
|
|
88
|
+ case 11: System.out.println("result: " + Math.cos(displayNum));
|
|
89
|
+ num = sc.nextDouble();
|
|
90
|
+ // displayNum = displayNum * displayNum;
|
|
91
|
+ break;
|
|
92
|
+ case 12: System.out.println("result: " + Math.tan(displayNum));
|
|
93
|
+ num = sc.nextDouble();
|
|
94
|
+ // displayNum = displayNum * displayNum;
|
|
95
|
+ break;
|
|
96
|
+ case 13: System.out.println("result: " + Math.asin(displayNum));
|
|
97
|
+ num = sc.nextDouble();
|
|
98
|
+ // displayNum = displayNum * displayNum;
|
|
99
|
+ break;
|
|
100
|
+ case 14: System.out.println("result: " + Math.acos(displayNum));
|
|
101
|
+ num = sc.nextDouble();
|
|
102
|
+ // displayNum = displayNum * displayNum;
|
|
103
|
+ break;
|
|
104
|
+ case 15: System.out.println("result: " + Math.atan(displayNum));
|
|
105
|
+ num = sc.nextDouble();
|
|
106
|
+ // displayNum = displayNum * displayNum;
|
|
107
|
+ break;
|
|
108
|
+ case 16: System.out.println("Quitting!");
|
|
109
|
+ calcOn = false;
|
|
110
|
+ break;
|
|
111
|
+ default: break;
|
|
112
|
+
|
|
113
|
+ }
|
|
114
|
+
|
32
|
115
|
}
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+ }
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
|
33
|
133
|
}
|