Alfredo Castillo 6 anni fa
parent
commit
1b246ea28e
5 ha cambiato i file con 271 aggiunte e 147 eliminazioni
  1. 6
    1
      Calculator.java
  2. 49
    1
      CalculatorTest.java
  3. 207
    144
      Console.java
  4. 1
    1
      NumericInputParser.java
  5. 8
    0
      __SHELL16.java

+ 6
- 1
Calculator.java Vedi File

@@ -4,10 +4,14 @@ public class Calculator
4 4
     // instance variables - replace the example below with your own
5 5
     private double displayValue;
6 6
     private double memoryValue;
7
+<<<<<<< HEAD
7 8
 
8 9
     //private CoreFeatures core;
9 10
     //private ScientificFeatures science;
10 11
     //private BonusFeatures bonus;
12
+=======
13
+    private boolean err;
14
+>>>>>>> 85cb6e118e199051f7fc22d59044b58cdf7dcf01
11 15
 
12 16
     /**
13 17
      * Constructor for objects of class Calculator
@@ -16,6 +20,7 @@ public class Calculator
16 20
     {
17 21
         displayValue = 0;
18 22
         memoryValue = 0;
23
+        err = false;
19 24
     }
20 25
 
21 26
     //Memory set
@@ -37,6 +42,7 @@ public class Calculator
37 42
     //Clear and retrieve display value
38 43
     public double clear(){
39 44
         displayValue = 0;
45
+        err = false;
40 46
         return displayValue;
41 47
     }
42 48
 
@@ -148,6 +154,5 @@ public class Calculator
148 154
     displayValue = Math.E;
149 155
     return Math.E;
150 156
     }
151
-    
152 157
 
153 158
 }

+ 49
- 1
CalculatorTest.java Vedi File

@@ -25,14 +25,62 @@ public class CalculatorTest
25 25
     }
26 26
    @Test
27 27
     public void subtractionTest(){
28
+        //:Given
29
+        Double expected = Double.valueOf(2-1);
30
+        //:When
31
+        Calculator c = new Calculator();
32
+        Double actual = c.subtract(2, 1);
33
+        //Then
34
+        Assert.assertEquals(expected, actual);
28 35
     }
29 36
    @Test
30 37
    public void mutliplicationTest(){
38
+       //:Given
39
+       Double expected = Double.valueOf(2*3);
40
+       //:When
41
+       Calculator c = new Calculator();
42
+       Double actual = c.multiply(2, 3);
43
+       //:Then
44
+       Assert.assertEquals(expected, actual);
31 45
     }
32 46
    @Test
33
-   public void divisiionTest(){
47
+   public void divisionTest(){
48
+       //:Given
49
+       Double expected = Double.valueOf(4/3);
50
+       //:When
51
+       Calculator c = new Calculator();
52
+       Double actual = c.divide(4,3);
53
+       //:Then
54
+       Assert.assertEquals(expected, actual);
55
+    }
56
+   @Test
57
+   public void powTest(){
58
+       //:Given
59
+       Double expected = Double.valueOf(9);
60
+       //:When
61
+       Calculator c = new Calculator();
62
+       Double actual = c.pow(3,2);
63
+       //:Then
64
+       Assert.assertEquals(expected, actual);
65
+    }
66
+   @Test
67
+    public void squareTest(){
68
+        //:Given
69
+        Double expected = Double.valueOf(225);
70
+        //:When
71
+        Calculator c = new Calculator();
72
+        Double actual = c.square(15);
73
+        //:Then
74
+        Assert.assertEquals(expected, actual);
34 75
     }
35 76
    @Test
36 77
    public void sqrtTest(){
78
+       //:Given
79
+       Double expected = Double.valueOf(Math.sqrt(4));
80
+       //:When
81
+       Calculator c = new Calculator();
82
+       Double actual = c.squareRoot(4);
83
+       //:Then
84
+       Assert.assertEquals(expected, actual);
37 85
     }
38 86
 }

+ 207
- 144
Console.java Vedi File

@@ -1,7 +1,5 @@
1
- 
2 1
 
3 2
 import java.util.Scanner;
4
-
5 3
 /**
6 4
  * Created by leon on 2/9/18.
7 5
  * 
@@ -14,177 +12,230 @@ import java.util.Scanner;
14 12
  * 
15 13
  */
