Browse Source

formatting changes

Margaret Pierse 6 years ago
parent
commit
8525839467
6 changed files with 306 additions and 49 deletions
  1. 86
    31
      Calculator.java
  2. 24
    7
      MathMethods.java
  3. 25
    10
      MathMethodsTest.java
  4. 36
    0
      Memory.java
  5. 66
    0
      MemoryTest.java
  6. 69
    1
      package.bluej

+ 86
- 31
Calculator.java View File

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

+ 24
- 7
MathMethods.java View File

@@ -80,25 +80,28 @@ public class MathMethods
80 80
         return wiseWords[arrIndex];
81 81
     }
82 82
 
83
+    public static double square(double n1) {
84
+        return Math.pow(n1, 2);}
85
+
83 86
     public static String switchDisplayMode(String mode){
84
-        Calculator display = new Calculator();
85 87
 
88
+        Calculator display = new Calculator();
86 89
         String answer = "";
87 90
         switch (mode) {
88 91
             case "binary":
89
-            answer = "0b " + Integer.toBinaryString((int)display.memory);
92
+            answer = "0b " + Integer.toBinaryString((int)display.lastAns);
90 93
             break;
91 94
 
92 95
             case "octal":
93
-            answer = "0 " + Integer.toOctalString((int)display.memory);
96
+            answer = "0 " + Integer.toOctalString((int)display.lastAns);
94 97
             break;
95 98
 
96 99
             case "decimal":
97
-            answer = String.valueOf(display.memory);
100
+            answer = String.valueOf(display.lastAns);
98 101
             break;
99 102
 
100 103
             case "hexadecimal":
101
-            answer = "0x " + Integer.toHexString((int)display.memory);
104
+            answer = "0x " + Integer.toHexString((int)display.lastAns);
102 105
             break;
103 106
 
104 107
         }
@@ -106,9 +109,7 @@ public class MathMethods
106 109
     }
107 110
 
108 111
     public String switchDisplayMode(){
109
-
110 112
         String answer = "";
111
-
112 113
         if (displayModeCounter%4 == 0){ 
113 114
             answer = switchDisplayMode("binary");
114 115
             displayModeCounter++;
@@ -135,7 +136,23 @@ public class MathMethods
135 136
 
136 137
     public static void printAns(String printAnswer){
137 138
         System.out.println("ANSWER: " + printAnswer + "\n \n");
139
+    }
138 140
 
141
+    public static double inverse(double n1) {
142
+        return 1 / n1;
143
+    }
144
+
145
+    public static double tangent(double n1) {
146
+        return Math.tan(n1);
147
+    }
148
+
149
+    public static double iTan(double n1) {
150
+        return Math.atan(n1);
151
+    }
152
+
153
+    public static double state() {
154
+        Calculator display = new Calculator();
155
+        return (display.lastAns);
139 156
     }
140 157
 
141 158
 }

+ 25
- 10
MathMethodsTest.java View File

@@ -1,5 +1,4 @@
1 1
 
2
-
3 2
 import static org.junit.Assert.*;
4 3
 import org.junit.After;
5 4
 import org.junit.Before;
@@ -41,6 +40,7 @@ public class MathMethodsTest
41 40
     }
42 41
 
43 42
     @Test
43
+
44 44
     public void testSquareRoot()
45 45
     {
46 46
         assertEquals(16.0, MathMethods.squareRoot(256), 0.1);
@@ -76,15 +76,13 @@ public class MathMethodsTest
76 76
         assertEquals(57.2957791308232, MathMethods.radToDeg(1), 0.1);
77 77
     }
78 78
 
79
-    
80
-
81 79
     // @Test
82 80
     // public void testOctalDisplay()
83 81
     // {
84
-        // MathMethods mathMethods1 = new MathMethods();
85
-        // mathMethods1.memory = 20;
86
-        
87
-        // assertEquals("0 24", MathMethods.switchDisplayMode("octal"));
82
+    // MathMethods mathMethods1 = new MathMethods();
83
+    // mathMethods1.memory = 20;
84
+
85
+    // assertEquals("0 24", MathMethods.switchDisplayMode("octal"));
88 86
     // }
89 87
 
90 88
     @Test
@@ -92,13 +90,30 @@ public class MathMethodsTest
92 90
     {
93 91
         assertEquals(1.5707963267948966, MathMethods.iSin(1), 0.1);
94 92
     }
