Quellcode durchsuchen

Roy made changes

Roy vor 6 Jahren
Ursprung
Commit
0f22853990
4 geänderte Dateien mit 226 neuen und 21 gelöschten Zeilen
  1. 1
    0
      MainApplication.java
  2. 73
    21
      Operations.java
  3. 58
    0
      bonusFunctionsTest.java
  4. 94
    0
      scienticFunctionsTest.java

+ 1
- 0
MainApplication.java Datei anzeigen

@@ -7,6 +7,7 @@ public class MainApplication {
7 7
     public static void main(String[] args) {
8 8
 
9 9
         Console.println("Welcome to my calculator!");
10
+        Console.println("for HELP press ?");
10 11
         Operations.runCommand();
11 12
 
12 13
     }

+ 73
- 21
Operations.java Datei anzeigen

@@ -10,7 +10,16 @@ public class Operations
10 10
 {
11 11
     /**
12 12
      * prompting a user for a command solving for that command
13
+     * 
13 14
      */
15
+    public static void printHelp(){
16
+        System.out.println("\n? | + | - | * | / |");
17
+        System.out.println("^ | sqrt | exp | inverse |");
18
+        System.out.println("cos | sine | tan | cos-1 | sin-1 | m+ | m- | MRC");
19
+        System.out.println("log | ln | ex |");
20
+        System.out.println("to clear display enter clrDisplay\n");
21
+    }
22
+
14 23
     public static Double runCommand()
15 24
     {
16 25
         // put your code here
@@ -30,55 +39,98 @@ public class Operations
30 39
                 System.out.print("Thanks for calculating with us!");
31 40
                 break;
32 41
             }
33
-            if (command.equalsIgnoreCase("sine"))  {
34
-                result = ScientificFunctions.sine(Console.getDoubleInput("Enter number"));
42
+            else if (command.equalsIgnoreCase("sine"))  {
43
+                if (result != 0){
44
+                    result = ScientificFunctions.sine(result);
45
+                }
46
+                else{
47
+                    result = ScientificFunctions.sine(Console.getDoubleInput("Enter number"));
48
+                }
35 49
                 Console.printResult(result);
36 50
             }
37
-            if (command.equalsIgnoreCase("cos"))  {
38
-                result = ScientificFunctions.cosine(Console.getDoubleInput("Enter number"));
51
+            else if (command.equalsIgnoreCase("cos"))  {
52
+                if (result != 0){
53
+                    result = ScientificFunctions.cosine(result);
54
+                }
55
+                else{
56
+                    result = ScientificFunctions.cosine(Console.getDoubleInput("Enter number"));
57
+                }
39 58
                 Console.printResult(result);
40 59
             }
41
-            if (command.equalsIgnoreCase("tan"))  {
42
-                result = ScientificFunctions.tangent(Console.getDoubleInput("Enter number"));
60
+            else if (command.equalsIgnoreCase("tan"))  {
61
+                if (result != 0){
62
+                    result = ScientificFunctions.tangent(result);
63
+                }
64
+                else{
65
+                    result = ScientificFunctions.tangent(Console.getDoubleInput("Enter number"));
66
+                }
43 67
                 Console.printResult(result);
44 68
             }
45
-            if (command.equalsIgnoreCase("cos-1"))  {
46
-                result = ScientificFunctions.inverseCosine(Console.getDoubleInput("Enter number"));
69
+            else if (command.equalsIgnoreCase("cos-1"))  {
70
+                if (result != 0){
71
+                    result = ScientificFunctions.inverseCosine(result);
72
+                }
73
+                else{
74
+                    result = ScientificFunctions.inverseCosine(Console.getDoubleInput("Enter number"));
75
+                }
47 76
                 Console.printResult(result);
48 77
             }
49
-            if (command.equalsIgnoreCase("sin-1"))  {
50
-                result = ScientificFunctions.inverseSine(Console.getDoubleInput("Enter number"));
78
+            else if (command.equalsIgnoreCase("sin-1"))  {
79
+                if (result != 0){
80
+                    result = ScientificFunctions.inverseSine(result);
81
+                }
82
+                else{
83
+                    result = ScientificFunctions.inverseSine(Console.getDoubleInput("Enter number"));
84
+                }
51 85
                 Console.printResult(result);
52 86
             }
53
-            if (command.equalsIgnoreCase("tan-1"))  {
54
-                result = ScientificFunctions.inverseTangent(Console.getDoubleInput("Enter number"));
87
+            else if (command.equalsIgnoreCase("tan-1"))  {
88
+                if (result != 0){
89
+                    result = ScientificFunctions.inverseTangent(result);
90
+                }
91
+                else{
92
+                    result = ScientificFunctions.inverseTangent(Console.getDoubleInput("Enter number"));
93
+                }
55 94
                 Console.printResult(result);
56 95
             }
57
-            if(command.equalsIgnoreCase("m+")){
96
+            else if(command.equalsIgnoreCase("M+")){
58 97
                 memory.storeInMemory(result);
59 98
                 Console.printResult(result);
60 99
             }
61
-            
62
-            if (command.equalsIgnoreCase("m-")){
100
+
101
+            else if (command.equalsIgnoreCase("M-")){
102
+                Console.printResult(memory.clearMemory());
103
+            }
104
+
105
+            else if (command.equalsIgnoreCase("MRC")){
63 106
                 Console.printResult(memory.retrieveFromMemory());
64 107
             }
65
-            
66
-            if (command.equalsIgnoreCase("switchmode")){
108
+
109
+            else if (command.equalsIgnoreCase("switchmode")){
67 110
                 displayMode.setResult(result);
68 111
                 String mode = displayMode.enterMode();
69 112
                 //System.out.println(mode);
70 113
                 displayMode.switchDisplayMode(mode);
71
-                
114
+
72 115
             }
73
-            if (command.equalsIgnoreCase("log")){
116
+            else if (command.equalsIgnoreCase("log")){
74 117
                 Console.printResult(bonusFunctions.LogBaseTenOfAnumber(result));
75 118
             }
76
-            if (command.equalsIgnoreCase("ln")){
119
+            else if (command.equalsIgnoreCase("ln")){
77 120
                 Console.printResult(bonusFunctions.naturalLogLn(result));
78 121
             }
79
-            if (command.equalsIgnoreCase("ex")){
122
+            else if (command.equalsIgnoreCase("ex")){
80 123
                 Console.printResult(bonusFunctions.exponential(result));
81 124
             }
125
+            else if(command.equalsIgnoreCase("?")){
126
+                printHelp();
127
+            }
128
+            else if(command.equalsIgnoreCase("clrDisplay")){
129
+                result = 0.0;
130
+            }
131
+            else{
132
+                Console.println("Invalid command please press ? for help");
133
+            }
82 134
             //firstNumber = result;
83 135
             // and on and on
84 136
         }

+ 58
- 0
bonusFunctionsTest.java Datei anzeigen

@@ -0,0 +1,58 @@
1
+
2
+
3
+import org.junit.Assert;
4
+import org.junit.After;
5
+import org.junit.Before;
6
+import org.junit.Test;
7
+
8
+/**
9
+ * The test class bonusFunctionsTest.
10
+ *
11
+ * @author  (your name)
12
+ * @version (a version number or a date)
13
+ */
14
+public class bonusFunctionsTest
15
+{
16
+    private bonusFunctions bonusFunctions;
17
+    @Before
18
+    public void setUp(){
19
+        bonusFunctions = new bonusFunctions();
20
+    }
21
+
22
+    @Test
23
+    public void naturalLogLnTest(){
24
+        // :Given
25
+        Double expected = 0.0;
26
+
27
+        // :When
28
+        Double actual = bonusFunctions.naturalLogLn(1.0);
29
+
30
+        // :Then
31
+        Assert.assertEquals(expected, actual);
32
+    }
33
+    
34
+    @Test
35
+    public void LogBaseTenOfAnumberTest(){
36
+        // :Given
37
+        Double expected = 1.0;
38
+
39
+        // :When
40
+        Double actual = bonusFunctions.LogBaseTenOfAnumber(10.0);
41
+
42
+        // :Then
43
+        Assert.assertEquals(expected, actual);
44
+    }
45
+    
46
+    @Test
47
+    public void exponentialTest(){
48
+        // :Given
49
+        Double expected = 2.7182818285;
50
+
51
+        // :When
52
+        Double actual = bonusFunctions.exponential(1.0);
53
+
54
+        // :Then
55
+        Assert.assertEquals(expected, actual);
56
+    }
57
+    
58
+}

+ 94
- 0
scienticFunctionsTest.java Datei anzeigen

@@ -0,0 +1,94 @@
1
+
2
+
3
+import org.junit.Assert;
4
+import org.junit.After;
5
+import org.junit.Before;
6
+import org.junit.Test;
7
+
8
+/**
9
+ * The test class scienticFunctionsTest.
10
+ *
11
+ * @author  (your name)
12
+ * @version (a version number or a date)
13
+ */
14
+public class scienticFunctionsTest
15
+{
16
+    private scienticFunctions scientificFunctions;
17
+    
18
+    @Before
19
+    public void setUp(){
20
+        scientificFunctions = new scienticFunctions();
21
+    }
22
+
23
+    @Test
24
+    public void sineTest(){
25
+        // :Given
26
+        Double expected = 0.0;
27
+
28
+        // :When
29
+        Double actual = scientificFunctions.sine(0.0);
30
+
31
+        // :Then
32
+        Assert.assertEquals(expected, actual);
33
+    }
34
+    
35
+    @Test
36
+    public void cosineTest(){
37
+        // :Given
38
+        Double expected = 1.0;
39
+
40
+        // :When
41
+        Double actual = scientificFunctions.cosine(0);
42
+
43
+        // :Then
44
+        Assert.assertEquals(expected, actual);
45
+    }
46
+    
47
+    @Test
48
+    public void tangentTest(){
49
+        // :Given
50
+        Double expected = 0.0;
51
+
52
+        // :When
53
+        Double actual = scientificFunctions.tangent(0);
54
+
55
+        // :Then
56
+        Assert.assertEquals(expected, actual);
57
+    }
58
+    
59
+    @Test
60
+    public void inverseTangentTest(){
61
+        // :Given
62
+        Double expected = 45.0;
63
+
64
+        // :When
65
+        Double actual = scientificFunctions.inverseTangent(1);
66
+
67
+        // :Then
68
+        Assert.assertEquals(expected, actual);
69
+    }
70
+    
71
+    @Test
72
+    public void inverseCosineTest(){
73
+        // :Given
74
+        Double expected = 90.0;
75
+
76
+        // :When
77
+        Double actual = scientificFunctions.inverseCosine(0.0);
78
+
79
+        // :Then
80
+        Assert.assertEquals(expected, actual);
81
+    }
82
+    
83
+    @Test
84
+    public void inverseSineTest(){
85
+        // :Given
86
+        Double expected = 0.0;
87
+
88
+        // :When
89
+        Double actual = scientificFunctions.inverseSine(0.0);
90
+
91
+        // :Then
92
+        Assert.assertEquals(expected, actual);
93
+    }
94
+}