Browse Source

Added rounding

Nathan Hall 6 years ago
parent
commit
a0c06052a8
5 changed files with 233 additions and 66 deletions
  1. 132
    0
      Calculator.java
  2. 0
    32
      Console.java
  3. 0
    17
      MainApplication.java
  4. 84
    0
      MathMethods.java
  5. 17
    17
      package.bluej

+ 132
- 0
Calculator.java View File

@@ -0,0 +1,132 @@
1
+
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
+                    case "roundTo":
124
+                    memory = result;
125
+                    calc.roundTo(memory, (int)num1);
126
+                    break;
127
+                }
128
+            }
129
+
130
+        }
131
+    }
132
+}

+ 0
- 32
Console.java View File

@@ -1,32 +0,0 @@
1
- 
2
-
3
-import java.util.Scanner;
4
-
5
-/**
6
- * Created by leon on 2/9/18.
7
- */
8
-public class Console {
9
-
10
-    public static void print(String output, Object... args) {
11
-        System.out.printf(output, args);
12
-    }
13
-
14
-    public static void println(String output, Object... args) {
15
-        print(output + "\n", args);
16
-    }
17
-
18
-    public static String getStringInput(String prompt) {
19
-        Scanner scanner = new Scanner(System.in);
20
-        println(prompt);
21
-        String userInput = scanner.nextLine();
22
-        return userInput;
23
-    }
24
-
25
-    public static Integer getIntegerInput(String prompt) {
26
-        return null;
27
-    }
28
-
29
-    public static Double getDoubleInput(String prompt) {
30
-        return null;
31
-    }
32
-}

+ 0
- 17
MainApplication.java View File

@@ -1,17 +0,0 @@
1
- 
2
-
3
-/**
4
- * Created by leon on 2/9/18.
5
- */
6
-public class MainApplication {
7
-    public static void main(String[] args) {
8
-        Console.println("Welcome to my calculator!");
9
-        String s = Console.getStringInput("Enter a string");
10
-        Integer i = Console.getIntegerInput("Enter an integer");
11
-        Double d = Console.getDoubleInput("Enter a double.");
12
-
13
-        Console.println("The user input %s as a string", s);
14
-        Console.println("The user input %s as a integer", i);
15
-        Console.println("The user input %s as a d", d);
16
-    }
17
-}

+ 84
- 0
MathMethods.java View File

@@ -0,0 +1,84 @@
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
+    public static void printAns(String printAnswer){
72
+        System.out.println("ANSWER: " + printAnswer);
73
+
74
+    }
75
+    
76
+    public static void roundTo(Double memory, int numPlaces){
77
+
78
+        System.out.printf("ANSWER: %." + (numPlaces)+ "f\n", memory);
79
+
80
+    }
81
+    
82
+    
83
+
84
+}

+ 17
- 17
package.bluej View File

@@ -1,20 +1,20 @@
1 1
 #BlueJ package file
2
-dependency1.from=MainApplication
3
-dependency1.to=Console
2
+dependency1.from=Calculator
3
+dependency1.to=MathMethods
4 4
 dependency1.type=UsesDependency
5 5
 editor.fx.0.height=722
6 6
 editor.fx.0.width=800
7
-editor.fx.0.x=640
8
-editor.fx.0.y=23
9
-objectbench.height=214
7
+editor.fx.0.x=0
8
+editor.fx.0.y=132
9
+objectbench.height=205
10 10
 objectbench.width=595
11 11
 package.divider.horizontal=0.6
12
-package.divider.vertical=0.6847360912981455
13
-package.editor.height=473
12
+package.divider.vertical=0.6849925705794948
13
+package.editor.height=454
14 14
 package.editor.width=493
15 15
 package.editor.x=35
16
-package.editor.y=60
17
-package.frame.height=759
16
+package.editor.y=23
17
+package.frame.height=731
18 18
 package.frame.width=619
19 19
 package.numDependencies=1
20 20
 package.numTargets=2
@@ -27,16 +27,16 @@ readme.width=47
27 27
 readme.x=10
28 28
 readme.y=10
29 29
 target1.height=50
30
-target1.name=Console
30
+target1.name=Calculator
31 31
 target1.showInterface=false
32 32
 target1.type=ClassTarget
33
-target1.width=80
34
-target1.x=80
35
-target1.y=200
33
+target1.width=120
34
+target1.x=70
35
+target1.y=70
36 36
 target2.height=50
37
-target2.name=MainApplication
37
+target2.name=MathMethods
38 38
 target2.showInterface=false
39 39
 target2.type=ClassTarget
40
-target2.width=120
41
-target2.x=70
42
-target2.y=70
40
+target2.width=110
41
+target2.x=80
42
+target2.y=200