Yauheni Papko 6 years ago
parent
commit
a56adefff6
7 changed files with 286 additions and 106 deletions
  1. 26
    7
      Calculator.java
  2. 2
    1
      CoreTest.java
  3. 28
    15
      Scientific.java
  4. 76
    0
      ScientificDegrees.java
  5. 47
    0
      ScientificDegreesTest.java
  6. 15
    8
      ScientificTest.java
  7. 92
    75
      package.bluej

+ 26
- 7
Calculator.java View File

@@ -10,6 +10,7 @@ public class Calculator
10 10
 
11 11
         DisplayMode conversion = new DisplayMode();
12 12
         int modeOrdinal = 0;
13
+        int trigMode = 0; //0 - degrees (default); 1 - radians
13 14
 
14 15
         String initial = Console.getStringInput("Enter number");
15 16
         display.setDisplay(initial);
@@ -137,9 +138,27 @@ public class Calculator
137 138
                 Console.println(Double.toString(out));
138 139
                 display.setDisplay(Double.toString(out));
139 140
             }
141
+            if (command.equalsIgnoreCase("deg")) {
142
+                    trigMode = 0;
143
+                    Console.println("Trigonometric units are degrees");
144
+            }
145
+            if (command.equalsIgnoreCase("rad")) {
146
+                    trigMode = 1;
147
+                    Console.println("Trigonometric units are radians");
148
+            }
149
+            if (command.equalsIgnoreCase("trigSwitch")) {
150
+                if (trigMode == 0) {
151
+                    trigMode = 1;
152
+                    Console.println("Trigonometric units are radians");
153
+                } else  
154
+                {
155
+                    trigMode = 0;
156
+                    Console.println("Trigonometric units are degrees");
157
+                }
158
+            }
140 159
             if (command.equalsIgnoreCase("sin")) {
141 160
                 double first = Double.parseDouble(display.getDisplay());
142
-                double out = Scientific.sine(first);
161
+                double out = Scientific.sine(first, trigMode);
143 162
                 if (Double.toString(out) == "NaN") {
144 163
                     display.errorDisplay();
145 164
                 } else {
@@ -149,7 +168,7 @@ public class Calculator
149 168
             }
150 169
             if (command.equalsIgnoreCase("cos")) {
151 170
                 double first = Double.parseDouble(display.getDisplay());
152
-                double out = Scientific.cosine(first);
171
+                double out = Scientific.cosine(first, trigMode);
153 172
                 if (Double.toString(out) == "NaN") {
154 173
                     display.errorDisplay();
155 174
                 } else {
@@ -159,7 +178,7 @@ public class Calculator
159 178
             }
160 179
             if (command.equalsIgnoreCase("tan")) {
161 180
                 double first = Double.parseDouble(display.getDisplay());
162
-                double out = Scientific.tangent(first);
181
+                double out = Scientific.tangent(first, trigMode);
163 182
                 if (Double.toString(out) == "NaN") {
164 183
                     display.errorDisplay();
165 184
                 } else {
@@ -169,7 +188,7 @@ public class Calculator
169 188
             }
170 189
             if (command.equalsIgnoreCase("asin")) {
171 190
                 double first = Double.parseDouble(display.getDisplay());
172
-                double out = Scientific.inverseSin(first);
191
+                double out = Scientific.inverseSin(first, trigMode);
173 192
                 if (Double.toString(out) == "NaN") {
174 193
                     display.errorDisplay();
175 194
                 } else {
@@ -179,7 +198,7 @@ public class Calculator
179 198
             }
180 199
             if (command.equalsIgnoreCase("acos")) {
181 200
                 double first = Double.parseDouble(display.getDisplay());
182
-                double out = Scientific.inverseCosine(first);
201
+                double out = Scientific.inverseCosine(first, trigMode);
183 202
                 if (Double.toString(out) == "NaN") {
184 203
                     display.errorDisplay();
185 204
                 } else {
@@ -189,7 +208,7 @@ public class Calculator
189 208
             }
190 209
             if (command.equalsIgnoreCase("atan")) {
191 210
                 double first = Double.parseDouble(display.getDisplay());
192
-                double out = Scientific.inverseTangent(first);
211
+                double out = Scientific.inverseTangent(first, trigMode);
193 212
                 if (Double.toString(out) == "NaN") {
194 213
                     display.errorDisplay();
195 214
                 } else {
@@ -197,7 +216,7 @@ public class Calculator
197 216
                     display.setDisplay(Double.toString(out));
198 217
                 }
199 218
             }
200
-            if (command.equalsIgnoreCase("fact") || command.equalsIgnoreCase("!x")) {
219
+            if (command.equalsIgnoreCase("fact") || command.equalsIgnoreCase("x!")) {
201 220
                 double first = Custom.round(Double.parseDouble(display.getDisplay()));
202 221
                 BigInteger out = Scientific.factorial(first);
203 222
                 Console.println(out.toString());

+ 2
- 1
CoreTest.java View File

@@ -42,7 +42,8 @@ public class CoreTest
42 42
     }
43 43
 
44 44
     @Test
45
-    public void testAdd(){
45
+    public void testAdd()
46
+    {
46 47
         //expected
47 48
         double expected = 2.0;
48 49
         //actual

+ 28
- 15
Scientific.java View File

@@ -14,41 +14,54 @@ public class Scientific
14 14
         x = 0;
15 15
     }
16 16
 
17
-    public static double sine(double angle)
17
+    public static double sine(double angle, int mode)
18 18
     {
19
-        angle = Math.toRadians(angle);
19
+        if (mode == 0) {
20
+            angle = Math.toRadians(angle);
21
+        }
20 22
         return Math.sin(angle) ;
21 23
     }
22 24
 
23
-    public static double cosine(double angle)
25
+    public static double cosine(double angle, int mode)
24 26
     {
25
-        angle = Math.toRadians(angle);
27
+        if (mode == 0) {
28
+            angle = Math.toRadians(angle);
29
+        }
26 30
         return Math.cos(angle) ;
27 31
     }
28 32
 
29
-    public static double tangent(double angle)
33
+    public static double tangent(double angle, int mode)
30 34
     {
31
-        angle = Math.toRadians(angle);
35
+        if (mode == 0) {
36
+            angle = Math.toRadians(angle);
37
+        }
32 38
         return Math.tan(angle) ;
33 39
     }
34 40
 
35
-    public static double inverseSin(double angle)
36
-    {   angle = Math.toRadians(angle);
41
+    public static double inverseSin(double angle, int mode)
42
+    {   
43
+        if (mode == 0) {
44
+            angle = Math.toRadians(angle);
45
+        }
37 46
         return Math.asin(angle) ;
38
-
39 47
     }
40 48
 
41
-    public static double inverseCosine(double angle)
49
+    public static double inverseCosine(double angle, int mode)
42 50
     {
43
-        angle = Math.toRadians(angle);
51
+        if (mode == 0) {
52
+            angle = Math.toRadians(angle);
53
+        }
44 54
         return Math.acos(angle) ;
45 55
     }
46 56
 
47
-    public static double inverseTangent(double angle)
57
+    public static double inverseTangent(double angle, int mode)
48 58
     {
49
-        angle = Math.toRadians(angle);
59
+        if (mode == 0) {
60
+            angle = Math.toRadians(angle);
61
+        }
50 62
         return Math.atan(angle) ;
51 63
     }
64
+
52 65
     public static BigInteger factorial(double x)
53 66
     {
54 67
         int y = (int) x;
@@ -57,7 +70,7 @@ public class Scientific
57 70
             factorialValue = factorialValue.multiply(BigInteger.valueOf(i));
58 71
         }
59 72
         return factorialValue;
60
-        }
61
-
62 73
     }
63 74
 
75
+}
76
+

+ 76
- 0
ScientificDegrees.java View File

@@ -0,0 +1,76 @@
1
+
2
+/**
3
+ * Write a description of class ScientificDegrees here.
4
+ *
5
+ * @author (your name)
6
+ * @version (a version number or a date)
7
+ */
8
+public class ScientificDegrees
9
+{
10
+    // instance variables - replace the example below with your own
11
+    private int x;
12
+
13
+    /**
14
+     * Constructor for objects of class ScientificDegrees
15
+     */
16
+    public ScientificDegrees()
17
+    {
18
+        // initialise instance variables
19
+        x = 0;
20
+    }
21
+
22
+    public static double sine(double angle)
23
+    {
24
+        
25
+
26
+        return Math.sin(angle) ;
27
+    }
28
+
29
+    public static double cosine(double angle)
30
+    {
31
+        
32
+
33
+        return Math.cos(angle) ;
34
+    }
35
+
36
+    public static double tangent(double angle)
37
+    {
38
+        
39
+
40
+        return Math.tan(angle) ;
41
+    }
42
+
43
+    public static double inverseSin(double angle)
44
+    {   
45
+
46
+        return Math.asin(angle) ;
47
+
48
+    }
49
+
50
+    public static double inverseCosine(double angle)
51
+    {
52
+        
53
+
54
+        return Math.acos(angle) ;
55
+    }
56
+
57
+    public static double inverseTangent(double angle)
58
+    {
59
+        
60
+
61
+        return Math.atan(angle) ;
62
+    }
63
+
64
+    /*public static BigInteger factorial(double number) {
65
+        double result = double.ValueOf(1);
66
+
67
+        for (double factor = 2; factor <= number.longValue(); factor++) {
68
+            result = result.multiply(double.valueOf(factor));
69
+        }
70
+
71
+        return result;
72
+    }*/
73
+}
74
+
75
+
76
+

+ 47
- 0
ScientificDegreesTest.java View File

@@ -0,0 +1,47 @@
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
+ * The test class ScientificDegreesTest.
9
+ *
10
+ * @author  (your name)
11
+ * @version (a version number or a date)
12
+ */
13
+public class ScientificDegreesTest
14
+{
15
+    private Scientific test;
16
+    public ScientificDegreesTest(){
17
+    }
18
+
19
+    /**
20
+     * Sets up the test fixture.
21
+     *
22
+     * Called before every test case method.
23
+     */
24
+    @Before
25
+    public void setUp()
26
+    {
27
+        Scientific test = new Scientific();
28
+    }
29
+
30
+    /**
31
+     * Tears down the test fixture.
32
+     *
33
+     * Called after every test case method.
34
+     */
35
+    @Test
36
+    public void testSine()
37
+    {
38
+        //expected
39
+        double expected = 1.0;
40
+        //actual
41
+        
42
+        double actual = test.sine(90.0,0);
43
+        //assertion
44
+        assertEquals(expected, actual, 0.1);
45
+    }
46
+    }
47
+

+ 15
- 8
ScientificTest.java View File

@@ -13,11 +13,8 @@ import org.junit.Test;
13 13
  */
14 14
 public class ScientificTest
15 15
 {
16
-    /**
17
-     * Default constructor for test class ScientificTest
18
-     */
19
-    public ScientificTest()
20
-    {
16
+    private Scientific test;
17
+    public ScientificTest(){
21 18
     }
22 19
 
23 20
     /**
@@ -28,6 +25,7 @@ public class ScientificTest
28 25
     @Before
29 26
     public void setUp()
30 27
     {
28
+        Scientific test = new Scientific();
31 29
     }
32 30
 
33 31
     /**
@@ -35,8 +33,17 @@ public class ScientificTest
35 33
      *
36 34
      * Called after every test case method.
37 35
      */
38
-    @After
39
-    public void tearDown()
36
+    @Test
37
+    public void testSine()
40 38
     {
39
+        //expected
40
+        double expected = 0.8;
41
+        //actual
42
+        
43
+        double actual = Math.toRadians(90);
44
+        double actualSine = test.sine(actual);
45
+        //assertion
46
+        assertEquals(expected, actualSine, 0.1);
41 47
     }
42
-}
48
+    }
49
+

+ 92
- 75
package.bluej View File

@@ -8,14 +8,17 @@ dependency10.type=UsesDependency
8 8
 dependency11.from=Calculator
9 9
 dependency11.to=Scientific
10 10
 dependency11.type=UsesDependency
11
+dependency12.from=ScientificDegreesTest
12
+dependency12.to=Scientific
13
+dependency12.type=UsesDependency
11 14
 dependency2.from=MainApplication
12 15
 dependency2.to=Calculator
13 16
 dependency2.type=UsesDependency
14
-dependency3.from=CoreTest
15
-dependency3.to=Core
17
+dependency3.from=Display
18
+dependency3.to=Console
16 19
 dependency3.type=UsesDependency
17
-dependency4.from=Display
18
-dependency4.to=Console
20
+dependency4.from=CoreTest
21
+dependency4.to=Core
19 22
 dependency4.type=UsesDependency
20 23
 dependency5.from=Calculator
21 24
 dependency5.to=Display
@@ -34,20 +37,20 @@ dependency9.to=Core
34 37
 dependency9.type=UsesDependency
35 38
 editor.fx.0.height=722
36 39
 editor.fx.0.width=1223
37
-editor.fx.0.x=142
38
-editor.fx.0.y=110
40
+editor.fx.0.x=170
41
+editor.fx.0.y=107
39 42
 objectbench.height=101
40 43
 objectbench.width=677
41 44
 package.divider.horizontal=0.6
42 45
 package.divider.vertical=0.8459343794579173
43 46
 package.editor.height=586
44 47
 package.editor.width=1034
45
-package.editor.x=64
46
-package.editor.y=87
48
+package.editor.x=51
49
+package.editor.y=70
47 50
 package.frame.height=759
48 51
 package.frame.width=1160
49
-package.numDependencies=11
50
-package.numTargets=15
52
+package.numDependencies=12
53
+package.numTargets=17
51 54
 package.showExtends=true
52 55
 package.showUses=true
53 56
 project.charset=UTF-8
@@ -57,113 +60,127 @@ readme.width=47
57 60
 readme.x=10
58 61
 readme.y=10
59 62
 target1.height=50
60
-target1.name=ScientificTest
63
+target1.name=ScientificDegrees
61 64
 target1.showInterface=false
62
-target1.type=UnitTestTargetJunit4
63
-target1.width=80
64
-target1.x=740
65
-target1.y=140
66
-target10.association=ConversionTest
65
+target1.type=ClassTarget
66
+target1.width=140
67
+target1.x=780
68
+target1.y=270
67 69
 target10.height=50
68
-target10.name=DisplayMode
70
+target10.name=CoreTest
69 71
 target10.showInterface=false
70
-target10.type=ClassTarget
71
-target10.width=110
72
-target10.x=80
73
-target10.y=300
72
+target10.type=UnitTestTargetJunit4
73
+target10.width=80
74
+target10.x=740
75
+target10.y=20
74 76
 target11.height=50
75
-target11.name=Calculator
77
+target11.name=MemoryTest
76 78
 target11.showInterface=false
77
-target11.type=ClassTarget
78
-target11.width=90
79
-target11.x=440
80
-target11.y=170
79
+target11.type=UnitTestTargetJunit4
80
+target11.width=80
81
+target11.x=140
82
+target11.y=230
83
+target12.association=ConversionTest
81 84
 target12.height=50
82
-target12.name=Console
85
+target12.name=DisplayMode
83 86
 target12.showInterface=false
84 87
 target12.type=ClassTarget
85
-target12.width=80
86
-target12.x=380
87
-target12.y=440
88
+target12.width=110
89
+target12.x=110
90
+target12.y=380
88 91
 target13.height=50
89
-target13.name=MainApplication
92
+target13.name=Calculator
90 93
 target13.showInterface=false
91 94
 target13.type=ClassTarget
92
-target13.width=120
93
-target13.x=290
94
-target13.y=100
95
-target14.association=CoreTest
95
+target13.width=90
96
+target13.x=440
97
+target13.y=170
96 98
 target14.height=50
97
-target14.name=Core
99
+target14.name=Console
98 100
 target14.showInterface=false
99 101
 target14.type=ClassTarget
100 102
 target14.width=80
101
-target14.x=710
102
-target14.y=50
103
-target15.association=DisplayTest
103
+target14.x=380
104
+target14.y=460
104 105
 target15.height=50
105
-target15.name=Display
106
+target15.name=MainApplication
106 107
 target15.showInterface=false
107 108
 target15.type=ClassTarget
108
-target15.width=80
109
-target15.x=80
109
+target15.width=120
110
+target15.x=300
110 111
 target15.y=50
111
-target2.association=MemoryTest
112
+target16.association=CoreTest
113
+target16.height=50
114
+target16.name=Core
115
+target16.showInterface=false
116
+target16.type=ClassTarget
117
+target16.width=80
118
+target16.x=710
119
+target16.y=50
120
+target17.association=DisplayTest
121
+target17.height=50
122
+target17.name=Display
123
+target17.showInterface=false
124
+target17.type=ClassTarget
125
+target17.width=80
126
+target17.x=30
127
+target17.y=120
112 128
 target2.height=50
113
-target2.name=Memory
129
+target2.name=ScientificTest
114 130
 target2.showInterface=false
115
-target2.type=ClassTarget
131
+target2.type=UnitTestTargetJunit4
116 132
 target2.width=80
117
-target2.x=80
118
-target2.y=170
119
-target3.association=ScientificTest
133
+target2.x=740
134
+target2.y=140
120 135
 target3.height=50
121
-target3.name=Scientific
136
+target3.name=ScientificDegreesTest
122 137
 target3.showInterface=false
123
-target3.type=ClassTarget
124
-target3.width=80
125
-target3.x=710
126
-target3.y=170
138
+target3.type=UnitTestTargetJunit4
139
+target3.width=160
140
+target3.x=800
141
+target3.y=240
142
+target4.association=MemoryTest
127 143
 target4.height=50
128
-target4.name=CustomTest
144
+target4.name=Memory
129 145
 target4.showInterface=false
130
-target4.type=UnitTestTargetJunit4
146
+target4.type=ClassTarget
131 147
 target4.width=80
132
-target4.x=740
133
-target4.y=250
134
-target5.association=CustomTest
148
+target4.x=110
149
+target4.y=260
150
+target5.association=ScientificTest
135 151
 target5.height=50
136
-target5.name=Custom
152
+target5.name=Scientific
137 153
 target5.showInterface=false
138 154
 target5.type=ClassTarget
139 155
 target5.width=80
140 156
 target5.x=710
141
-target5.y=280
157
+target5.y=170
142 158
 target6.height=50
143
-target6.name=ConversionTest
159
+target6.name=CustomTest
144 160
 target6.showInterface=false
145 161
 target6.type=UnitTestTargetJunit4
146
-target6.width=110
147
-target6.x=110
148
-target6.y=270
162
+target6.width=80
163
+target6.x=740
164
+target6.y=350
165
+target7.association=CustomTest
149 166
 target7.height=50
150
-target7.name=DisplayTest
167
+target7.name=Custom
151 168
 target7.showInterface=false
152
-target7.type=UnitTestTargetJunit4
169
+target7.type=ClassTarget
153 170
 target7.width=80
154
-target7.x=110
155
-target7.y=20
171
+target7.x=710
172
+target7.y=380
156 173
 target8.height=50
157
-target8.name=CoreTest
174
+target8.name=ConversionTest
158 175
 target8.showInterface=false
159 176
 target8.type=UnitTestTargetJunit4
160
-target8.width=80
161
-target8.x=740
162
-target8.y=20
177
+target8.width=110
178
+target8.x=140
179
+target8.y=350
163 180
 target9.height=50
164
-target9.name=MemoryTest
181
+target9.name=DisplayTest
165 182
 target9.showInterface=false
166 183
 target9.type=UnitTestTargetJunit4
167 184
 target9.width=80
168
-target9.x=110
169
-target9.y=140
185
+target9.x=60
186
+target9.y=90