Explorar el Código

memory class add

Seth hace 6 años
padre
commit
c3681eda7f
Se han modificado 6 ficheros con 392 adiciones y 233 borrados
  1. 237
    214
      Calculator.java
  2. 1
    1
      MathMethods.java
  3. 7
    5
      MathMethodsTest.java
  4. 36
    0
      Memory.java
  5. 66
    0
      MemoryTest.java
  6. 45
    13
      package.bluej

+ 237
- 214
Calculator.java Ver fichero

@@ -8,7 +8,7 @@ import java.util.*;
8 8
  */
9 9
 
10 10
 class Calculator{
11
-    
11
+
12 12
     public static void main(String args[]){
13 13
 
14 14
         String toDo;                //what the user wants to do
@@ -23,232 +23,255 @@ class Calculator{
23 23
         while (off == false){
24 24
             Scanner input1 = new Scanner(System.in);
25 25
             System.out.println("What do you want to do?");
26
-            toDo = input1.nextLine();
26
+            toDo = input1.next();
27 27
 
28
-            if (toDo.equals("quit")){
28
+            if (toDo.equalsIgnoreCase("quit")){
29 29
                 off = true;
30 30
                 System.out.println("Goodbye!!");
31 31
 
32
-            } else {
33
-
34
-                //System.out.println("toDO is: " + toDo);
35
-
36
-                System.out.println(toDo + " -- Enter first number:");
37
-                num1 = input1.nextDouble();
38
-                //System.out.println("num1 is: " + num1);
39
-
40
-                switch (toDo){
41
-                    case "add":
42
-                    case "subtract":
43
-                    case "multiply":
44
-                    case "divide":
45
-                    case "exponent":
46
-                    System.out.println(toDo + " -- Enter second number:");
47
-                    num2 = input1.nextDouble();
48
-                    //System.out.println("num2 is: " + num2);
49
-
50
-                }
51
-
52
-                switch (toDo) {        
53
-                    case "add":
54
-                    result = calc.add(num1, num2);
55
-                    memory = result;
56
-                    calc.printAns(String.valueOf(result));
57
-                    break;
58
-
59
-                    case "subtract":
60
-                    result = calc.subtract(num1, num2);
61
-                    memory = result;
62
-                    calc.printAns(String.valueOf(result));
63
-                    break;
64
-
65
-                    case "multiply":
66
-                    result = calc.multiply(num1, num2);
67
-                    memory = result;
68
-                    calc.printAns(String.valueOf(result));
69
-                    break;
70
-
71
-                    case "divide":
72
-                    result = calc.divide(num1, num2);
73
-                    memory = result;
74
-                    calc.printAns(String.valueOf(result));
75
-                    break;
76
-
77
-                    case "exponent":
78
-                    result = calc.exponent(num1, num2);
79
-                    memory = result;
80
-                    calc.printAns(String.valueOf(result));
81
-                    break;
82
-
83
-                    case "cosine":
84
-                    result = calc.cosine(num1);
85
-                    memory = result;
86
-                    calc.printAns(String.valueOf(result));
87
-                    break;
88
-
89
-                    case "inverse cosine":
90
-                    result = calc.invCosine(num1);
91
-                    memory = result;
92
-                    calc.printAns(String.valueOf(result));
93
-                    break;
94
-
95
-                    case "square root":
96
-                    result = calc.squareRoot(num1);
97
-                    memory = result;
98
-                    calc.printAns(String.valueOf(result));
32
+            } else if(toDo.equalsIgnoreCase("memory")){
33
+                System.out.println("Read, Change or Clear?");
34
+                String mem = input1.next();
35
+                switch(mem) {
36
+                    case "Read" :
37
+                    result = Memory.memRead();
38
+                    Memory.printAns(String.valueOf(result));
99 39
                     break;
100 40
                     
101
-                    case "invert":
102
-                    result = calc.invert(num1);
103
-                    memory = result;
104
-                    calc.printAns(String.valueOf(result));
41
+                    case "Change" :
42
+                    System.out.println("Enter number:");
43
+                    num1 = input1.nextDouble();
44
+                    result = Memory.memUpdate(num1);
45
+                    Memory.printAns(String.valueOf(result));
105 46
                     break;
106 47
                     
107
-                    case "sine":
108
-                    result = calc.sin(num1);
109
-                    memory = result;
110
-                    calc.printAns(String.valueOf(result));
48
+                    case "Clear" :
49
+                    result = Memory.memClear();
50
+                    Memory.printAns(String.valueOf(result));
111 51
                     break;
112 52
                     
113
-                    case "inverse sine":
114
-                    result = calc.iSin(num1);
115
-                    memory = result;
116
-                    calc.printAns(String.valueOf(result));
117
-                    break;
118
-                    
119
-                    case "degree to radian":
120
-                    result = calc.degToRad(num1);
121
-                    memory = result;
122
-                    calc.printAns(String.valueOf(result));
123
-                    break;
124
-                    
125
-                    case "radian to degree":
126
-                    result = calc.radToDeg(num1);
127
-                    memory = result;
128
-                    calc.printAns(String.valueOf(result));
129
-                    break;
130
-                    
131
-                    case "square":
132
-                    result = calc.square(num1);
133
-                    memory = result;
134
-                    calc.printAns(String.valueOf(result));
135
-                    break;
136
-                    
137
-                    case "inverse":
138
-                    result = calc.inverse(num1);
139
-                    memory = result;
140
-                    calc.printAns(String.format("1/%.0f", result));
141
-                    break;
142
-                    
143
-                    case "tangent":
144
-                    result = calc.tangent(num1);
145
-                    memory = result;
146
-                    calc.printAns(String.valueOf(result));
147
-                    break;
148
-                    
149
-                    case "inverse tangent":
150
-                    result = calc.iTan(num1);
151
-                    memory = result;
152
-                    calc.printAns(String.valueOf(result));
153
-                    break;
154
-                    
155
-                    
156 53
                 }
157 54
             }
55
+        
56
+            
57
+            else {
58
+
59
+            //System.out.println("toDO is: " + toDo);
60
+
61
+            System.out.println(toDo + " -- Enter first number:");
62
+            num1 = input1.nextDouble();
63
+            //System.out.println("num1 is: " + num1);
64
+
65
+            switch (toDo){
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
+                case "add":
79
+                result = calc.add(num1, num2);
80
+                memory = result;
81
+                calc.printAns(String.valueOf(result));
82
+                break;
83
+
84
+                case "subtract":
85
+                result = calc.subtract(num1, num2);
86
+                memory = result;
87
+                calc.printAns(String.valueOf(result));
88
+                break;
89
+
90
+                case "multiply":
91
+                result = calc.multiply(num1, num2);
92
+                memory = result;
93
+                calc.printAns(String.valueOf(result));
94
+                break;
95
+
96
+                case "divide":
97
+                result = calc.divide(num1, num2);
98
+                memory = result;
99
+                calc.printAns(String.valueOf(result));
100
+                break;
101
+
102
+                case "exponent":
103
+                result = calc.exponent(num1, num2);
104
+                memory = result;
105
+                calc.printAns(String.valueOf(result));
106
+                break;
107
+
108
+                case "cosine":
109
+                result = calc.cosine(num1);
110
+                memory = result;
111
+                calc.printAns(String.valueOf(result));
112
+                break;
113
+
114
+                case "inverse cosine":
115
+                result = calc.invCosine(num1);
116
+                memory = result;
117
+                calc.printAns(String.valueOf(result));
118
+                break;
119
+
120
+                case "square root":
121
+                result = calc.squareRoot(num1);
122
+                memory = result;
123
+                calc.printAns(String.valueOf(result));
124
+                break;
125
+
126
+                case "invert":
127
+                result = calc.invert(num1);
128
+                memory = result;
129
+                calc.printAns(String.valueOf(result));
130
+                break;
131
+
132
+                case "sine":
133
+                result = calc.sin(num1);
134
+                memory = result;
135
+                calc.printAns(String.valueOf(result));
136
+                break;
137
+
138
+                case "inverse sine":
139
+                result = calc.iSin(num1);
140
+                memory = result;
141
+                calc.printAns(String.valueOf(result));
142
+                break;
143
+
144
+                case "degree to radian":
145
+                result = calc.degToRad(num1);
146
+                memory = result;
147
+                calc.printAns(String.valueOf(result));
148
+                break;
149
+
150
+                case "radian to degree":
151
+                result = calc.radToDeg(num1);
152
+                memory = result;
153
+                calc.printAns(String.valueOf(result));
154
+                break;
155
+
156
+                case "square":
157
+                result = calc.square(num1);
158
+                memory = result;
159
+                calc.printAns(String.valueOf(result));
160
+                break;
161
+
162
+                case "inverse":
163
+                result = calc.inverse(num1);
164
+                memory = result;
165
+                calc.printAns(String.valueOf(result));
166
+                break;
167
+
168
+                case "tangent":
169
+                result = calc.tangent(num1);
170
+                memory = result;
171
+                calc.printAns(String.valueOf(result));
172
+                break;
173
+
174
+                case "inverse tangent":
175
+                result = calc.iTan(num1);
176
+                memory = result;
177
+                calc.printAns(String.valueOf(result));
178
+                break; 
179
+
180
+            }
158 181
         }
159 182
     }
160 183
 }
184
+}
161 185
 
162
-        // /**
163
-        // * Created by leon on 2/9/18.
164
-        // */
165
-        // public class Console {
166
-        // Calculator calc = new Calculator();
167
-
168
-        // public static void print(String output, Object... args) {
169
-        // System.out.printf(output, args);
170
-        // }
171
-
172
-        // public static void println(String output, Object... args) {
173
-        // print(output + "\n", args);
174
-        // }
175
-
176
-        // public static String getMethod() {
177
-        // Scanner scanner = new Scanner(System.in);
178
-        // String userInput = scanner.nextLine();
179
-        // return userInput;
180
-        // }
181
-
182
-        // public static String switchStatment() {
183
-        // Scanner scanner = new Scanner(System.in);
184
-        // String ultimateAnswer = "";
185
-        // Boolean turnOff = false;
186
-
187
-        // switch (getMethod()) {
188
-        // case "add" :
189
-        // System.out.println("Enter your numbers:");
190
-        // double userInput = scanner.nextDouble();
191
-        // double nextInput = scanner.nextDouble();
192
-
193
-        // double answer = Calculator.add(userInput, nextInput);
194
-        // ultimateAnswer = Double.toString(answer);
195
-        // return Double.toString(answer);
196
-
197
-        // case "subtract" :
198
-        // System.out.println("Enter your numbers:");
199
-        // userInput = scanner.nextDouble();
200
-        // nextInput = scanner.nextDouble();
201
-
202
-        // answer = Calculator.subtract(userInput, nextInput);
203
-        // ultimateAnswer = Double.toString(answer);
204
-        // return ultimateAnswer;
205
-
206
-        // case "multiply" :
207
-        // System.out.println("Enter your numbers:");
208
-        // userInput = scanner.nextDouble();
209
-        // nextInput = scanner.nextDouble();
210
-
211
-        // answer = Calculator.multiply(userInput, nextInput);
212
-        // ultimateAnswer = Double.toString(answer);
213
-        // return ultimateAnswer;
214
-
215
-        // case "divide" :
216
-        // System.out.println("Enter your numbers:");
217
-        // userInput = scanner.nextDouble();
218
-        // nextInput = scanner.nextDouble();
219
-
220
-        // answer = Calculator.divide(userInput, nextInput);
221
-        // ultimateAnswer = Double.toString(answer);
222
-        // return ultimateAnswer;
223
-
224
-        // case "Square Root" :
225
-
226
-           
227
-        // }
228
-        // return ultimateAnswer;
229
-        // }
230
-
231
-        // /*public static String getStringInput(String prompt) {
232
-        // Scanner scanner = new Scanner(System.in);
233
-        // println(prompt);
234
-        // String userInput = scanner.nextLine();
235
-        // return userInput;
236
-        // }
237
-
238
-        // public static Integer getIntegerInput(String prompt) {
239
-        // Scanner scanner = new Scanner(System.in);
240
-        // println(prompt);
241
-        // int userInput = scanner.nextInt();
242
-        // return null;
243
-        // }
244
-
245
-        // public static Double getDoubleInput(String prompt) {
246
-        // Scanner scanner = new Scanner(System.in);
247
-        // println(prompt);
248
-        // double userInput = scanner.nextDouble();
249
-        // double nextInput = scanner.nextDouble();
250
-        // double answer = Calculator.add(userInput, nextInput);
251
-        // return answer;
252
-        // }*/
253
-
254
-        // }
186
+// /**
187
+// * Created by leon on 2/9/18.
188
+// */
189
+// public class Console {
190
+// Calculator calc = new Calculator();
191
+
192
+// public static void print(String output, Object... args) {
193
+// System.out.printf(output, args);
194
+// }
195
+
196
+// public static void println(String output, Object... args) {
197
+// print(output + "\n", args);
198
+// }
199
+
200
+// public static String getMethod() {
201
+// Scanner scanner = new Scanner(System.in);
202
+// String userInput = scanner.nextLine();
203
+// return userInput;
204
+// }
205
+
206
+// public static String switchStatment() {
207
+// Scanner scanner = new Scanner(System.in);
208
+// String ultimateAnswer = "";
209
+// Boolean turnOff = false;
210
+
211
+// switch (getMethod()) {
212
+// case "add" :
213
+// System.out.println("Enter your numbers:");
214
+// double userInput = scanner.nextDouble();
215
+// double nextInput = scanner.nextDouble();
216
+
217
+// double answer = Calculator.add(userInput, nextInput);
218
+// ultimateAnswer = Double.toString(answer);
219
+// return Double.toString(answer);
220
+
221
+// case "subtract" :
222
+// System.out.println("Enter your numbers:");
223
+// userInput = scanner.nextDouble();
224
+// nextInput = scanner.nextDouble();
225
+
226
+// answer = Calculator.subtract(userInput, nextInput);
227
+// ultimateAnswer = Double.toString(answer);
228
+// return ultimateAnswer;
229
+
230
+// case "multiply" :
231
+// System.out.println("Enter your numbers:");
232
+// userInput = scanner.nextDouble();
233
+// nextInput = scanner.nextDouble();
234
+
235
+// answer = Calculator.multiply(userInput, nextInput);
236
+// ultimateAnswer = Double.toString(answer);
237
+// return ultimateAnswer;
238
+
239
+// case "divide" :
240
+// System.out.println("Enter your numbers:");
241
+// userInput = scanner.nextDouble();
242
+// nextInput = scanner.nextDouble();
243
+
244
+// answer = Calculator.divide(userInput, nextInput);
245
+// ultimateAnswer = Double.toString(answer);
246
+// return ultimateAnswer;
247
+
248
+// case "Square Root" :
249
+
250
+// }
251
+// return ultimateAnswer;
252
+// }
253
+
254
+// /*public static String getStringInput(String prompt) {
255
+// Scanner scanner = new Scanner(System.in);
256
+// println(prompt);
257
+// String userInput = scanner.nextLine();
258
+// return userInput;
259
+// }
260
+
261
+// public static Integer getIntegerInput(String prompt) {
262
+// Scanner scanner = new Scanner(System.in);
263
+// println(prompt);
264
+// int userInput = scanner.nextInt();
265
+// return null;
266
+// }
267
+
268
+// public static Double getDoubleInput(String prompt) {
269
+// Scanner scanner = new Scanner(System.in);
270
+// println(prompt);
271
+// double userInput = scanner.nextDouble();
272
+// double nextInput = scanner.nextDouble();
273
+// double answer = Calculator.add(userInput, nextInput);
274
+// return answer;
275
+// }*/
276
+
277
+// }

+ 1
- 1
MathMethods.java Ver fichero

@@ -73,7 +73,7 @@ public class MathMethods
73 73
     }
74 74
 
75 75
     public static double inverse(double n1) {
76
-        return n1;
76
+        return 1 / n1;
77 77
     }
78 78
 
79 79
     public static double tangent(double n1) {

+ 7
- 5
MathMethodsTest.java Ver fichero

@@ -46,11 +46,6 @@ public class MathMethodsTest
46 46
         assertEquals(16, MathMethods.square(4), 0.1);
47 47
     }
48 48
 
49
-    @Test
50
-    public void inverseTest()
51
-    {
52
-        assertEquals(5, MathMethods.inverse(5), 0.1);
53
-    }
54 49
 
55 50
     @Test
56 51
     public void tangentTest()
@@ -63,8 +58,15 @@ public class MathMethodsTest
63 58
     {
64 59
         assertEquals(1.550798992821746, MathMethods.iTan(50), 0.1);
65 60
     }
61
+
62
+    @Test
63
+    public void inverseTest()
64
+    {
65
+        assertEquals(.1, MathMethods.inverse(10), 0.1);
66
+    }
66 67
 }
67 68
 
68 69
 
69 70
 
70 71
 
72
+

+ 36
- 0
Memory.java Ver fichero

@@ -0,0 +1,36 @@
1
+
2
+/**
3
+ * Write a description of class Memory here.
4
+ *
5
+ * @author (your name)
6
+ * @version (a version number or a date)
7
+ */
8
+public class Memory
9
+{
10
+    private static double mem;
11
+    
12
+    public Memory()
13
+    {
14
+        // initialise instance variables
15
+        mem = 0;
16
+    }
17
+
18
+    public static double memRead() 
19
+    {
20
+        return mem;    
21
+    }
22
+    
23
+    public static double memUpdate(double n1) 
24
+    {
25
+        return mem = n1;
26
+    }
27
+    
28
+    public static double memClear()
29
+    {
30
+        return mem = 0;
31
+    }
32
+    
33
+    public static void printAns(String printAnswer){
34
+        System.out.println(printAnswer);       
35
+    }
36
+}

+ 66
- 0
MemoryTest.java Ver fichero

@@ -0,0 +1,66 @@
1
+
2
+
3
+import static org.junit.Assert.*;
4
+import org.junit.After;
5
+import org.junit.Before;
6
+import org.junit.Test;
7
+
8
+/**
9
+ * The test class MemoryTest.
10
+ *
11
+ * @author  (your name)
12
+ * @version (a version number or a date)
13
+ */
14
+public class MemoryTest
15
+{
16
+    /**
17
+     * Default constructor for test class MemoryTest
18
+     */
19
+    public MemoryTest()
20
+    {
21
+    }
22
+
23
+    /**
24
+     * Sets up the test fixture.
25
+     *
26
+     * Called before every test case method.
27
+     */
28
+    @Before
29
+    public void setUp()
30
+    {
31
+    }
32
+
33
+    /**
34
+     * Tears down the test fixture.
35
+     *
36
+     * Called after every test case method.
37
+     */
38
+    @After
39
+    public void tearDown()
40
+    {
41
+    }
42
+
43
+    @Test
44
+    public void memTest()
45
+    {
46
+        assertEquals(0, Memory.memRead(), 0.1);
47
+    }
48
+
49
+    @Test
50
+    public void memUpdateTest()
51
+    {
52
+        assertEquals(10, Memory.memUpdate(10), 0.1);
53
+    }
54
+
55
+
56
+
57
+    @Test
58
+    public void memClearTest()
59
+    {
60
+        assertEquals(0, Memory.memClear(), 0.1);
61
+    }
62
+}
63
+
64
+
65
+
66
+

+ 45
- 13
package.bluej Ver fichero

@@ -1,10 +1,19 @@
1 1
 #BlueJ package file
2
-dependency1.from=Calculator
2
+dependency1.from=MathMethodsTest
3 3
 dependency1.to=MathMethods
4 4
 dependency1.type=UsesDependency
5
+dependency2.from=Calculator
6
+dependency2.to=MathMethods
7
+dependency2.type=UsesDependency
8
+dependency3.from=Calculator
9
+dependency3.to=Memory
10
+dependency3.type=UsesDependency
11
+dependency4.from=MemoryTest
12
+dependency4.to=Memory
13
+dependency4.type=UsesDependency
5 14
 editor.fx.0.height=797
6 15
 editor.fx.0.width=1247
7
-editor.fx.0.x=78
16
+editor.fx.0.x=87
8 17
 editor.fx.0.y=23
9 18
 objectbench.height=80
10 19
 objectbench.width=595
@@ -16,8 +25,8 @@ package.editor.x=102
16 25
 package.editor.y=23
17 26
 package.frame.height=625
18 27
 package.frame.width=619
19
-package.numDependencies=1
20
-package.numTargets=2
28
+package.numDependencies=4
29
+package.numTargets=5
21 30
 package.showExtends=true
22 31
 package.showUses=true
23 32
 project.charset=UTF-8
@@ -27,16 +36,39 @@ readme.width=47
27 36
 readme.x=10
28 37
 readme.y=10
29 38
 target1.height=50
30
-target1.name=Calculator
39
+target1.name=MemoryTest
31 40
 target1.showInterface=false
32
-target1.type=ClassTarget
33
-target1.width=90
34
-target1.x=200
35
-target1.y=310
41
+target1.type=UnitTestTargetJunit4
42
+target1.width=80
43
+target1.x=400
44
+target1.y=140
45
+target2.association=MemoryTest
36 46
 target2.height=50
37
-target2.name=MathMethods
47
+target2.name=Memory
38 48
 target2.showInterface=false
39 49
 target2.type=ClassTarget
40
-target2.width=110
41
-target2.x=70
42
-target2.y=10
50
+target2.width=80
51
+target2.x=370
52
+target2.y=170
53
+target3.height=50
54
+target3.name=Calculator
55
+target3.showInterface=false
56
+target3.type=ClassTarget
57
+target3.width=90
58
+target3.x=240
59
+target3.y=80
60
+target4.height=50
61
+target4.name=MathMethodsTest
62
+target4.showInterface=false
63
+target4.type=UnitTestTargetJunit4
64
+target4.width=110
65
+target4.x=170
66
+target4.y=280
67
+target5.association=MathMethodsTest
68
+target5.height=50
69
+target5.name=MathMethods
70
+target5.showInterface=false
71
+target5.type=ClassTarget
72
+target5.width=110
73
+target5.x=140
74
+target5.y=310