Browse Source

Use two classes MathMethods and Calculator with operations and switch statment respectivly

Margaret Pierse 6 years ago
parent
commit
9d9574817b
5 changed files with 358 additions and 145 deletions
  1. 217
    29
      Calculator.java
  2. 0
    95
      Console.java
  3. 13
    19
      MainApplication.java
  4. 126
    0
      MathMethods.java
  5. 2
    2
      package.bluej

+ 217
- 29
Calculator.java View File

@@ -1,32 +1,220 @@
1 1
 
2
-/**
3
- * Write a description of class Calculator here.
4
- *
5
- * @author (your name)
6
- * @version (a version number or a date)
7
- */
8
-public class Calculator
9
-
10
-{
11
-    public static double add(double x, double y) {
12
-    double result = x+y;
13
-    return result;
14
-    }
15
-    
16
-    public static double subtract(double x, double y) {
17
-    double result = x-y;
18
-    return result;
19
-    }
20
-    
21
-    public static double multiply(double x, double y) {
22
-    double result = x*y;
23
-    return result;
24
-    }
25
-    
26
-    public static double divide(double x, double y) {
27
-    double result = x/y;
28
-    return result;
29
-    }
2
+import java.util.*;
3
+class Calculator{
4
+    public static void main(String args[]){
5
+
6
+        String toDo;                //what the user wants to do
7
+        double num1 = 0;                //The user's first input
8
+        double num2 = 0;            //The user's second input
9
+        double result = 0;          //The user's answer
10
+        boolean off = false;        //The user typed quit, becomes true
11
+        double memory;
12
+        boolean error = false;
13
+        MathMethods calc = new MathMethods();
14
+
15
+        while (off == false){
16
+            Scanner input1 = new Scanner(System.in);
17
+            System.out.println("What do you want to do?");
18
+            toDo = input1.nextLine();
19
+
20
+            if (toDo.equals("quit")){
21
+                off = true;
22
+                System.out.println("Goodbye!!");
23
+
24
+            } else {
25
+
26
+                //System.out.println("toDO is: " + toDo);
27
+
28
+                System.out.println(toDo + " -- Enter first number:");
29
+                num1 = input1.nextDouble();
30
+                //System.out.println("num1 is: " + num1);
31
+
32
+                switch (toDo){
33
+                    case "add":
34
+                    case "subtract":
35
+                    case "multiply":
36
+                    case "divide":
37
+                    case "exponent":
38
+                    System.out.println(toDo + " -- Enter second number:");
39
+                    num2 = input1.nextDouble();
40
+                    //System.out.println("num2 is: " + num2);
41
+
42
+                }
43
+
44
+                switch (toDo) {        
45
+                    case "add":
46
+                    result = calc.add(num1, num2);
47
+                    memory = result;
48
+                    calc.printAns(String.valueOf(result));
49
+                    break;
50
+
51
+                    case "subtract":
52
+                    result = calc.subtract(num1, num2);
53
+                    memory = result;
54
+                    calc.printAns(String.valueOf(result));
55
+                    break;
56
+
57
+                    case "multiply":
58
+                    result = calc.multiply(num1, num2);
59
+                    memory = result;
60
+                    calc.printAns(String.valueOf(result));
61
+                    break;
62
+
63
+                    case "divide":
64
+                    result = calc.divide(num1, num2);
65
+                    memory = result;
66
+                    calc.printAns(String.valueOf(result));
67
+                    break;
68
+
69
+                    case "exponent":
70
+                    result = calc.exponent(num1, num2);
71
+                    memory = result;
72
+                    calc.printAns(String.valueOf(result));
73
+                    break;
74
+
75
+                    case "cosine":
76
+                    result = calc.cosine(num1);
77
+                    memory = result;
78
+                    calc.printAns(String.valueOf(result));
79
+                    break;
80
+
81
+                    case "inverse cosine":
82
+                    result = calc.invCosine(num1);
83
+                    memory = result;
84
+                    calc.printAns(String.valueOf(result));
85
+                    break;
86
+
87
+                    case "square root":
88
+                    result = calc.squareRoot(num1);
89
+                    memory = result;
90
+                    calc.printAns(String.valueOf(result));
91
+                    break;
92
+                    
93
+                    case "invert":
94
+                    result = calc.invert(num1);
95
+                    memory = result;
96
+                    calc.printAns(String.valueOf(result));
97
+                    break;
98
+                    
99
+                    case "sine":
100
+                    result = calc.sin(num1);
101
+                    memory = result;
102
+                    calc.printAns(String.valueOf(result));
103
+                    break;
104
+                    
105
+                    case "inverse sine":
106
+                    result = calc.iSin(num1);
107
+                    memory = result;
108
+                    calc.printAns(String.valueOf(result));
109
+                    break;
110
+                    
111
+                    case "degree to radian":
112
+                    result = calc.degToRad(num1);
113
+                    memory = result;
114
+                    calc.printAns(String.valueOf(result));
115
+                    break;
116
+                    
117
+                    case "radian to degree":
118
+                    result = calc.radToDeg(num1);
119
+                    memory = result;
120
+                    calc.printAns(String.valueOf(result));
121
+                    break;
122
+                }
123
+            }
30 124
 
31
-  
125
+        }
126
+    }
32 127
 }
128
+        // /**
129
+        // * Created by leon on 2/9/18.
130
+        // */
131
+        // public class Console {
132
+        // Calculator calc = new Calculator();
133
+
134
+        // public static void print(String output, Object... args) {
135
+        // System.out.printf(output, args);
136
+        // }
137
+
138
+        // public static void println(String output, Object... args) {
139
+        // print(output + "\n", args);
140
+        // }
141
+
142
+        // public static String getMethod() {
143
+        // Scanner scanner = new Scanner(System.in);
144
+        // String userInput = scanner.nextLine();
145
+        // return userInput;
146
+        // }
147
+
148
+        // public static String switchStatment() {
149
+        // Scanner scanner = new Scanner(System.in);
150
+        // String ultimateAnswer = "";
151
+        // Boolean turnOff = false;
152
+
153
+        // switch (getMethod()) {
154
+        // case "add" :
155
+        // System.out.println("Enter your numbers:");
156
+        // double userInput = scanner.nextDouble();
157
+        // double nextInput = scanner.nextDouble();
158
+
159
+        // double answer = Calculator.add(userInput, nextInput);
160
+        // ultimateAnswer = Double.toString(answer);
161
+        // return Double.toString(answer);
162
+
163
+        // case "subtract" :
164
+        // System.out.println("Enter your numbers:");
165
+        // userInput = scanner.nextDouble();
166
+        // nextInput = scanner.nextDouble();
167
+
168
+        // answer = Calculator.subtract(userInput, nextInput);
169
+        // ultimateAnswer = Double.toString(answer);
170
+        // return ultimateAnswer;
171
+
172
+        // case "multiply" :
173
+        // System.out.println("Enter your numbers:");
174
+        // userInput = scanner.nextDouble();
175
+        // nextInput = scanner.nextDouble();
176
+
177
+        // answer = Calculator.multiply(userInput, nextInput);
178
+        // ultimateAnswer = Double.toString(answer);
179
+        // return ultimateAnswer;
180
+
181
+        // case "divide" :
182
+        // System.out.println("Enter your numbers:");
183
+        // userInput = scanner.nextDouble();
184
+        // nextInput = scanner.nextDouble();
185
+
186
+        // answer = Calculator.divide(userInput, nextInput);
187
+        // ultimateAnswer = Double.toString(answer);
188
+        // return ultimateAnswer;
189
+
190
+        // case "Square Root" :
191
+
192
+           
193
+        // }
194
+        // return ultimateAnswer;
195
+        // }
196
+
197
+        // /*public static String getStringInput(String prompt) {
198
+        // Scanner scanner = new Scanner(System.in);
199
+        // println(prompt);
200
+        // String userInput = scanner.nextLine();
201
+        // return userInput;
202
+        // }
203
+
204
+        // public static Integer getIntegerInput(String prompt) {
205
+        // Scanner scanner = new Scanner(System.in);
206
+        // println(prompt);
207
+        // int userInput = scanner.nextInt();
208
+        // return null;
209
+        // }
210
+
211
+        // public static Double getDoubleInput(String prompt) {
212
+        // Scanner scanner = new Scanner(System.in);
213
+        // println(prompt);
214
+        // double userInput = scanner.nextDouble();
215
+        // double nextInput = scanner.nextDouble();
216
+        // double answer = Calculator.add(userInput, nextInput);
217
+        // return answer;
218
+        // }*/
219
+
220
+        // }

+ 0
- 95
Console.java View File

@@ -1,95 +0,0 @@
1
-
2
-import java.util.Scanner;
3
-/**
4
- * Created by leon on 2/9/18.
5
- */
6
-public class Console {
7
-    Calculator calc = new Calculator();
8
-
9
-    public static void print(String output, Object... args) {
10
-        System.out.printf(output, args);
11
-    }
12
-
13
-    public static void println(String output, Object... args) {
14
-        print(output + "\n", args);
15
-    }
16
-
17
-    public static String getMethod() {
18
-        Scanner scanner = new Scanner(System.in);
19
-        String userInput = scanner.nextLine();
20
-        return userInput;
21
-    }
22
-
23
-    public static String switchStatment() {
24
-        Scanner scanner = new Scanner(System.in);
25
-        String ultimateAnswer = "";
26
-        Boolean turnOff = false;
27
-        switch (getMethod()) {
28
-            case "add" :
29
-                System.out.println("Enter your numbers:");
30
-                double userInput = scanner.nextDouble();
31
-                double nextInput = scanner.nextDouble();
32
-                
33
-                double answer = Calculator.add(userInput, nextInput);
34
-                ultimateAnswer = Double.toString(answer);
35
-                return ultimateAnswer;
36
-                
37
-            case "subtract" :
38
-                System.out.println("Enter your numbers:");
39
-                userInput = scanner.nextDouble();
40
-                nextInput = scanner.nextDouble();
41
-                
42
-                answer = Calculator.subtract(userInput, nextInput);
43
-                ultimateAnswer = Double.toString(answer);
44
-                return ultimateAnswer;
45
-                
46
-            case "multiply" :
47
-                System.out.println("Enter your numbers:");
48
-                userInput = scanner.nextDouble();
49
-                nextInput = scanner.nextDouble();
50
-                
51
-                answer = Calculator.multiply(userInput, nextInput);
52
-                ultimateAnswer = Double.toString(answer);
53
-                return ultimateAnswer;
54
-                
55
-            case "divide" :
56
-                System.out.println("Enter your numbers:");
57
-                userInput = scanner.nextDouble();
58
-                nextInput = scanner.nextDouble();
59
-                
60
-                answer = Calculator.divide(userInput, nextInput);
61
-                ultimateAnswer = Double.toString(answer);
62
-                return ultimateAnswer;
63
-                
64
-            case "quit" :
65
-                    return "quit";
66
-           
67
-
68
-        }
69
-        return ultimateAnswer;
70
-    }
71
-
72
-    /*public static String getStringInput(String prompt) {
73
-        Scanner scanner = new Scanner(System.in);
74
-        println(prompt);
75
-        String userInput = scanner.nextLine();
76
-        return userInput;
77
-    }
78
-
79
-    public static Integer getIntegerInput(String prompt) {
80
-        Scanner scanner = new Scanner(System.in);
81
-        println(prompt);
82
-        int userInput = scanner.nextInt();
83
-        return null;
84
-    }
85
-
86
-    public static Double getDoubleInput(String prompt) {
87
-        Scanner scanner = new Scanner(System.in);
88
-        println(prompt);
89
-        double userInput = scanner.nextDouble();
90
-        double nextInput = scanner.nextDouble();
91
-        double answer = Calculator.add(userInput, nextInput);
92
-        return answer;
93
-    }*/
94
-
95
-}

+ 13
- 19
MainApplication.java View File

@@ -1,24 +1,18 @@
1 1
  
2 2
 
3
-/**
4
- * Created by leon on 2/9/18.
5
- */
6
-public class MainApplication {
3
+// /**
4
+ // * Created by leon on 2/9/18.
5
+ // */
6
+// public class MainApplication {
7 7
     
8
-    public static void main(String[] args) {
9
-        String d = "";
10
-        while (d != "quit") {
11
-        Console.println("Welcome to my calculator! What would you like to do?");
12
-        d = Console.switchStatment();
13
-        //String s = Console.getStringInput("Enter a string");
14
-        //Integer i = Console.getIntegerInput("Enter an integer");
15
-        //Double d = Console.getDoubleInput("Enter a double.");
16
-
17
-        //Console.println("The user input %s as a string", s);
18
-        //Console.println("The user input %s as a integer", i);
19
-        Console.println("Your answer is %s", d);
20
-    }
21
-}
8
+    // public static void main(String[] args) {
9
+        // String d = "";
10
+        // while (d != "quit") {
11
+        // Console.println("Welcome to my calculator! What would you like to do?");
12
+        // d = Console.switchStatment();
13
+        // Console.println("Your answer is %s", d);
14
+    // }
15
+// }
22 16
     
23 17
     
24
-}
18
+// }

+ 126
- 0
MathMethods.java View File

@@ -0,0 +1,126 @@
1
+import java.util.*;
2
+/**
3
+ * Write a description of class Calculator here.
4
+ *
5
+ * @author (your name)
6
+ * @version (a version number or a date)
7
+ */
8
+public class MathMethods
9
+
10
+{
11
+
12
+    public static Double add(double n1, double n2){
13
+        return (n1 + n2);
14
+    }
15
+
16
+    public static Double subtract(double n1, double n2){
17
+        return (n1 - n2);
18
+    }
19
+
20
+    public static Double multiply(double n1, double n2){
21
+        return (n1 * n2);
22
+    }
23
+
24
+    public static Double divide(double n1, double n2){
25
+        return (n1 / n2);
26
+    }
27
+
28
+    public static Double exponent(double n1, double n2){
29
+        return (Math.pow(n1, n2));
30
+    }
31
+
32
+    public static Double cosine(double n1){
33
+        return (Math.cos(n1));
34
+    }
35
+
36
+    public static Double invCosine(double n1){
37
+        return (Math.acos(n1));
38
+    }
39
+
40
+    public static double squareRoot(double n1) {
41
+        return (Math.sqrt(n1));
42
+
43
+    }
44
+
45
+    public static double invert(double n1) {
46
+        return (n1*-1);
47
+
48
+    }
49
+
50
+    public static double sin(double n1){
51
+        return (Math.sin(n1));
52
+
53
+    }
54
+
55
+    public static double iSin(double n1){
56
+        return (Math.asin(n1));
57
+
58
+    }
59
+
60
+    public static double degToRad(double n1) {
61
+        return (Math.toRadians(n1));
62
+
63
+    }
64
+
65
+    public static double radToDeg(double n1) {
66
+        return (Math.toDegrees(n1));
67
+
68
+    }
69
+
70
+    
71
+    
72
+    public static void printAns(String printAnswer){
73
+        System.out.println("ANSWER: " + printAnswer);
74
+
75
+    }
76
+
77
+}
78
+
79
+
80
+    
81
+    
82
+    
83
+    
84
+// public static double squareRoot(double n1) {
85
+// double result = Math.sqrt(x);
86
+// return result;
87
+// }
88
+
89
+// public static double invert(double n1) {
90
+// double result = x*-1;
91
+// return result;
92
+// }
93
+
94
+// public static double sin(double n1){
95
+// double result = Math.sin(x);
96
+// return result;
97
+// }
98
+
99
+// public static double iSin(double n1){
100
+// double result = Math.asin(x);
101
+// return result;
102
+// }
103
+
104
+// public static double degToRad(double n1) {
105
+// double result = Math.toRadians(x);
106
+// return result;
107
+// }
108
+
109
+// public static double radToDeg(double n1) {
110
+// double result = Math.toDegrees(x);
111
+// return result;
112
+// }
113
+
114
+// public static Double exponent(double n1, double n2){
115
+// return (Math.pow(n1, n2));
116
+// }
117
+
118
+// public static Double cosine(double n1){
119
+// return (Math.cos(n1));
120
+// }
121
+
122
+// public static Double invCosine(double n1){
123
+// return (Math.acos(n1));
124
+// }
125
+
126
+

+ 2
- 2
package.bluej View File

@@ -7,8 +7,8 @@ dependency2.to=Calculator
7 7
 dependency2.type=UsesDependency
8 8
 editor.fx.0.height=1020
9 9
 editor.fx.0.width=1247
10
-editor.fx.0.x=2093
11
-editor.fx.0.y=165
10
+editor.fx.0.x=1891
11
+editor.fx.0.y=66
12 12
 objectbench.height=80
13 13
 objectbench.width=352
14 14
 package.divider.horizontal=0.5993322203672788