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
 import java.util.*;
1
 import java.util.*;
2
+
2
 public class Calculator{
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
     public static void main(String args[]){
15
     public static void main(String args[]){
5
-        
6
         String toDo;                //what the user wants to do
16
         String toDo;                //what the user wants to do
7
         double num1 = 0;                //The user's first input
17
         double num1 = 0;                //The user's first input
8
         double num2 = 0;            //The user's second input
18
         double num2 = 0;            //The user's second input
15
         while (off == false){
25
         while (off == false){
16
             Scanner input1 = new Scanner(System.in);
26
             Scanner input1 = new Scanner(System.in);
17
             System.out.println("What do you want to do?");
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
                 off = true;
31
                 off = true;
22
                 System.out.println("Goodbye!! \n \n");
32
                 System.out.println("Goodbye!! \n \n");
23
 
33
 
24
             } else {
34
             } else {
25
                 switch (toDo) {
35
                 switch (toDo) {
26
-                //System.out.println("toDO is: " + toDo);
36
+                    //System.out.println("toDO is: " + toDo);
27
                     case "add":
37
                     case "add":
28
                     case "subtract":
38
                     case "subtract":
29
                     case "multiply":
39
                     case "multiply":
37
                     case "inverse sine" :
47
                     case "inverse sine" :
38
                     case "degree to radian" :
48
                     case "degree to radian" :
39
                     case "radian to degree" :
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
                 switch (toDo){
59
                 switch (toDo){
45
                     case "add":
60
                     case "add":
46
                     case "subtract":
61
                     case "subtract":
56
                 switch (toDo) {        
71
                 switch (toDo) {        
57
                     case "add":
72
                     case "add":
58
                     result = calc.add(num1, num2);
73
                     result = calc.add(num1, num2);
59
-                    memory = result;
74
+                    lastAns = result;
60
                     calc.printAns(String.valueOf(result));
75
                     calc.printAns(String.valueOf(result));
61
                     break;
76
                     break;
62
 
77
 
63
                     case "subtract":
78
                     case "subtract":
64
                     result = calc.subtract(num1, num2);
79
                     result = calc.subtract(num1, num2);
65
-                    memory = result;
80
+                    lastAns = result;
66
                     calc.printAns(String.valueOf(result));
81
                     calc.printAns(String.valueOf(result));
67
                     break;
82
                     break;
68
 
83
 
69
                     case "multiply":
84
                     case "multiply":
70
                     result = calc.multiply(num1, num2);
85
                     result = calc.multiply(num1, num2);
71
-                    memory = result;
86
+                    lastAns = result;
72
                     calc.printAns(String.valueOf(result));
87
                     calc.printAns(String.valueOf(result));
73
                     break;
88
                     break;
74
 
89
 
75
                     case "divide":
90
                     case "divide":
76
                     result = calc.divide(num1, num2);
91
                     result = calc.divide(num1, num2);
77
-                    memory = result;
92
+                    lastAns = result;
78
                     calc.printAns(String.valueOf(result));
93
                     calc.printAns(String.valueOf(result));
79
                     break;
94
                     break;
80
 
95
 
81
                     case "exponent":
96
                     case "exponent":
82
                     result = calc.exponent(num1, num2);
97
                     result = calc.exponent(num1, num2);
83
-                    memory = result;
98
+                    lastAns = result;
84
                     calc.printAns(String.valueOf(result));
99
                     calc.printAns(String.valueOf(result));
85
                     break;
100
                     break;
86
 
101
 
87
                     case "cosine":
102
                     case "cosine":
88
                     result = calc.cosine(num1);
103
                     result = calc.cosine(num1);
89
-                    memory = result;
104
+                    lastAns = result;
90
                     calc.printAns(String.valueOf(result));
105
                     calc.printAns(String.valueOf(result));
91
                     break;
106
                     break;
92
 
107
 
93
                     case "inverse cosine":
108
                     case "inverse cosine":
94
                     result = calc.invCosine(num1);
109
                     result = calc.invCosine(num1);
95
-                    memory = result;
110
+                    lastAns = result;
96
                     calc.printAns(String.valueOf(result));
111
                     calc.printAns(String.valueOf(result));
97
                     break;
112
                     break;
98
 
113
 
99
                     case "square root":
114
                     case "square root":
100
                     result = calc.squareRoot(num1);
115
                     result = calc.squareRoot(num1);
101
-                    memory = result;
116
+                    lastAns = result;
102
                     calc.printAns(String.valueOf(result));
117
                     calc.printAns(String.valueOf(result));
103
                     break;
118
                     break;
104
 
119
 
105
                     case "invert":
120
                     case "invert":
106
                     result = calc.invert(num1);
121
                     result = calc.invert(num1);
107
-                    memory = result;
122
+                    lastAns = result;
108
                     calc.printAns(String.valueOf(result));
123
                     calc.printAns(String.valueOf(result));
109
                     break;
124
                     break;
110
 
125
 
111
                     case "sine":
126
                     case "sine":
112
                     result = calc.sin(num1);
127
                     result = calc.sin(num1);
113
-                    memory = result;
128
+                    lastAns = result;
114
                     calc.printAns(String.valueOf(result));
129
                     calc.printAns(String.valueOf(result));
115
                     break;
130
                     break;
116
 
131
 
117
                     case "inverse sine":
132
                     case "inverse sine":
118
                     result = calc.iSin(num1);
133
                     result = calc.iSin(num1);
119
-                    memory = result;
134
+                    lastAns = result;
120
                     calc.printAns(String.valueOf(result));
135
                     calc.printAns(String.valueOf(result));
121
                     break;
136
                     break;
122
 
137
 
123
                     case "degree to radian":
138
                     case "degree to radian":
124
                     result = calc.degToRad(num1);
139
                     result = calc.degToRad(num1);
125
-                    memory = result;
140
+                    lastAns = result;
126
                     calc.printAns(String.valueOf(result));
141
                     calc.printAns(String.valueOf(result));
127
                     break;
142
                     break;
128
 
143
 
129
                     case "radian to degree":
144
                     case "radian to degree":
130
                     result = calc.radToDeg(num1);
145
                     result = calc.radToDeg(num1);
131
-                    memory = result;
146
+                    lastAns = result;
132
                     calc.printAns(String.valueOf(result));
147
                     calc.printAns(String.valueOf(result));
133
                     break;
148
                     break;
134
 
149
 
137
                     break;
152
                     break;
138
 
153
 
139
                     case "display mode decimal":
154
                     case "display mode decimal":
140
-                    calc.printAns(String.valueOf(memory));
155
+                    calc.printAns(String.valueOf(lastAns));
141
                     break;
156
                     break;
142
-                    
157
+
143
                     case "display mode hexadecimal":
158
                     case "display mode hexadecimal":
144
                     calc.printAns(calc.switchDisplayMode("hexadecimal"));
159
                     calc.printAns(calc.switchDisplayMode("hexadecimal"));
145
-                    
160
+
146
                     case "display mode octal":
161
                     case "display mode octal":
147
                     calc.printAns(calc.switchDisplayMode("octal"));
162
                     calc.printAns(calc.switchDisplayMode("octal"));
148
                     break;
163
                     break;
149
-                    
164
+
150
                     case "display mode binary":
165
                     case "display mode binary":
151
                     calc.printAns(calc.switchDisplayMode("binary"));
166
                     calc.printAns(calc.switchDisplayMode("binary"));
152
                     break;
167
                     break;
153
-                    
168
+
154
                     case "display mode" :
169
                     case "display mode" :
155
                     calc.printAns(calc.switchDisplayMode());
170
                     calc.printAns(calc.switchDisplayMode());
156
                     break;
171
                     break;
157
-                    
172
+
158
                     case "random number":
173
                     case "random number":
159
                     result = calc.randomNumber();
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
                     calc.printAns(String.valueOf(result));
194
                     calc.printAns(String.valueOf(result));
162
                     break;
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
         return wiseWords[arrIndex];
80
         return wiseWords[arrIndex];
81
     }
81
     }
82
 
82
 
83
+    public static double square(double n1) {
84
+        return Math.pow(n1, 2);}
85
+
83
     public static String switchDisplayMode(String mode){
86
     public static String switchDisplayMode(String mode){
84
-        Calculator display = new Calculator();
85
 
87
 
88
+        Calculator display = new Calculator();
86
         String answer = "";
89
         String answer = "";
87
         switch (mode) {
90
         switch (mode) {
88
             case "binary":
91
             case "binary":
89
-            answer = "0b " + Integer.toBinaryString((int)display.memory);
92
+            answer = "0b " + Integer.toBinaryString((int)display.lastAns);
90
             break;
93
             break;
91
 
94
 
92
             case "octal":
95
             case "octal":
93
-            answer = "0 " + Integer.toOctalString((int)display.memory);
96
+            answer = "0 " + Integer.toOctalString((int)display.lastAns);
94
             break;
97
             break;
95
 
98
 
96
             case "decimal":
99
             case "decimal":
97
-            answer = String.valueOf(display.memory);
100
+            answer = String.valueOf(display.lastAns);
98
             break;
101
             break;
99
 
102
 
100
             case "hexadecimal":
103
             case "hexadecimal":
101
-            answer = "0x " + Integer.toHexString((int)display.memory);
104
+            answer = "0x " + Integer.toHexString((int)display.lastAns);
102
             break;
105
             break;
103
 
106
 
104
         }
107
         }
106
     }
109
     }
107
 
110
 
108
     public String switchDisplayMode(){
111
     public String switchDisplayMode(){
109
-
110
         String answer = "";
112
         String answer = "";
111
-
112
         if (displayModeCounter%4 == 0){ 
113
         if (displayModeCounter%4 == 0){ 
113
             answer = switchDisplayMode("binary");
114
             answer = switchDisplayMode("binary");
114
             displayModeCounter++;
115
             displayModeCounter++;
135
 
136
 
136
     public static void printAns(String printAnswer){
137
     public static void printAns(String printAnswer){
137
         System.out.println("ANSWER: " + printAnswer + "\n \n");
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
 
1
 
2
-
3
 import static org.junit.Assert.*;
2
 import static org.junit.Assert.*;
4
 import org.junit.After;
3
 import org.junit.After;
5
 import org.junit.Before;
4
 import org.junit.Before;
41
     }
40
     }
42
 
41
 
43
     @Test
42
     @Test
43
+
44
     public void testSquareRoot()
44
     public void testSquareRoot()
45
     {
45
     {
46
         assertEquals(16.0, MathMethods.squareRoot(256), 0.1);
46
         assertEquals(16.0, MathMethods.squareRoot(256), 0.1);
76
         assertEquals(57.2957791308232, MathMethods.radToDeg(1), 0.1);
76
         assertEquals(57.2957791308232, MathMethods.radToDeg(1), 0.1);
77
     }
77
     }
78
 
78
 
79
-    
80
-
81
     // @Test
79
     // @Test
82
     // public void testOctalDisplay()
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
     @Test
88
     @Test
92
     {
90
     {
93
         assertEquals(1.5707963267948966, MathMethods.iSin(1), 0.1);
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

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

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
 #BlueJ package file
1
 #BlueJ package file
2
+<<<<<<< HEAD
2
 dependency1.from=Calculator
3
 dependency1.from=Calculator
3
 dependency1.to=MathMethods
4
 dependency1.to=MathMethods
4
 dependency1.type=UsesDependency
5
 dependency1.type=UsesDependency
24
 package.frame.width=832
25
 package.frame.width=832
25
 package.numDependencies=3
26
 package.numDependencies=3
26
 package.numTargets=3
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
 package.showExtends=true
58
 package.showExtends=true
28
 package.showUses=true
59
 package.showUses=true
29
 project.charset=UTF-8
60
 project.charset=UTF-8
33
 readme.x=10
64
 readme.x=10
34
 readme.y=10
65
 readme.y=10
35
 target1.height=50
66
 target1.height=50
36
-target1.name=Calculator
67
+target1.name=MemoryTest
37
 target1.showInterface=false
68
 target1.showInterface=false
69
+<<<<<<< HEAD
38
 target1.type=ClassTarget
70
 target1.type=ClassTarget
39
 target1.width=140
71
 target1.width=140
40
 target1.x=80
72
 target1.x=80
54
 target3.width=110
86
 target3.width=110
55
 target3.x=240
87
 target3.x=240
56
 target3.y=340
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