Sfoglia il codice sorgente

the calculator lab final changes

Roy 6 anni fa
parent
commit
911bce7442
7 ha cambiato i file con 288 aggiunte e 125 eliminazioni
  1. 3
    3
      Console.java
  2. 84
    3
      Operations.java
  3. 3
    3
      bonusFunctions.java
  4. 1
    1
      bonusFunctionsTest.java
  5. 49
    6
      calculatorCoreFunctions.java
  6. 97
    0
      calculatorCoreFunctionsTest.java
  7. 51
    109
      package.bluej

+ 3
- 3
Console.java Vedi File

@@ -18,7 +18,7 @@ public class Console extends DiplayMode {
18 18
         print(output + "\n", args);
19 19
     }
20 20
 
21
-    public static void printResult(Double result) {
21
+    public static void printResult(double result) {
22 22
         System.out.println("Result = " + result);
23 23
     }
24 24
 
@@ -36,10 +36,10 @@ public class Console extends DiplayMode {
36 36
         return firstNumber;
37 37
     }
38 38
 
39
-    public static Double getDoubleInput(String prompt) {
39
+    public static double getDoubleInput(String prompt) {
40 40
         Scanner scanner = new Scanner(System.in);
41 41
         println(prompt);
42
-        Double userInput = scanner.nextDouble();
42
+        double userInput = scanner.nextDouble();
43 43
         return userInput;
44 44
     }
45 45
 

+ 84
- 3
Operations.java Vedi File

@@ -14,23 +14,25 @@ public class Operations
14 14
      */
15 15
     public static void printHelp(){
16 16
         System.out.println("\n? | + | - | * | / |");
17
-        System.out.println("^ | sqrt | exp | inverse |");
17
+        System.out.println("^ | sqrt | inverse |");
18 18
         System.out.println("cos | sine | tan | cos-1 | sin-1 | m+ | m- | MRC");
19 19
         System.out.println("log | ln | ex |");
20
+        System.out.println("To exit the program enter exit");
20 21
         System.out.println("to clear display enter clrDisplay\n");
21 22
     }
22 23
 
23
-    public static Double runCommand()
24
+    public static double runCommand()
24 25
     {
25 26
         // put your code here
26 27
 
27 28
         Scanner input = new Scanner(System.in);
28
-        Double result = 0.0;
29
+        double result = 0.0;
29 30
         //String userInput = scanner.nextLine();
30 31
         scienticFunctions ScientificFunctions = new scienticFunctions();
31 32
         Memory memory = new Memory();
32 33
         DiplayMode displayMode = new DiplayMode();
33 34
         bonusFunctions bonusFunctions = new bonusFunctions();
35
+        calculatorCoreFunctions calculatorCoreFunctions = new calculatorCoreFunctions();
34 36
         // Calculator calc = new ....
35 37
         while (true) {
36 38
             System.out.print("Enter a command: ");
@@ -39,6 +41,85 @@ public class Operations
39 41
                 System.out.print("Thanks for calculating with us!");
40 42
                 break;
41 43
             }
44
+            else if (command.equalsIgnoreCase("+")){
45
+                if (result != 0){
46
+                    double numInput = Console.getDoubleInput("Enter number");
47
+                    result = calculatorCoreFunctions.add(result, numInput);
48
+                }
49
+                else{
50
+                    double firstNumber = Console.getDoubleInput("Enter first number");
51
+                    double secondNumber = Console.getDoubleInput("Enter second number");
52
+                    result = calculatorCoreFunctions.add(firstNumber, secondNumber);
53
+                }
54
+                Console.printResult(result);
55
+                
56
+            }
57
+            else if (command.equalsIgnoreCase("-")){
58
+                if (result != 0){
59
+                    double numInput = Console.getDoubleInput("Enter number");
60
+                    result = calculatorCoreFunctions.subtract(result, numInput);
61
+                }
62
+                else{
63
+                    double firstNumber = Console.getDoubleInput("Enter first number");
64
+                    double secondNumber = Console.getDoubleInput("Enter second number");
65
+                    result = calculatorCoreFunctions.subtract(firstNumber, secondNumber);;
66
+                }
67
+                Console.printResult(result);
68
+            }
69
+            else if (command.equalsIgnoreCase("*")){
70
+                if (result != 0){
71
+                    double numInput = Console.getDoubleInput("Enter number");
72
+                    result = calculatorCoreFunctions.multiply(result, numInput);
73
+                }
74
+                else{
75
+                    double firstNumber = Console.getDoubleInput("Enter first number");
76
+                    double secondNumber = Console.getDoubleInput("Enter second number");
77
+                    result = calculatorCoreFunctions.multiply(firstNumber, secondNumber);;
78
+                }
79
+                Console.printResult(result);
80
+            }
81
+            else if (command.equalsIgnoreCase("/")){
82
+                if (result != 0){
83
+                    double numInput = Console.getDoubleInput("Enter number");
84
+                    result = calculatorCoreFunctions.divide(result, numInput);
85
+                }
86
+                else{
87
+                    double firstNumber = Console.getDoubleInput("Enter first number");
88
+                    double secondNumber = Console.getDoubleInput("Enter second number");
89
+                    result = calculatorCoreFunctions.divide(firstNumber, secondNumber);;
90
+                }
91
+                Console.printResult(result);
92
+            }
93
+            else if (command.equalsIgnoreCase("^")){
94
+                if (result != 0){
95
+                    double numInput = Console.getDoubleInput("Enter number");
96
+                    result = calculatorCoreFunctions.exponent(result, numInput);
97
+                }
98
+                else{
99
+                    double firstNumber = Console.getDoubleInput("Enter first number");
100
+                    double secondNumber = Console.getDoubleInput("Enter second number");
101
+                    result = calculatorCoreFunctions.exponent(firstNumber, secondNumber);;
102
+                }
103
+                Console.printResult(result);
104
+            }
105
+            else if (command.equalsIgnoreCase("sqrt"))  {
106
+                if (result != 0){
107
+                    result = calculatorCoreFunctions.sqrt(result);
108
+                }
109
+                else{
110
+                    result = calculatorCoreFunctions.sqrt(Console.getDoubleInput("Enter number"));
111
+                }
112
+                Console.printResult(result);
113
+            }
114
+            else if (command.equalsIgnoreCase("inverse"))  {
115
+                if (result != 0){
116
+                    result = calculatorCoreFunctions.inverse(result);
117
+                }
118
+                else{
119
+                    result = calculatorCoreFunctions.inverse(Console.getDoubleInput("Enter number"));
120
+                }
121
+                Console.printResult(result);
122
+            }
42 123
             else if (command.equalsIgnoreCase("sine"))  {
43 124
                 if (result != 0){
44 125
                     result = ScientificFunctions.sine(result);

+ 3
- 3
bonusFunctions.java Vedi File

@@ -13,19 +13,19 @@ public class bonusFunctions
13 13
      * @param  y  a sample parameter for a method
14 14
      * @return    the sum of x and y
15 15
      */
16
-    public Double naturalLogLn(Double x)
16
+    public double naturalLogLn(double x)
17 17
     {
18 18
         // put your code here
19 19
         return Math.log(x);
20 20
     }
21 21
     
22
-    public Double LogBaseTenOfAnumber(Double x)
22
+    public double LogBaseTenOfAnumber(double x)
23 23
     {
24 24
         // put your code here
25 25
         return Math.log10(x);
26 26
     }
27 27
     
28
-    public Double exponential(Double x)
28
+    public double exponential(double x)
29 29
     {
30 30
         // put your code here
31 31
         return Math.exp(x);

+ 1
- 1
bonusFunctionsTest.java Vedi File

@@ -52,7 +52,7 @@ public class bonusFunctionsTest
52 52
         Double actual = bonusFunctions.exponential(1.0);
53 53
 
54 54
         // :Then
55
-        Assert.assertEquals(expected, actual);
55
+        Assert.assertEquals(expected, actual, 0.10);
56 56
     }
57 57
     
58 58
 }

+ 49
- 6
calculatorCoreFunctions.java Vedi File

@@ -5,20 +5,24 @@
5 5
  * @author (your name)
6 6
  * @version (a version number or a date)
7 7
  */
8
+import java.lang.*;
9
+import java.util.*;
8 10
 public class calculatorCoreFunctions
9 11
 {
10 12
     // instance variables - replace the example below with your own
11
-    private int x;
12
-    
13 13
 
14 14
     /**
15
-     * Constructor for objects of class calculatorCoreFunctions
15
+     * Write a description of class calculatorCoreFunctions here.
16
+     *
17
+     * @author (your name)
18
+     * @version (a version number or a date)
16 19
      */
20
+
17 21
     public calculatorCoreFunctions()
18 22
     {
19 23
         // initialise instance variables
20
-        x = 5;
21
-        //y= 0;
24
+        double x = 0;
25
+        double y = 0;
22 26
     }
23 27
 
24 28
     /**
@@ -27,9 +31,48 @@ public class calculatorCoreFunctions
27 31
      * @param  y  a sample parameter for a method
28 32
      * @return    the sum of x and y
29 33
      */
30
-    public int add(int y)
34
+    public double add(double x, double y)
31 35
     {
32 36
         // put your code here
33 37
         return x + y;
34 38
     }
39
+
40
+    public double subtract(double x, double y)
41
+    {
42
+        return x - y;
43
+    }
44
+
45
+    public double multiply(double x, double y)
46
+    {
47
+        return x * y;
48
+    }
49
+
50
+    public double divide(double x, double y)
51
+    {
52
+        if (y == 0)
53
+        {
54
+            System.out.println("Invalid input");
55
+            return 0;
56
+        }
57
+        return x / y;
58
+    }
59
+
60
+    public double sqrt(double x)
61
+    {
62
+        return Math.sqrt(x);
63
+    }
64
+
65
+    public double exponent(double x, double y)
66
+    {
67
+        return Math.pow(x, y);
68
+    }
69
+
70
+    public double inverse(double x)
71
+    {
72
+        if (x ==0){
73
+            return 0;
74
+        }
75
+        return 1 / x;
76
+    }
35 77
 }
78
+

+ 97
- 0
calculatorCoreFunctionsTest.java Vedi File

@@ -0,0 +1,97 @@
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 calculatorCoreFunctionsTest.
10
+ *
11
+ * @author  (your name)
12
+ * @version (a version number or a date)
13
+ */
14
+import static org.junit.Assert.*;
15
+import org.junit.After;
16
+import org.junit.Before;
17
+import org.junit.Test;
18
+
19
+/**
20
+* The test class calculatorCoreFunctionsTest.
21
+*
22
+* @author  (your name)
23
+* @version (a version number or a date)
24
+*/
25
+public class calculatorCoreFunctionsTest
26
+{
27
+   public calculatorCoreFunctions coreFunctions;
28
+   
29
+ 
30
+   @Before
31
+   public void setUp()
32
+   {
33
+     coreFunctions = new calculatorCoreFunctions();
34
+   }
35
+   
36
+   @After
37
+   public void tearDown()
38
+   {
39
+   }
40
+   
41
+   @Test
42
+   public void addTest()
43
+   {
44
+       double expected = 9.0;
45
+       double actual = coreFunctions.add(8,1);
46
+       assertEquals(expected, actual, 0.1);
47
+     
48
+   }
49
+
50
+   @Test
51
+   public void subtractTest()
52
+   {
53
+       double expected = 5.0;
54
+       double actual = coreFunctions.subtract(7,2);
55
+       assertEquals(expected, actual, 0.1);
56
+   }
57
+   
58
+   @Test
59
+   public void multiplyTest()
60
+   {
61
+       double expected = 12.0;
62
+       double actual = coreFunctions.multiply(3,4);
63
+       assertEquals(expected, actual, 0.1);
64
+   }
65
+   
66
+   @Test
67
+   public void divideTest()
68
+   {
69
+       double expected = 7.0;
70
+       double actual = coreFunctions.divide(21,3);
71
+       assertEquals(expected, actual, 0.1);
72
+   }
73
+   
74
+   @Test
75
+   public void expTest()
76
+   {
77
+       double expected = 4.0;
78
+       double actual = coreFunctions.exponent(2,2);
79
+       assertEquals(expected, actual, 0.1);
80
+   }
81
+   
82
+   @Test
83
+   public void sqrtTest()
84
+   {
85
+       double expected = 5.0;
86
+       double actual = coreFunctions.sqrt(25.0);
87
+       assertEquals(expected, actual, 0.1);
88
+   }
89
+   
90
+   @Test
91
+   public void invertTest()
92
+   {
93
+       double expected = 0.2;
94
+       double actual = coreFunctions.inverse(5.0);
95
+       assertEquals(expected, actual, 0.1);
96
+   }
97
+}

+ 51
- 109
package.bluej Vedi File

@@ -2,45 +2,36 @@
2 2
 dependency1.from=bonusFunctionsTest
3 3
 dependency1.to=bonusFunctions
4 4
 dependency1.type=UsesDependency
5
-dependency10.from=Operations
6
-dependency10.to=Console
5
+dependency10.from=MainApplication
6
+dependency10.to=Operations
7 7
 dependency10.type=UsesDependency
8
-dependency11.from=ConsoleTest
9
-dependency11.to=Console
10
-dependency11.type=UsesDependency
11
-dependency12.from=MainApplication
12
-dependency12.to=Console
13
-dependency12.type=UsesDependency
14
-dependency13.from=MainApplication
15
-dependency13.to=Operations
16
-dependency13.type=UsesDependency
17 8
 dependency2.from=scienticFunctionsTest
18 9
 dependency2.to=scienticFunctions
19 10
 dependency2.type=UsesDependency
20 11
 dependency3.from=DiplayMode
21 12
 dependency3.to=Console
22 13
 dependency3.type=UsesDependency
23
-dependency4.from=Calculator
24
-dependency4.to=CoreFunctions
14
+dependency4.from=Operations
15
+dependency4.to=scienticFunctions
25 16
 dependency4.type=UsesDependency
26
-dependency5.from=Calculator
27
-dependency5.to=Operations
17
+dependency5.from=Operations
18
+dependency5.to=Memory
28 19
 dependency5.type=UsesDependency
29 20
 dependency6.from=Operations
30
-dependency6.to=scienticFunctions
21
+dependency6.to=DiplayMode
31 22
 dependency6.type=UsesDependency
32 23
 dependency7.from=Operations
33
-dependency7.to=Memory
24
+dependency7.to=bonusFunctions
34 25
 dependency7.type=UsesDependency
35 26
 dependency8.from=Operations
36
-dependency8.to=DiplayMode
27
+dependency8.to=Console
37 28
 dependency8.type=UsesDependency
38
-dependency9.from=Operations
39
-dependency9.to=bonusFunctions
29
+dependency9.from=MainApplication
30
+dependency9.to=Console
40 31
 dependency9.type=UsesDependency
41 32
 editor.fx.0.height=717
42 33
 editor.fx.0.width=800
43
-editor.fx.0.x=371
34
+editor.fx.0.x=480
44 35
 editor.fx.0.y=23
45 36
 objectbench.height=200
46 37
 objectbench.width=552
@@ -48,12 +39,12 @@ package.divider.horizontal=0.6004296455424275
48 39
 package.divider.vertical=0.6854103343465046
49 40
 package.editor.height=444
50 41
 package.editor.width=825
51
-package.editor.x=35
52
-package.editor.y=23
42
+package.editor.x=105
43
+package.editor.y=32
53 44
 package.frame.height=716
54 45
 package.frame.width=951
55
-package.numDependencies=13
56
-package.numTargets=17
46
+package.numDependencies=10
47
+package.numTargets=10
57 48
 package.showExtends=true
58 49
 package.showUses=true
59 50
 project.charset=UTF-8
@@ -63,98 +54,48 @@ readme.width=47
63 54
 readme.x=10
64 55
 readme.y=10
65 56
 target1.height=50
66
-target1.name=CoreFunctionsCo
57
+target1.name=DiplayMode
67 58
 target1.showInterface=false
68 59
 target1.type=ClassTarget
69
-target1.width=130
70
-target1.x=10
71
-target1.y=120
60
+target1.width=100
61
+target1.x=50
62
+target1.y=360
72 63
 target10.height=50
73
-target10.name=CoreFunctionsTest
64
+target10.name=MainApplication
74 65
 target10.showInterface=false
75 66
 target10.type=ClassTarget
76
-target10.width=140
77
-target10.x=10
78
-target10.y=240
79
-target11.height=50
80
-target11.name=Calculator
81
-target11.showInterface=false
82
-target11.type=ClassTarget
83
-target11.width=90
84
-target11.x=160
85
-target11.y=260
86
-target12.height=50
87
-target12.name=Console
88
-target12.showInterface=false
89
-target12.type=ClassTarget
90
-target12.width=80
91
-target12.x=240
92
-target12.y=200
93
-target13.association=bonusFunctionsTest
94
-target13.height=50
95
-target13.name=bonusFunctions
96
-target13.showInterface=false
97
-target13.type=ClassTarget
98
-target13.width=120
99
-target13.x=500
100
-target13.y=330
101
-target14.height=50
102
-target14.name=Operations
103
-target14.showInterface=false
104
-target14.type=ClassTarget
105
-target14.width=90
106
-target14.x=380
107
-target14.y=370
108
-target15.height=50
109
-target15.name=ConsoleTest
110
-target15.showInterface=false
111
-target15.type=ClassTarget
112
-target15.width=100
113
-target15.x=10
114
-target15.y=300
115
-target16.height=50
116
-target16.name=MainApplication
117
-target16.showInterface=false
118
-target16.type=ClassTarget
119
-target16.width=120
120
-target16.x=140
121
-target16.y=60
122
-target17.height=50
123
-target17.name=Display
124
-target17.showInterface=false
125
-target17.type=ClassTarget
126
-target17.width=80
127
-target17.x=160
128
-target17.y=320
67
+target10.width=120
68
+target10.x=140
69
+target10.y=60
129 70
 target2.height=50
130
-target2.name=CoreFunctionsCoTest
71
+target2.name=Memory
131 72
 target2.showInterface=false
132 73
 target2.type=ClassTarget
133
-target2.width=160
134
-target2.x=10
135
-target2.y=180
74
+target2.width=80
75
+target2.x=640
76
+target2.y=370
136 77
 target3.height=50
137
-target3.name=Memory
78
+target3.name=bonusFunctionsTest
138 79
 target3.showInterface=false
139
-target3.type=ClassTarget
140
-target3.width=80
141
-target3.x=640
142
-target3.y=370
80
+target3.type=UnitTestTargetJunit4
81
+target3.width=120
82
+target3.x=530
83
+target3.y=300
84
+target4.association=scienticFunctionsTest
143 85
 target4.height=50
144
-target4.name=bonusFunctionsTest
86
+target4.name=scienticFunctions
145 87
 target4.showInterface=false
146
-target4.type=UnitTestTargetJunit4
147
-target4.width=120
148
-target4.x=530
149
-target4.y=300
150
-target5.association=scienticFunctionsTest
88
+target4.type=ClassTarget
89
+target4.width=130
90
+target4.x=500
91
+target4.y=230
151 92
 target5.height=50
152
-target5.name=scienticFunctions
93
+target5.name=Console
153 94
 target5.showInterface=false
154 95
 target5.type=ClassTarget
155
-target5.width=130
156
-target5.x=500
157
-target5.y=230
96
+target5.width=80
97
+target5.x=30
98
+target5.y=160
158 99
 target6.height=50
159 100
 target6.name=scienticFunctionsTest
160 101
 target6.showInterface=false
@@ -169,17 +110,18 @@ target7.type=ClassTarget
169 110
 target7.width=180
170 111
 target7.x=460
171 112
 target7.y=150
113
+target8.association=bonusFunctionsTest
172 114
 target8.height=50
173
-target8.name=CoreFunctions
115
+target8.name=bonusFunctions
174 116
 target8.showInterface=false
175 117
 target8.type=ClassTarget
176 118
 target8.width=120
177
-target8.x=150
178
-target8.y=120
119
+target8.x=500
120
+target8.y=330
179 121
 target9.height=50
180
-target9.name=DiplayMode
122
+target9.name=Operations
181 123
 target9.showInterface=false
182 124
 target9.type=ClassTarget
183
-target9.width=100
184
-target9.x=50
185
-target9.y=360
125
+target9.width=90
126
+target9.x=380
127
+target9.y=370