16 14
 public class Console {
17
-    
15
+
18 16
     private Calculator calc;
19 17
     private NumericInputParser parse;
20
-    
18
+
21 19
     public Console(){
22 20
         calc = new Calculator();
23 21
         parse = new NumericInputParser();
24 22
     }
25
-    
26
-    
23
+
27 24
     public void runCommandLoop() 
28 25
     {
29 26
         Scanner input = new Scanner(System.in);
30
-        Calculator calc = new Calculator();
31 27
         println("Welcome to this calculator!");
32 28
         println("For a user guide, enter '?'");
33
-        
29
+
34 30
         while (true) {
35 31
             print("Enter a command: ");
36 32
             String command = input.next();
37
-            if (command.equalsIgnoreCase("exit"))  {
38
-                print("Thanks for calculating with us!");
39
-                break;
40
-            }
41
-            else if (command.equalsIgnoreCase("clear")){
42
-                calc.clear();
43
-                println(parse.getFormattedNumber(calc.getDisplayValue()));
44
-            }
45
-            else if (command.equalsIgnoreCase("val")){
46
-                double val = calc.getDisplayValue();
47
-                println("The current value is: %s", parse.getFormattedNumber(val));
48
-            }
49
-            else if (command.equalsIgnoreCase("?")){
50
-                printGuide();
51
-            }
52
-            
53
-            //Core Features
54
-            
55
-            else if (command.equalsIgnoreCase("add"))  {
56
-                double d1 = getDoubleInput("First value: ");
57
-                double d2 = getDoubleInput("Second value: ");
58
-                double result = calc.add(d1, d2);
59
-                println("The sum is: %s", parse.getFormattedNumber(result));
60
-            }
61
-            else if (command.equalsIgnoreCase("subtract")) {
62
-               double d1 = getDoubleInput("First value: ");
63
-               double d2 = getDoubleInput("Second value: ");
64
-               double result = calc.subtract(d1, d2);
65
-               println("The difference is: %s", parse.getFormattedNumber(result));
66
-               
67
-            }
68
-            else if (command.equalsIgnoreCase("multiply")) {
69
-                double d1 = getDoubleInput("Multiplicand: ");
70
-                double d2 = getDoubleInput("Multiplier: ");
71
-                double result = calc.multiply(d1, d2);
72
-                println("The product is: %s", parse.getFormattedNumber(result));
73
-            }
74
-            else if (command.equalsIgnoreCase("divide")) {
75
-                double d1 = getDoubleInput("Dividend: ");
76
-                double d2 = getDoubleInput("Divisor: ");
77
-                double result = calc.multiply(d1, d2);
78
-                println("The quotient is: %s", parse.getFormattedNumber(result));
79
-            }
80
-            else if (command.equalsIgnoreCase("square"))  {
81
-                double d = getDoubleInput("Value: ");
82
-                double result = calc.square(d);
83
-                println("The square is: %s", parse.getFormattedNumber(result));
84
-                
85
-            }
86
-            else if (command.equalsIgnoreCase("pow")) {
87
-                double b = getDoubleInput("Base: ");
88
-                int e = getIntegerInput("Exponent: ");
89
-                double result = calc.pow(b, e);
90
-                println("The exponentiation is: %s", parse.getFormattedNumber(result));
91
-            }
92
-            else if (command.equalsIgnoreCase("sqrt")) {
93
-                double d = getDoubleInput("Value: ");
94
-                double result = calc.squareRoot(d);
95
-                println("The square root is: %s", parse.getFormattedNumber(result));
96
-                
97
-            }
98
-            else if (command.equalsIgnoreCase("inverse")) {
99
-                double d = getDoubleInput("Value: ");
100
-                double result = calc.inverse(d);
101
-                println("The inverse is: %s", result);
102
-            }
103
-            
104
-            //trig functions
105
-            else if (command.equalsIgnoreCase("sin")) {
106
-                double d = getDoubleInput("Value: ");
107
-                //call the sin method on calc and display
108
-                double result = calc.sin(d);
109
-                println("The sin is: %s", parse.getFormattedAngle(result));
110
-                
111
-            }
112
-            else if (command.equalsIgnoreCase("cos")) {
113
-                double d = getDoubleInput("Value: ");
114
-                //call the sin method on calc and display
115
-                double result = calc.cos(d);
116
-                println("The cos is: %s", parse.getFormattedAngle(result));
117
-                
118
-            }
119
-            else if (command.equalsIgnoreCase("tan")) {
120
-                double d = getDoubleInput("Value: ");
121
-                //call the tan method on calc and display
122
-                double result = calc.tan(d);
123
-                println("The tan is: %s", parse.getFormattedAngle(result));
124
-                
125
-            }
126
-            
127
-            //display mode changes
128
-            else if (command.equalsIgnoreCase("hex") ||
129
-                     command.equalsIgnoreCase("oct") ||
130
-                     command.equalsIgnoreCase("dec") ||
131
-                     command.equalsIgnoreCase("bin") ){
132
-                String displayMode = parse.switchDisplayMode(command);
133
-                println("Display mode changed to %s", displayMode);
134
-            }
135
-            else if (command.equalsIgnoreCase("display")) {
136
-                String displayMode = parse.switchDisplayMode();
137
-                println("Display mode changed to %s", displayMode);
138
-            }
139
-            
140
-            //trig units mode changes
141
-            else if (command.equalsIgnoreCase("rad") ||
142
-                     command.equalsIgnoreCase("deg")) {
143
-                String trigUnitsMode = parse.switchUnitsMode(command);
144
-                println("Trig units changed to %s", trigUnitsMode);
145
-            }
146
-            else if (command.equalsIgnoreCase("trig")) {
147
-                String trigUnitsMode = parse.switchUnitsMode();
148
-                println("Trig units changed to %s", trigUnitsMode);
149
-            }
150
-            
151
-            //memory features
152
-            else if (command.equalsIgnoreCase("M+")) {
153
-                //add the display value to memory
154
-                double mem = calc.memSet();
155
-                println("Memory value set to: %s", parse.getFormattedNumber(mem));
156
-            }
157
-            else if (command.equalsIgnoreCase("MC")) {
158
-                //clear the memory
159
-                double mem = calc.memClear();
160
-                println("Memory cleared.\nMemory value set to: %s", parse.getFormattedNumber(mem));
161
-            }
162
-            else if (command.equalsIgnoreCase("MRC")) {
163
-                double mem = calc.mrc();
164
-                
33
+
34
+            try {
35
+                if (command.equalsIgnoreCase("exit"))  {
36
+                    print("Thanks for calculating with us!");
37
+                    break;
38
+                }
39
+                else if (command.equalsIgnoreCase("clear")){
40
+                    calc.clear();
41
+                    println(parse.getFormattedNumber(calc.getDisplayValue()));
42
+                }
43
+                else if (command.equalsIgnoreCase("val")){
44
+                    double val = calc.getDisplayValue();
45
+                    println("The current value is: %s", parse.getFormattedNumber(val));
46
+                }
47
+                else if (command.equalsIgnoreCase("?")){
48
+                    printGuide();
49
+                }
50
+
51
+                //Core Features
52
+
53
+                else if (command.equalsIgnoreCase("add"))  {
54
+                    double d1 = getDoubleInput("First value: ");
55
+                    double d2 = getDoubleInput("Second value: ");
56
+                    double result = calc.add(d1, d2);
57
+                    println("The sum is: %s", parse.getFormattedNumber(result));
58
+                }
59
+                else if (command.equalsIgnoreCase("sub")) {
60
+                    double d1 = getDoubleInput("First value: ");
61
+                    double d2 = getDoubleInput("Second value: ");
62
+                    double result = calc.subtract(d1, d2);
63
+                    println("The difference is: %s", parse.getFormattedNumber(result));
64
+
65
+                }
66
+                else if (command.equalsIgnoreCase("mult")) {
67
+                    double d1 = getDoubleInput("Multiplicand: ");
68
+                    double d2 = getDoubleInput("Multiplier: ");
69
+                    double result = calc.multiply(d1, d2);
70
+                    println("The product is: %s", parse.getFormattedNumber(result));
71
+                }
72
+                else if (command.equalsIgnoreCase("div")) {
73
+                    double d1 = getDoubleInput("Dividend: ");
74
+                    double d2 = getDoubleInput("Divisor: ");
75
+                    if (d2 == 0){
76
+                        errMessage();
77
+                    }
78
+                    else { 
79
+                        double result = calc.divide(d1, d2);
80
+                        println("The quotient is: %s", parse.getFormattedNumber(result));
81
+                    }   
82
+                }
83
+                else if (command.equalsIgnoreCase("sq"))  {
84
+                    double d = getDoubleInput("Value: ");
85
+                    double result = calc.square(d);
86
+                    println("The square is: %s", parse.getFormattedNumber(result));
87
+
88
+                }
89
+                else if (command.equalsIgnoreCase("pow")) {
90
+                    double b = getDoubleInput("Base: ");
91
+                    int e = getIntegerInput("Exponent: ");
92
+                    double result = calc.pow(b, e);
93
+                    println("The exponentiation is: %s", parse.getFormattedNumber(result));
94
+                }
95
+                else if (command.equalsIgnoreCase("sqrt")) {
96
+                    double d = getDoubleInput("Value: ");
97
+                    double result = calc.squareRoot(d);
98
+                    println("The square root is: %s", parse.getFormattedNumber(result));
99
+
100
+                }
101
+                else if (command.equalsIgnoreCase("inv")) {
102
+                    double d = getDoubleInput("Value: ");
103
+                    if (d == 0){
104
+                        errMessage();
105
+                    }
106
+                    else {
107
+                        double result = calc.inverse(d);
108
+                        println("The inverse is: %s", result);
109
+                    }
110
+                }
111
+
112
+                //trig functions
113
+                else if (command.equalsIgnoreCase("sin")) {
114
+                    double d = getDoubleInput("Value: ");
115
+                    //call the sin method on calc and display
116
+                    double result = calc.sin(d);
117
+                    println("The sin is: %s", parse.getFormattedAngle(result));
118
+
119
+                }
120
+                else if (command.equalsIgnoreCase("cos")) {
121
+                    double d = getDoubleInput("Value: ");
122
+                    //call the sin method on calc and display
123
+                    double result = calc.cos(d);
124
+                    println("The cos is: %s", parse.getFormattedAngle(result));
125
+
126
+                }
127
+                else if (command.equalsIgnoreCase("tan")) {
128
+                    double d = getDoubleInput("Value: ");
129
+                    //call the tan method on calc and display
130
+                    double result = calc.tan(d);
131
+                    println("The tan is: %s", parse.getFormattedAngle(result));
132
+
133
+                }
134
+                else if (command.equalsIgnoreCase("cot")) {
135
+                    double d = getDoubleInput("Value: ");
136
+                    double result = calc.inverseTan(d);
137
+                    println("The cot is: %s", parse.getFormattedAngle(result));
138
+
139
+                }
140
+                else if (command.equalsIgnoreCase("csc")){
141
+                    double d = getDoubleInput("Value: ");
142
+                    double result = calc.inverseSin(d);
143
+                    println("The csc is: %s", parse.getFormattedAngle(result));
144
+                }
145
+                else if (command.equalsIgnoreCase("sec")){
146
+                    double d = getDoubleInput("Value: ");
147
+                    double result = calc.inverseCos(d);
148
+                    println("The sec is: %s", parse.getFormattedAngle(result));
149
+                }
150
+
151
+                //Bonus Functions
152
+                else if (command.equalsIgnoreCase("log")){
153
+                    double d = getDoubleInput("Value: ");
154
+                    double result = calc.log(d);
155
+                    println("The log is: %s", parse.getFormattedNumber(result));
156
+                }
157
+                else if (command.equalsIgnoreCase("antilog")){
158
+                    double d = getDoubleInput("Value: ");
159
+                    double result = calc.inverseLog(d);
160
+                    println("The inverse log (base 10) is: %s", parse.getFormattedNumber(result));
161
+                }
162
+                else if (command.equalsIgnoreCase("antiln")){
163
+                    double d = getDoubleInput("Value: ");
164
+                    double result = calc.inverseLn(d);
165
+                    println("The inverse natural log is: %s", parse.getFormattedNumber(result));
166
+                }
167
+                else if (command.equalsIgnoreCase("pi")){
168
+                    println("The current value is now: %s", parse.getFormattedNumber(calc.displayPi()));
169
+                }
170
+                else if (command.equalsIgnoreCase("e")){
171
+                    println("The current value is now: %s", parse.getFormattedNumber(calc.displayE()));
172
+                }
173
+                //display mode changes
174
+                else if (command.equalsIgnoreCase("hex") ||
175
+                command.equalsIgnoreCase("oct") ||
176
+                command.equalsIgnoreCase("dec") ||
177
+                command.equalsIgnoreCase("bin") ){
178
+                    String displayMode = parse.switchDisplayMode(command);
179
+                    println("Display mode changed to %s", displayMode);
180
+                }
181
+                else if (command.equalsIgnoreCase("display")) {
182
+                    String displayMode = parse.switchDisplayMode();
183
+                    println("Display mode changed to %s", displayMode);
184
+                }
185
+
186
+                //trig units mode changes
187
+                else if (command.equalsIgnoreCase("rad") ||
188
+                command.equalsIgnoreCase("deg")) {
189
+                    String trigUnitsMode = parse.switchUnitsMode(command);
190
+                    println("Trig units changed to %s", trigUnitsMode);
191
+                }
192
+                else if (command.equalsIgnoreCase("trig")) {
193
+                    String trigUnitsMode = parse.switchUnitsMode();
194
+                    println("Trig units changed to %s", trigUnitsMode);
195
+                }
196
+
197
+                //memory features
198
+                else if (command.equalsIgnoreCase("M+")) {
199
+                    //add the display value to memory
200
+                    double mem = calc.memSet();
201
+                    println("Memory value set to: %s", parse.getFormattedNumber(mem));
202
+                }
203
+                else if (command.equalsIgnoreCase("MC")) {
204
+                    //clear the memory
205
+                    double mem = calc.memClear();
206
+                    println("Memory cleared.\nMemory value set to: %s", parse.getFormattedNumber(mem));
207
+                }
208
+                else if (command.equalsIgnoreCase("MRC")) {
209
+                    double mem = calc.mrc();
210
+
211
+                }
212
+
213
+                else {
214
+                    System.out.println("Invalid input. Enter '?' for a user guide");
215
+                    System.out.println("Enter 'exit' to quit");
216
+                }
165 217
             }
166
-            
167
-            
168
-            else {
169
-                System.out.println("Invalid input. Enter '?' for a user guide");
170
-                System.out.println("Enter 'exit' to quit");
218
+            catch (ArithmeticException e) {
219
+                errMessage();
171 220
             }
172 221
         }
173 222
 
174 223
     }
175
-    
224
+
176 225
     public static void print(String output, Object... args) {
177 226
         System.out.printf(output, args);
178 227
     }
228
+
179 229
     public static void println(String output, Object... args) {
180 230
         print(output + "\n", args);
181 231
     }
182
-    
232
+
183 233
     public String getStringInput(String prompt) {
184 234
         Scanner s = new Scanner(System.in);
185 235
         print(prompt);
186 236
         return s.nextLine();
187 237
     }
238
+
188 239
     public Integer getIntegerInput(String prompt) {
189 240
         Scanner s = new Scanner(System.in);
190 241
         print(prompt);
@@ -211,6 +262,7 @@ public class Console {
211 262
         return i;
212 263
 
213 264
     }
265
+
214 266
     public Double getDoubleInput(String prompt) {
215 267
         Scanner s = new Scanner(System.in);
216 268
         print(prompt);
@@ -232,15 +284,26 @@ public class Console {
232 284
                     println("Current display mode is: " + parse.getDisplayMode());
233 285
                 }
234 286
             }
235
-        
287
+
236 288
         }
237 289
         return d;
238 290
     }
291
+
292
+    private void errMessage(){
293
+        while(true){
294
+            String clear = getStringInput("ERR\n");
295
+            if (clear.equalsIgnoreCase("clear")){
296
+                break;
297
+            }   
298
+        }
299
+    }
300
+
239 301
     public double getAngleInput(String prompt) {
240 302
         Scanner s = new Scanner(System.in);
241 303
         print(prompt);
242 304
         return parse.getAngle(s.nextLine());
243 305
     }
306
+
244 307
     public void printGuide(){
245 308
         println("Guide Under Construction");
246 309
     }

+ 1
- 1
NumericInputParser.java Vedi File

@@ -66,7 +66,7 @@ public class NumericInputParser
66 66
         double val = 0;
67 67
         switch (displayMode) {
68 68
             case "dec" :
69
-                val = Integer.parseInt(input, 10);
69
+                val = Double.parseDouble(input);
70 70
                 break;
71 71
             case "bin" :
72 72
                 val = Integer.parseInt(input, 2);

+ 8
- 0
__SHELL16.java Vedi File

@@ -0,0 +1,8 @@
1
+
2
+public class __SHELL16 extends bluej.runtime.Shell {
3
+public static void run() throws Throwable {
4
+
5
+java.lang.String[] __bluej_param0 = { };
6
+MainApplication.main(__bluej_param0);
7
+
8
+}}