Margaret Pierse преди 6 години
родител
ревизия
b807549eab
променени са 3 файла, в които са добавени 222 реда и са изтрити 171 реда
  1. 174
    170
      Calculator.java
  2. 12
    1
      MathMethods.java
  3. 36
    0
      package.bluej

+ 174
- 170
Calculator.java Целия файл

@@ -1,8 +1,8 @@
1
+
1 2
 import java.util.*;
2 3
 
3 4
 public class Calculator{
4 5
 
5
-
6 6
     /**
7 7
      * Write a description of class Calculator here.
8 8
      *
@@ -19,12 +19,15 @@ public class Calculator{
19 19
         double result = 0;          //The user's answer
20 20
         boolean off = false;        //The user typed quit, becomes true
21 21
 
22
+        double memory;
23
+
22 24
         boolean error = false;
23 25
         MathMethods calc = new MathMethods();
24 26
 
25 27
         while (off == false){
26 28
             Scanner input1 = new Scanner(System.in);
27 29
             System.out.println("What do you want to do?");
30
+
28 31
             toDo = input1.next();
29 32
 
30 33
             if (toDo.equalsIgnoreCase("quit")){
@@ -34,7 +37,7 @@ public class Calculator{
34 37
             } else {
35 38
                 switch (toDo) {
36 39
                     //System.out.println("toDO is: " + toDo);
37
-                    
40
+
38 41
                     case "add":
39 42
                     case "subtract":
40 43
                     case "multiply":
@@ -52,181 +55,182 @@ public class Calculator{
52 55
                     case "tangent" :
53 56
                     case "inverse tangent" :
54 57
                     case "inverse" :
55
-                    
58
+
56 59
                     System.out.println(toDo + " -- Enter first number:");
57 60
                     num1 = input1.nextDouble();
58 61
                     //System.out.println("num1 is: " + num1);
59
-                }
60
-                switch (toDo){
61
-                    // case "previous":
62
-                    // num1 = lastAns;
63
-                    // System.out.println(toDo + " -- Enter second number:");
64
-                    // num2 = input1.nextDouble();
65
-                    //break;
66
-                    case "add":
67
-                    case "subtract":
68
-                    case "multiply":
69
-                    case "divide":
70
-                    case "exponent":
71
-                    System.out.println(toDo + " -- Enter second number:");
72
-                    num2 = input1.nextDouble();
73
-                    //System.out.println("num2 is: " + num2);
74
-
75
-                }
76
-
77
-                switch (toDo) {        
78
-                    
79
-                    
80
-                    case "add":
81
-                    result = calc.add(num1, num2);
82
-                    lastAns = result;
83
-                    calc.printAns(String.valueOf(result));
84
-                    break;
85
-
86
-                    case "subtract":
87
-                    result = calc.subtract(num1, num2);
88
-                    lastAns = result;
89
-                    calc.printAns(String.valueOf(result));
90
-                    break;
91
-
92
-                    case "multiply":
93
-                    result = calc.multiply(num1, num2);
94
-                    lastAns = result;
95
-                    calc.printAns(String.valueOf(result));
96
-                    break;
97
-
98
-                    case "divide":
99
-                    result = calc.divide(num1, num2);
100
-                    lastAns = result;
101
-                    calc.printAns(String.valueOf(result));
102
-                    break;
103
-
104
-                    case "exponent":
105
-                    result = calc.exponent(num1, num2);
106
-                    lastAns = result;
107
-                    calc.printAns(String.valueOf(result));
108
-                    break;
109
-
110
-                    case "cosine":
111
-                    result = calc.cosine(num1);
112
-                    lastAns = result;
113
-                    calc.printAns(String.valueOf(result));
114
-                    break;
115
-
116
-                    case "inverse cosine":
117
-                    result = calc.invCosine(num1);
118
-                    lastAns = result;
119
-                    calc.printAns(String.valueOf(result));
120
-                    break;
121
-
122
-                    case "square root":
123
-                    result = calc.squareRoot(num1);
124
-                    lastAns = result;
125
-                    calc.printAns(String.valueOf(result));
126
-                    break;
127
-
128
-                    case "invert":
129
-                    result = calc.invert(num1);
130
-                    lastAns = result;
131
-                    calc.printAns(String.valueOf(result));
132
-                    break;
133
-
134
-                    case "sine":
135
-                    result = calc.sin(num1);
136
-                    lastAns = result;
137
-                    calc.printAns(String.valueOf(result));
138
-                    break;
139
-
140
-                    case "inverse sine":
141
-                    result = calc.iSin(num1);
142
-                    lastAns = result;
143
-                    calc.printAns(String.valueOf(result));
144
-                    break;
145
-
146
-                    case "degree to radian":
147
-                    result = calc.degToRad(num1);
148
-                    lastAns = result;
149
-                    calc.printAns(String.valueOf(result));
150
-                    break;
151
-
152
-                    case "radian to degree":
153
-                    result = calc.radToDeg(num1);
154
-                    lastAns = result;
155
-                    calc.printAns(String.valueOf(result));
156
-                    break;
157
-
158
-                    case "fortune cookie":
159
-                    calc.printAns(calc.fortuneCookie());
160
-                    break;
161
-
162
-                    case "display mode decimal":
163
-                    calc.printAns(String.valueOf(lastAns));
164
-                    break;
165
-
166
-                    case "display mode hexadecimal":
167
-                    calc.printAns(calc.switchDisplayMode("hexadecimal"));
168
-
169
-                    case "display mode octal":
170
-                    calc.printAns(calc.switchDisplayMode("octal"));
171
-                    break;
172
-
173
-                    case "display mode binary":
174
-                    calc.printAns(calc.switchDisplayMode("binary"));
175
-                    break;
176
-
177
-                    case "display mode" :
178
-                    calc.printAns(calc.switchDisplayMode());
179
-                    break;
180
-
181
-                    case "random number":
182
-                    result = calc.randomNumber();
183
-                    lastAns = result;
184
-                    calc.printAns(String.valueOf(result));
185
-                    break;      
186
-
187
-                    case "square":
188
-                    result = calc.square(num1);
189
-                    lastAns = result;
190
-                    calc.printAns(String.valueOf(result));
191
-                    break;
192
-
193
-                    case "inverse":
194
-                    result = calc.inverse(num1);
195
-                    lastAns = result;
196
-                    calc.printAns(String.valueOf(result));
197
-                    break;
198
-
199
-                    case "tangent":
200
-                    result = calc.tangent(num1);
201
-                    lastAns = result;
202
-                    calc.printAns(String.valueOf(result));
203
-                    break;
204
-
205
-                    case "inverse tangent":
206
-                    result = calc.iTan(num1);
207
-                    lastAns = result;
208
-                    calc.printAns(String.valueOf(result));
209
-                    break; 
210
-
211
-                    case "MR" :
212
-                    result = Memory.memRead();
213
-                    Memory.printAns(String.valueOf(result));
214
-                    break;
215
-
216
-                    case "M+" :
217
-                    result = Memory.memUpdate(lastAns);
218
-                    Memory.printAns(String.valueOf(result));
219
-                    break;
220
-
221
-                    case "MC" :
222
-                    result = Memory.memClear();
223
-                    Memory.printAns(String.valueOf(result));
224
-                    break;
225 62
 
63
+                    switch (toDo){
64
+                        case "add":
65
+                        case "subtract":
66
+                        case "multiply":
67
+                        case "divide":
68
+                        case "exponent":
69
+                        System.out.println(toDo + " -- Enter second number:");
70
+                        num2 = input1.nextDouble();
71
+                        //System.out.println("num2 is: " + num2);
72
+
73
+                    }
74
+
75
+                    switch (toDo) {        
76
+
77
+                        case "add":
78
+                        result = calc.add(num1, num2);
79
+                        lastAns = result;
80
+                        calc.printAns(String.valueOf(result));
81
+                        break;
82
+
83
+                        case "subtract":
84
+                        result = calc.subtract(num1, num2);
85
+                        lastAns = result;
86
+                        calc.printAns(String.valueOf(result));
87
+                        break;
88
+
89
+                        case "multiply":
90
+                        result = calc.multiply(num1, num2);
91
+                        lastAns = result;
92
+                        calc.printAns(String.valueOf(result));
93
+                        break;
94
+
95
+                        case "divide":
96
+                        result = calc.divide(num1, num2);
97
+                        lastAns = result;
98
+                        calc.printAns(String.valueOf(result));
99
+                        break;
100
+
101
+                        case "exponent":
102
+                        result = calc.exponent(num1, num2);
103
+                        lastAns = result;
104
+                        calc.printAns(String.valueOf(result));
105
+                        break;
106
+
107
+                        case "cosine":
108
+                        result = calc.cosine(num1);
109
+                        lastAns = result;
110
+                        calc.printAns(String.valueOf(result));
111
+                        break;
112
+
113
+                        case "inverse cosine":
114
+                        result = calc.invCosine(num1);
115
+                        lastAns = result;
116
+                        calc.printAns(String.valueOf(result));
117
+                        break;
118
+
119
+                        case "square root":
120
+                        result = calc.squareRoot(num1);
121
+                        lastAns = result;
122
+                        calc.printAns(String.valueOf(result));
123
+                        break;
124
+
125
+                        case "invert":
126
+                        result = calc.invert(num1);
127
+                        lastAns = result;
128
+                        calc.printAns(String.valueOf(result));
129
+                        break;
130
+
131
+                        case "sine":
132
+                        result = calc.sin(num1);
133
+                        lastAns = result;
134
+                        calc.printAns(String.valueOf(result));
135
+                        break;
136
+
137
+                        case "inverse sine":
138
+                        result = calc.iSin(num1);
139
+                        lastAns = result;
140
+                        calc.printAns(String.valueOf(result));
141
+                        break;
142
+
143
+                        case "degree to radian":
144
+                        result = calc.degToRad(num1);
145
+                        lastAns = result;
146
+                        calc.printAns(String.valueOf(result));
147
+                        break;
148
+
149
+                        case "radian to degree":
150
+                        result = calc.radToDeg(num1);
151
+                        lastAns = result;
152
+                        calc.printAns(String.valueOf(result));
153
+                        break;
154
+
155
+                        case "fortune cookie":
156
+                        calc.printAns(calc.fortuneCookie());
157
+                        break;
158
+
159
+                        case "display mode decimal":
160
+                        calc.printAns(String.valueOf(lastAns));
161
+                        break;
162
+
163
+                        case "display mode hexadecimal":
164
+                        calc.printAns(calc.switchDisplayMode("hexadecimal"));
165
+
166
+                        case "display mode octal":
167
+                        calc.printAns(calc.switchDisplayMode("octal"));
168
+                        break;
169
+
170
+                        case "display mode binary":
171
+                        calc.printAns(calc.switchDisplayMode("binary"));
172
+                        break;
173
+
174
+                        case "display mode" :
175
+                        calc.printAns(calc.switchDisplayMode());
176
+                        break;
177
+
178
+                        case "random number":
179
+                        result = calc.randomNumber();
180
+                        lastAns = result;
181
+                        calc.printAns(String.valueOf(result));
182
+                        break;      
183
+
184
+                        case "square":
185
+                        result = calc.square(num1);
186
+                        lastAns = result;
187
+                        calc.printAns(String.valueOf(result));
188
+                        break;
189
+
190
+                        case "inverse":
191
+                        result = calc.inverse(num1);
192
+                        lastAns = result;
193
+                        calc.printAns(String.valueOf(result));
194
+                        break;
195
+
196
+                        case "tangent":
197
+                        result = calc.tangent(num1);
198
+                        lastAns = result;
199
+                        calc.printAns(String.valueOf(result));
200
+                        break;
201
+
202
+                        case "inverse tangent":
203
+                        result = calc.iTan(num1);
204
+                        lastAns = result;
205
+                        calc.printAns(String.valueOf(result));
206
+                        break; 
207
+
208
+                        case "MR" :
209
+                        result = Memory.memRead();
210
+                        Memory.printAns(String.valueOf(result));
211
+                        break;
212
+
213
+                        case "M+" :
214
+                        result = Memory.memUpdate(lastAns);
215
+                        Memory.printAns(String.valueOf(result));
216
+                        break;
217
+
218
+                        case "MC" :
219
+                        result = Memory.memClear();
220
+                        Memory.printAns(String.valueOf(result));
221
+                        break;
222
+                        
223
+                        case "roundTo":
224
+                        memory = result;
225
+                        calc.roundTo(memory, (int)num1);
226
+                        break;
227
+
228
+                    }
226 229
                 }
227 230
             }
228 231
         }
229
-    }
230 232
 
233
+    }
231 234
 }
232 235
 
236
+    

+ 12
- 1
MathMethods.java Целия файл

@@ -1,5 +1,7 @@
1 1
 import java.util.*;
2
+
2 3
 import java.lang.*;
4
+
3 5
 /**
4 6
  * Write a description of class Calculator here.
5 7
  *
@@ -9,8 +11,10 @@ import java.lang.*;
9 11
 public class MathMethods
10 12
 
11 13
 {
14
+
12 15
     private static int displayModeCounter=4;
13 16
 
17
+
14 18
     public static Double add(double n1, double n2){
15 19
         return (n1 + n2);
16 20
     }
@@ -69,6 +73,7 @@ public class MathMethods
69 73
 
70 74
     }
71 75
 
76
+
72 77
     public static String fortuneCookie() {
73 78
         Random rand = new Random();
74 79
          
@@ -137,7 +142,7 @@ public class MathMethods
137 142
     }
138 143
 
139 144
     public static void printAns(String printAnswer){
140
-        System.out.println( String.valueOf(printAnswer));
145
+        System.out.println( "ANSWER: " + printAnswer + " \n \n");
141 146
     }
142 147
 
143 148
     public static double inverse(double n1) {
@@ -157,5 +162,11 @@ public class MathMethods
157 162
         return (display.lastAns);
158 163
     }
159 164
 
165
+    public static void roundTo(Double memory, int numPlaces){
166
+
167
+        System.out.printf("ANSWER: %." + (numPlaces)+ "f\n", memory);
168
+
169
+    }
160 170
 }
161 171
 
172
+

+ 36
- 0
package.bluej Целия файл

@@ -1,5 +1,6 @@
1 1
 #BlueJ package file
2 2
 <<<<<<< HEAD
3
+<<<<<<< HEAD
3 4
 dependency1.from=Calculator
4 5
 dependency1.to=MathMethods
5 6
 dependency1.type=UsesDependency
@@ -51,6 +52,24 @@ package.editor.width=493
51 52
 package.editor.x=-188
52 53
 package.editor.y=-610
53 54
 package.frame.height=625
55
+=======
56
+dependency1.from=Calculator
57
+dependency1.to=MathMethods
58
+dependency1.type=UsesDependency
59
+editor.fx.0.height=722
60
+editor.fx.0.width=800
61
+editor.fx.0.x=0
62
+editor.fx.0.y=132
63
+objectbench.height=205
64
+objectbench.width=595
65
+package.divider.horizontal=0.6
66
+package.divider.vertical=0.6849925705794948
67
+package.editor.height=454
68
+package.editor.width=493
69
+package.editor.x=35
70
+package.editor.y=23
71
+package.frame.height=731
72
+>>>>>>> a0c06052a8de50df57cd0a41d73ffc3f9ce302c2
54 73
 package.frame.width=619
55 74
 package.numDependencies=4
56 75
 package.numTargets=5
@@ -64,10 +83,15 @@ readme.width=47
64 83
 readme.x=10
65 84
 readme.y=10
66 85
 target1.height=50
86
+<<<<<<< HEAD
67 87
 target1.name=MemoryTest
88
+=======
89
+target1.name=Calculator
90
+>>>>>>> a0c06052a8de50df57cd0a41d73ffc3f9ce302c2
68 91
 target1.showInterface=false
69 92
 <<<<<<< HEAD
70 93
 target1.type=ClassTarget
94
+<<<<<<< HEAD
71 95
 target1.width=140
72 96
 target1.x=80
73 97
 target1.y=200
@@ -122,3 +146,15 @@ target5.width=110
122 146
 target5.x=60
123 147
 target5.y=280
124 148
 >>>>>>> 2a9a289e4239250cd6e2f776f8cc64f1130c5a52
149
+=======
150
+target1.width=120
151
+target1.x=70
152
+target1.y=70
153
+target2.height=50
154
+target2.name=MathMethods
155
+target2.showInterface=false
156
+target2.type=ClassTarget
157
+target2.width=110
158
+target2.x=80
159
+target2.y=200
160
+>>>>>>> a0c06052a8de50df57cd0a41d73ffc3f9ce302c2