Parcourir la source

Merge branch 'master' of WJB3003/ZCW-MacroLabs-OOP-ScientificCalculator into master

William Brown il y a 6 ans
Parent
révision
c47e0453a3
3 fichiers modifiés avec 67 ajouts et 58 suppressions
  1. 12
    8
      Console.java
  2. 24
    19
      opSwitch.java
  3. 31
    31
      simpleOp.java

+ 12
- 8
Console.java Voir le fichier

@@ -20,7 +20,7 @@ public class Console {
20 20
         return userInput;
21 21
     }
22 22
 
23
-    public static Integer getIntegerInput(String prompt) {
23
+    public static Integer  getIntInput(String prompt) {
24 24
         Scanner scanner = new Scanner(System.in);
25 25
         println(prompt);
26 26
         int userInput = scanner.nextInt();
@@ -44,12 +44,12 @@ public class Console {
44 44
         //CLEARS SCREEN / SET TO ZERO ??
45 45
     }
46 46
     
47
-    public static int getInput(String numberString){
47
+    public static double getInput(String numberString){
48 48
         //GETS INPUT FROM PERSON ( X )
49 49
             //String numberString = getStringInput("Please choose a number.");
50 50
             quit(numberString);
51 51
         try{
52
-            int number1 = Integer.parseInt(numberString);
52
+            double number1 = Double.parseDouble(numberString);
53 53
             //System.out.println(number1);
54 54
             return number1;
55 55
         }catch(Exception e){
@@ -57,10 +57,10 @@ public class Console {
57 57
         }
58 58
     }
59 59
     /*
60
-    public static int invert(int number){
60
+    public static double invert(double number){
61 61
         //TURNS NEGATIVE TO POSITIVE OR POSITIVE TO NEGATIVE
62 62
         try{
63
-            int invertedNumber = -number;
63
+            double invertedNumber = -number;
64 64
             System.out.println(invertedNumber);
65 65
             return invertedNumber;
66 66
         } catch(Exception e){
@@ -68,13 +68,13 @@ public class Console {
68 68
         }
69 69
     }
70 70
     */
71
-    public static int getNumber(String numberString){
71
+    public static double getNumber(String numberString){
72 72
         //GETS INPUT FROM PERSON ( Y )
73 73
             //System.out.println(first + " " + op);
74 74
             //String numberString = getStringInput("Please choose a number.");
75 75
             quit(numberString);
76 76
         try{
77
-            int number2 = Integer.parseInt(numberString);
77
+            double number2 = Double.parseDouble(numberString);
78 78
             //System.out.println(number2);
79 79
             return number2;
80 80
         }catch(Exception e){
@@ -84,6 +84,7 @@ public class Console {
84 84
     
85 85
     public static String getOp(){
86 86
         //GET OPERATION FROM PERSON
87
+            System.out.println("Current Number: " + simpleOp.currentNumber);
87 88
             String operation = getStringInput("Please choose your operation.");
88 89
             if (operation == null) {
89 90
                 System.out.println("You have not provided valid input. Please enter \"?\" for a list of commands.");
@@ -96,9 +97,12 @@ public class Console {
96 97
     
97 98
     public static void quit(String answer){
98 99
         if(answer.equals("quit") || answer.equals("exit")){
99
-               System.exit(0);
100
+            System.exit(0);
100 101
         } else if(answer.equals("clear")) {
101 102
             System.out.print('\f');
103
+        } else if(answer.equals("reset")){
104
+            simpleOp.currentNumber = 0;
105
+            System.out.println(simpleOp.currentNumber);
102 106
         }
103 107
     }
104 108
    

+ 24
- 19
opSwitch.java Voir le fichier

@@ -24,8 +24,10 @@ public class opSwitch
24 24
                 +"square root"+"\n"
25 25
                 +"exponent"+"\n"
26 26
                 +"inverse"+"\n"
27
+                +"invert"+"\n"
27 28
                 +"quit"+"\n"
28
-                +"clear"+"\n");
29
+                +"clear"+"\n"
30
+                +"reset"+"\n");
29 31
                 break;
30 32
             case "+": 
31 33
                 //x = Console.getStringInput("Please enter first number.");
@@ -33,36 +35,39 @@ public class opSwitch
33 35
                 simpleOp.add(op.currentNumber, Console.getNumber(y));
34 36
                 break;
35 37
             case "-": 
36
-                x = Console.getStringInput("Please enter first number.");
37
-                y = Console.getStringInput("Please enter second number.");
38
-                simpleOp.sub(Console.getNumber(x), Console.getNumber(y));
38
+                //x = Console.getStringInput("Please enter first number.");
39
+                y = Console.getStringInput("Please enter number.");
40
+                simpleOp.sub(op.currentNumber, Console.getNumber(y));
39 41
                 break;
40 42
             case "*": 
41
-                x = Console.getStringInput("Please enter multiplicand.");
42
-                y = Console.getStringInput("Please enter multiplier.");
43
-                simpleOp.mul(Console.getNumber(x), Console.getNumber(y));
43
+                //x = Console.getStringInput("Please enter multiplicand.");
44
+                y = Console.getStringInput("Please enter number.");
45
+                simpleOp.mul(op.currentNumber, Console.getNumber(y));
44 46
                 break;
45 47
             case "/": 
46
-                x = Console.getStringInput("Please enter dividend.");
47
-                y = Console.getStringInput("Please enter divisor.");
48
-                simpleOp.div(Console.getNumber(x), Console.getNumber(y));
48
+                //x = Console.getStringInput("Please enter dividend.");
49
+                y = Console.getStringInput("Please enter number.");
50
+                simpleOp.div(op.currentNumber, Console.getNumber(y));
49 51
                 break;
50 52
             case "square": 
51
-                x = Console.getStringInput("Please enter your number.");
52
-                simpleOp.squ(Console.getNumber(x));
53
+                //x = Console.getStringInput("Please enter your number.");
54
+                simpleOp.squ(op.currentNumber);
53 55
                 break;
54 56
             case "square root": 
55
-                x = Console.getStringInput("Please enter your number.");
56
-                simpleOp.sqrt(Console.getNumber(x));
57
+                //x = Console.getStringInput("Please enter your number.");
58
+                simpleOp.sqrt(op.currentNumber);
57 59
                 break;
58 60
             case "exponent": 
59
-                x = Console.getStringInput("Please enter base.");
60
-                y = Console.getStringInput("Please enter exponent.");
61
-                simpleOp.exp(Console.getNumber(x), Console.getNumber(y));
61
+                //x = Console.getStringInput("Please enter base.");
62
+                y = Console.getStringInput("Please enter the exponent.");
63
+                simpleOp.exp(op.currentNumber, Console.getNumber(y));
62 64
                 break;
63 65
             case "inverse":
64
-                x = Console.getStringInput("Please enter your number.");
65
-                simpleOp.inv(Console.getNumber(x));
66
+                //x = Console.getStringInput("Please enter your number.");
67
+                simpleOp.inv(op.currentNumber);
68
+                break;
69
+            case "invert":
70
+                simpleOp.invert(op.currentNumber);
66 71
                 break;
67 72
             // default:
68 73
                 // System.out.println("You have not provided valid input. Please enter \"?\" for a list of commands.");

+ 31
- 31
simpleOp.java Voir le fichier

@@ -8,73 +8,73 @@
8 8
 import java.util.*;
9 9
 public class simpleOp
10 10
 {
11
-    public static int currentNumber = 0;
11
+    public static double currentNumber = 0;
12 12
     
13
-    public static int add(int x, int y){
13
+    public static double add(double x, double y){
14 14
         //CALULATES THE SUM OF TWO NUMBERS
15 15
         currentNumber = x + y;
16
-        System.out.println(currentNumber);
16
+        //System.out.println(currentNumber);
17 17
         return currentNumber;
18 18
     }
19 19
     
20
-    public static int sub(int x, int y){
20
+    public static double sub(double x, double y){
21 21
         //CALULATES THE DIFFERENCE OF TWO NUMBERS
22
-        int currentNumber = x - y;
23
-        System.out.println(x + " - " + y + " = " + currentNumber);
22
+        currentNumber = x - y;
23
+        //System.out.println(currentNumber);
24 24
         return currentNumber;
25 25
     }
26 26
     
27
-    public static int mul(int x, int y){
27
+    public static double mul(double x, double y){
28 28
         //CALULATES THE PRODUCT OF TWO NUMBERS
29
-        int currentNumber = x * y;
30
-        System.out.println(x + " * " + y + " = " + currentNumber);
29
+        currentNumber = x * y;
30
+        //System.out.println(currentNumber);
31 31
         return currentNumber;
32 32
     }
33 33
     
34
-    public static int div(int x, int y){
34
+    public static double div(double x, double y){
35 35
         //CALULATES THE QUOTIENT OF TWO NUMBERS
36
-        int currentNumber = x / y;
37
-        System.out.println(x + " / " + y + " = " + currentNumber);
36
+        currentNumber = x / y;
37
+        //System.out.println(currentNumber);
38 38
         return currentNumber;
39 39
     }
40 40
     
41
-    public static int squ(int x){
41
+    public static double squ(double x){
42 42
         //CALULATES THE SQUARE OF ONE NUMBER
43
-        //CASTED TO INT FOR NOW... IT'S IN ALPHA, YOU DIG?
44
-        int currentNumber = (int) Math.pow(x,2);
45
-        System.out.println(x + "\u00B2 = " + currentNumber);
43
+        //CASTED TO double FOR NOW... IT'S IN ALPHA, YOU DIG?
44
+        currentNumber = (int) Math.pow(x,2);
45
+        //System.out.println(currentNumber);
46 46
         return currentNumber;
47 47
     }
48 48
     
49
-    public static int sqrt(int x){
49
+    public static double sqrt(double x){
50 50
         //CALULATES THE SQUARE ROOT OF ONE NUMBER
51
-        //CASTED TO INT FOR NOW... IT'S IN ALPHA, YOU DIG?
52
-        int currentNumber = (int) Math.sqrt(x);
53
-        System.out.println("√" + x + " = " + currentNumber);
51
+        //CASTED TO double FOR NOW... IT'S IN ALPHA, YOU DIG?
52
+        currentNumber = (int) Math.sqrt(x);
53
+        //System.out.println(currentNumber);
54 54
         return currentNumber;
55 55
     }
56 56
     
57
-    public static int exp(int x, int y){
57
+    public static double exp(double x, double y){
58 58
         //CALULATES EXPONENTIATION OF X TO POWER OF Y
59
-        //CASTED TO INT FOR NOW... IT'S IN ALPHA, YOU DIG?
60
-        int currentNumber = (int) Math.pow(x,y);
61
-        System.out.println(x + "^" + y + " = " + currentNumber);
59
+        //CASTED TO double FOR NOW... IT'S IN ALPHA, YOU DIG?
60
+        currentNumber = (int) Math.pow(x,y);
61
+        //System.out.println(currentNumber);
62 62
         return currentNumber;
63 63
     }
64 64
     
65 65
     public static double inv(double x){
66 66
         //CALULATES THE INVERSE OF NUMBER
67
-        //CASTED TO INT FOR NOW... IT'S IN ALPHA, YOU DIG?
68
-        double currentNumber = 1 / x;
69
-        System.out.println("1 / " + x + " = " + currentNumber);
67
+        //CASTED TO double FOR NOW... IT'S IN ALPHA, YOU DIG?
68
+        currentNumber = (1/x);
69
+        //System.out.println(currentNumber);
70 70
         return currentNumber;
71 71
     }
72 72
     
73
-    public static int invert(int number){
73
+    public static double invert(double x){
74 74
         //TURNS NEGATIVE TO POSITIVE OR POSITIVE TO NEGATIVE
75
-            currentNumber = -number;
76
-            System.out.println(currentNumber);
77
-            return currentNumber;
75
+        currentNumber = -x;
76
+        //System.out.println(currentNumber);
77
+        return currentNumber;
78 78
     }
79 79
     
80 80
 }