95
-}
96
-
97
-
98 93
 
94
+    public void squareTest()
95
+    {
96
+        assertEquals(16, MathMethods.square(4), 0.1);
97
+    }
99 98
 
99
+    @Test
100
+    public void tangentTest()
101
+    {
102
+        assertEquals(-3.380515005246586, MathMethods.tangent(5), 0.1);
103
+    }
100 104
 
105
+    @Test
106
+    public void inverseTangentTest()
107
+    {
108
+        assertEquals(1.550798992821746, MathMethods.iTan(50), 0.1);
109
+    }
101 110
 
111
+    @Test
112
+    public void inverseTest()
113
+    {
114
+        assertEquals(.1, MathMethods.inverse(10), 0.1);
102 115
 
116
+    }
117
+}
103 118
 
104 119
 

+ 36
- 0
Memory.java View File

@@ -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 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 View File

@@ -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
+

+ 69
- 1
package.bluej View File

@@ -1,4 +1,5 @@
1 1
 #BlueJ package file
2
+<<<<<<< HEAD
2 3
 dependency1.from=Calculator
3 4
 dependency1.to=MathMethods
4 5
 dependency1.type=UsesDependency
@@ -24,6 +25,36 @@ package.frame.height=807
24 25
 package.frame.width=832
25 26
 package.numDependencies=3
26 27
 package.numTargets=3
28
+=======
29
+dependency1.from=MathMethodsTest
30
+dependency1.to=MathMethods
31
+dependency1.type=UsesDependency
32
+dependency2.from=MemoryTest
33
+dependency2.to=Memory
34
+dependency2.type=UsesDependency
35
+dependency3.from=Calculator
36
+dependency3.to=MathMethods
37
+dependency3.type=UsesDependency
38
+dependency4.from=Calculator
39
+dependency4.to=Memory
40
+dependency4.type=UsesDependency
41
+editor.fx.0.height=814
42
+editor.fx.0.width=1191
43
+editor.fx.0.x=201
44
+editor.fx.0.y=-974
45
+objectbench.height=80
46
+objectbench.width=595
47
+package.divider.horizontal=0.5993322203672788
48
+package.divider.vertical=0.8465608465608465
49
+package.editor.height=473
50
+package.editor.width=493
51
+package.editor.x=-188
52
+package.editor.y=-610
53
+package.frame.height=625
54
+package.frame.width=619
55
+package.numDependencies=4
56
+package.numTargets=5
57
+>>>>>>> 2a9a289e4239250cd6e2f776f8cc64f1130c5a52
27 58
 package.showExtends=true
28 59
 package.showUses=true
29 60
 project.charset=UTF-8
@@ -33,8 +64,9 @@ readme.width=47
33 64
 readme.x=10
34 65
 readme.y=10
35 66
 target1.height=50
36
-target1.name=Calculator
67
+target1.name=MemoryTest
37 68
 target1.showInterface=false
69
+<<<<<<< HEAD
38 70
 target1.type=ClassTarget
39 71
 target1.width=140
40 72
 target1.x=80
@@ -54,3 +86,39 @@ target3.type=ClassTarget
54 86
 target3.width=110
55 87
 target3.x=240
56 88
 target3.y=340
89
+=======
90
+target1.type=UnitTestTargetJunit4
91
+target1.width=80
92
+target1.x=400
93
+target1.y=140
94
+target2.association=MemoryTest
95
+target2.height=50
96
+target2.name=Memory
97
+target2.showInterface=false
98
+target2.type=ClassTarget
99
+target2.width=80
100
+target2.x=370
101
+target2.y=170
102
+target3.height=50
103
+target3.name=Calculator
104
+target3.showInterface=false
105
+target3.type=ClassTarget
106
+target3.width=90
107
+target3.x=220
108
+target3.y=80
109
+target4.height=50
110
+target4.name=MathMethodsTest
111
+target4.showInterface=false
112
+target4.type=UnitTestTargetJunit4
113
+target4.width=110
114
+target4.x=90
115
+target4.y=250
116
+target5.association=MathMethodsTest
117
+target5.height=50
118
+target5.name=MathMethods
119
+target5.showInterface=false
120
+target5.type=ClassTarget
121
+target5.width=110
122
+target5.x=60
123
+target5.y=280
124
+>>>>>>> 2a9a289e4239250cd6e2f776f8cc64f1130c5a52