瀏覽代碼

second merge

Seth 6 年之前
父節點
當前提交
9b8d42689e
共有 5 個檔案被更改,包括 197 行新增34 行删除
  1. 52
    9
      Calculator.java
  2. 70
    2
      MathMethods.java
  3. 52
    5
      MathMethodsTest.java
  4. 2
    0
      Memory.java
  5. 21
    18
      package.bluej

+ 52
- 9
Calculator.java 查看文件

@@ -1,5 +1,6 @@
1 1
 import java.util.*;
2 2
 
3
+
3 4
 /**
4 5
  * Write a description of class Calculator here.
5 6
  *
@@ -8,8 +9,9 @@ import java.util.*;
8 9
  */
9 10
 public class Calculator{
10 11
     public static double lastAns = 0;
11
-    public static void main(String args[]){
12 12
 
13
+
14
+    public static void main(String args[]){
13 15
         String toDo;                //what the user wants to do
14 16
         double num1 = 0;                //The user's first input
15 17
         double num2 = 0;            //The user's second input
@@ -25,11 +27,14 @@ public class Calculator{
25 27
 
26 28
             if (toDo.equalsIgnoreCase("quit")){
27 29
                 off = true;
28
-                System.out.println("Goodbye!!");
30
+                System.out.println("Goodbye!! \n \n");
31
+
32
+            } 
33
+                                         
34
+            else {                                     
35
+                switch (toDo) {
36
+                    //System.out.println("toDO is: " + toDo);
29 37
 
30
-            }                              
31
-            else {
32
-                switch(toDo){
33 38
                     case "add":
34 39
                     case "subtract":
35 40
                     case "multiply":
@@ -46,15 +51,20 @@ public class Calculator{
46 51
                     case "tangent" :
47 52
                     case "inverse tangent" :
48 53
                     case "square" :                 
49
-                    case "M+" :
50 54
                    
51 55
 
52 56
                     //System.out.println("toDO is: " + toDo);
53 57
                     System.out.println(toDo + " -- Enter first number:");
54 58
                     num1 = input1.nextDouble();
55 59
                     //System.out.println("num1 is: " + num1);
56
-                }   
60
+                }                  
61
+
57 62
                 switch (toDo){
63
+                    // case "previous":
64
+                    // num1 = lastAns;
65
+                    // System.out.println(toDo + " -- Enter second number:");
66
+                    // num2 = input1.nextDouble();
67
+                    // break;
58 68
                     case "add":
59 69
                     case "subtract":
60 70
                     case "multiply":
@@ -144,6 +154,35 @@ public class Calculator{
144 154
                     lastAns = result;
145 155
                     calc.printAns(String.valueOf(result));
146 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;      
147 186
 
148 187
                     case "square":
149 188
                     result = calc.square(num1);
@@ -175,7 +214,6 @@ public class Calculator{
175 214
                     break;
176 215
 
177 216
                     case "M+" :
178
-                    System.out.println("Enter number:");               
179 217
                     result = Memory.memUpdate(lastAns);
180 218
                     Memory.printAns(String.valueOf(result));
181 219
                     break;
@@ -184,9 +222,14 @@ public class Calculator{
184 222
                     result = Memory.memClear();
185 223
                     Memory.printAns(String.valueOf(result));
186 224
                     break;
187
-
225
+                    
226
+                    case "clear":
227
+                    lastAns = 0;
228
+                    break;
188 229
                 }
189 230
             }
190 231
         }
191 232
     }
233
+
192 234
 }
235
+

+ 70
- 2
MathMethods.java 查看文件

@@ -1,4 +1,5 @@
1 1
 import java.util.*;
2
+import java.lang.*;
2 3
 /**
3 4
  * Write a description of class Calculator here.
4 5
  *
@@ -8,6 +9,7 @@ import java.util.*;
8 9
 public class MathMethods
9 10
 
10 11
 {
12
+    private static int displayModeCounter=4;
11 13
 
12 14
     public static Double add(double n1, double n2){
13 15
         return (n1 + n2);
@@ -67,9 +69,74 @@ public class MathMethods
67 69
 
68 70
     }
69 71
 
72
+    public static String fortuneCookie() {
73
+        Random rand = new Random();
74
+        String[] wiseWords = new String[] {"You're a hard worker and you will do great on all the labs!", 
75
+                "Look how much java you learned this week! That's awesome!", 
76
+                "Keep up the good work!" ,
77
+                "You're cushing it!", "You're a coding rockstar!", 
78
+                "Keep coding!"};  
79
+        int arrIndex = rand.nextInt(6);
80
+        return wiseWords[arrIndex];
81
+    }
82
+
70 83
     public static double square(double n1) {
71 84
         return Math.pow(n1, 2);
85
+    }
86
+
87
+    public static String switchDisplayMode(String mode){
88
+
89
+        Calculator display = new Calculator();
90
+        String answer = "";
91
+        switch (mode) {
92
+            case "binary":
93
+            answer = "0b " + Integer.toBinaryString((int)display.lastAns);
94
+            break;
72 95
 
96
+            case "octal":
97
+            answer = "0 " + Integer.toOctalString((int)display.lastAns);
98
+            break;
99
+
100
+            case "decimal":
101
+            answer = String.valueOf(display.lastAns);
102
+            break;
103
+
104
+            case "hexadecimal":
105
+            answer = "0x " + Integer.toHexString((int)display.lastAns);
106
+            break;
107
+
108
+        }
109
+        return answer;
110
+    }
111
+
112
+    public String switchDisplayMode(){
113
+        String answer = "";
114
+        if (displayModeCounter%4 == 0){ 
115
+            answer = switchDisplayMode("binary");
116
+            displayModeCounter++;
117
+        } 
118
+        else if (displayModeCounter%4 == 1){
119
+            answer = switchDisplayMode("octal");
120
+            displayModeCounter++;
121
+        } 
122
+        else if (displayModeCounter%4 == 2) {
123
+            answer = switchDisplayMode("hexadecimal");
124
+            displayModeCounter++;
125
+        } else {
126
+            answer = switchDisplayMode("decimal");
127
+            displayModeCounter++;
128
+        }
129
+        return answer;
130
+    }
131
+
132
+    public static double randomNumber() {
133
+        Random rand = new Random();
134
+        return rand.nextDouble()*101;
135
+
136
+    }
137
+
138
+    public static void printAns(String printAnswer){
139
+        System.out.println("ANSWER: " + printAnswer + "\n \n");
73 140
     }
74 141
 
75 142
     public static double inverse(double n1) {
@@ -84,8 +151,9 @@ public class MathMethods
84 151
         return Math.atan(n1);
85 152
     }
86 153
 
87
-    public static void printAns(String printAnswer){
88
-        System.out.println("ANSWER: " + printAnswer);
154
+    public static double state() {
155
+        Calculator display = new Calculator();
156
+        return (display.lastAns);
89 157
     }
90 158
 
91 159
 }

+ 52
- 5
MathMethodsTest.java 查看文件

@@ -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,12 +40,62 @@ public class MathMethodsTest
41 40
     }
42 41
 
43 42
     @Test
43
+
44
+    public void testSquareRoot()
45
+    {
46
+        assertEquals(16.0, MathMethods.squareRoot(256), 0.1);
47
+    }
48
+
49
+    @Test
50
+    public void testInvertPositive()
51
+    {
52
+        assertEquals(-5.0, MathMethods.invert(5), 0.1);
53
+    }
54
+
55
+    @Test
56
+    public void testInvertNegative()
57
+    {
58
+        assertEquals(11.0, MathMethods.invert(-11), 0.1);
59
+    }
60
+
61
+    @Test
62
+    public void testSin()
63
+    {
64
+        assertEquals(-0.26237485370392877, MathMethods.sin(50), 0.1);
65
+    }
66
+
67
+    @Test
68
+    public void testDegToRad()
69
+    {
70
+        assertEquals(0.3490658503988659, MathMethods.degToRad(20), 0.1);
71
+    }
72
+
73
+    @Test
74
+    public void testRadToDeg()
75
+    {
76
+        assertEquals(57.2957791308232, MathMethods.radToDeg(1), 0.1);
77
+    }
78
+
79
+    // @Test
80
+    // public void testOctalDisplay()
81
+    // {
82
+    // MathMethods mathMethods1 = new MathMethods();
83
+    // mathMethods1.memory = 20;
84
+
85
+    // assertEquals("0 24", MathMethods.switchDisplayMode("octal"));
86
+    // }
87
+
88
+    @Test
89
+    public void testInverseSine()
90
+    {
91
+        assertEquals(1.5707963267948966, MathMethods.iSin(1), 0.1);
92
+    }
93
+
44 94
     public void squareTest()
45 95
     {
46 96
         assertEquals(16, MathMethods.square(4), 0.1);
47 97
     }
48 98
 
49
-
50 99
     @Test
51 100
     public void tangentTest()
52 101
     {
@@ -63,10 +112,8 @@ public class MathMethodsTest
63 112
     public void inverseTest()
64 113
     {
65 114
         assertEquals(.1, MathMethods.inverse(10), 0.1);
115
+
66 116
     }
67 117
 }
68 118
 
69 119
 
70
-
71
-
72
-

+ 2
- 0
Memory.java 查看文件

@@ -22,7 +22,9 @@ public class Memory
22 22
     
23 23
     public static double memUpdate(double lastAns) 
24 24
     {
25
+
25 26
         return mem = lastAns;
27
+
26 28
     }
27 29
     
28 30
     public static double memClear()

+ 21
- 18
package.bluej 查看文件

@@ -1,9 +1,9 @@
1 1
 #BlueJ package file
2
-dependency1.from=MathMethodsTest
3
-dependency1.to=MathMethods
2
+dependency1.from=MemoryTest
3
+dependency1.to=Memory
4 4
 dependency1.type=UsesDependency
5
-dependency2.from=MemoryTest
6
-dependency2.to=Memory
5
+dependency2.from=MathMethodsTest
6
+dependency2.to=MathMethods
7 7
 dependency2.type=UsesDependency
8 8
 dependency3.from=Calculator
9 9
 dependency3.to=MathMethods
@@ -11,21 +11,24 @@ dependency3.type=UsesDependency
11 11
 dependency4.from=Calculator
12 12
 dependency4.to=Memory
13 13
 dependency4.type=UsesDependency
14
-editor.fx.0.height=814
15
-editor.fx.0.width=1191
16
-editor.fx.0.x=201
17
-editor.fx.0.y=-974
18
-objectbench.height=80
19
-objectbench.width=595
14
+dependency5.from=MathMethods
15
+dependency5.to=Calculator
16
+dependency5.type=UsesDependency
17
+editor.fx.0.height=811
18
+editor.fx.0.width=900
19
+editor.fx.0.x=399
20
+editor.fx.0.y=-1001
21
+objectbench.height=93
22
+objectbench.width=721
20 23
 package.divider.horizontal=0.5993322203672788
21
-package.divider.vertical=0.8465608465608465
22
-package.editor.height=473
23
-package.editor.width=493
24
-package.editor.x=-188
25
-package.editor.y=-610
26
-package.frame.height=625
27
-package.frame.width=619
28
-package.numDependencies=4
24
+package.divider.vertical=0.8493975903614458
25
+package.editor.height=557
26
+package.editor.width=619
27
+package.editor.x=119
28
+package.editor.y=-750
29
+package.frame.height=722
30
+package.frame.width=745
31
+package.numDependencies=5
29 32
 package.numTargets=5
30 33
 package.showExtends=true
31 34
 package.showUses=true