Quellcode durchsuchen

Notes are in the code

rayskeez21 vor 6 Jahren
Ursprung
Commit
042df86987
5 geänderte Dateien mit 203 neuen und 124 gelöschten Zeilen
  1. 14
    2
      Console.java
  2. 11
    8
      CoreFunctions.java
  3. 103
    65
      MainApplication.java
  4. 59
    33
      ScientificFunctions.java
  5. 16
    16
      package.bluej

+ 14
- 2
Console.java Datei anzeigen

@@ -1,4 +1,7 @@
1 1
 import java.util.Scanner;
2
+//Changed all int to double. If a decimal was entered first, it would
3
+    //create an error.
4
+
2 5
 
3 6
 public class Console {
4 7
 
@@ -23,8 +26,17 @@ public class Console {
23 26
         int userInput = scanner.nextInt();
24 27
         return userInput;
25 28
     }
26
-
29
+    //Created a new user input for the doubles.
27 30
     public static Double getDoubleInput(String prompt) {
28
-        return null;
31
+        Scanner scanner = new Scanner(System.in);
32
+        println(prompt);
33
+        double userInput = scanner.nextDouble();
34
+        return userInput;
29 35
     }
36
+    
37
+    
38
+    
39
+    
40
+    
41
+    
30 42
 }

+ 11
- 8
CoreFunctions.java Datei anzeigen

@@ -1,26 +1,27 @@
1
-
2
-public class CoreFunctions
1
+//Changed all int to double. If a decimal was entered first, it would
2
+    //create an error.
3
+public class CoreFunctions 
3 4
 {
4 5
     int display = 0;
5 6
     int memory = 0;
6 7
 
7
-    public static int add(int display, int entered) {
8
+    public static double add(double display, double entered) {
8 9
         System.out.println(display + entered);
9 10
         return display = display + entered;
10 11
 
11 12
     }
12 13
 
13
-    public static int subtract(int display, int entered) {
14
+    public static double subtract(double display, double entered) {
14 15
         System.out.println( display - entered);
15 16
         return display = display - entered;
16 17
     }
17 18
 
18
-    public static int multiply(int display, int entered) {
19
+    public static double multiply(double display, double entered) {
19 20
         System.out.println( display * entered);
20 21
         return display = display * entered;
21 22
     }
22 23
 
23
-    public static int divide(int display, int entered) {
24
+    public static double divide(double display, double entered) {
24 25
         if(entered != 0){
25 26
             System.out.println( display / entered);
26 27
             return display = display / entered;
@@ -31,9 +32,11 @@ public class CoreFunctions
31 32
         }
32 33
     }
33 34
 
34
-    public static int exp(int display, int entered) {
35
+    public static double exp(double display, double entered) {
35 36
         System.out.println( Math.pow(display,entered));
36
-        return display = (int)Math.pow(display,entered);
37
+        return display = (double)Math.pow(display,entered);
37 38
     }
38 39
 
40
+    
41
+
39 42
 }

+ 103
- 65
MainApplication.java Datei anzeigen

@@ -1,81 +1,119 @@
1 1
 
2 2
 public class MainApplication  {
3
-    int display = 0;
4
-    int memory = 0;
3
+    double display = 0;
4
+    //Changed all int to double. If a decimal was entered first, it would
5
+    //create an error.
5 6
    
7
+    
8
+    
9
+
6 10
     public static void main(String[] args) {
7 11
         Console.println("Welcome to Vince and Ray's Calculator!");
8
-        Integer display = Console.getIntegerInput("Enter your first integer: ");
9 12
         while(true){
10
-            String operator = Console.getStringInput("Enter the operation you'd like to perform or type\n'help' to see a list of commands.");
11
-            if(operator.equalsIgnoreCase("Add")){
12
-                Integer entered = Console.getIntegerInput("Enter your second integer: ");
13
-                CoreFunctions.add(display, entered);
14
-                display = display + entered;
15
-            }
16
-            if(operator.equalsIgnoreCase("Subtract")){
17
-                Integer entered = Console.getIntegerInput("Enter your second integer: ");
18
-                CoreFunctions.subtract(display, entered);
19
-                display = display - entered;
20
-            }
21
-            if(operator.equalsIgnoreCase("Multiply")){
22
-                Integer entered = Console.getIntegerInput("Enter your second integer: ");
23
-                CoreFunctions.multiply(display, entered);
24
-                display = display * entered;
25
-            }
26
-            if(operator.equalsIgnoreCase("Divide")){
27
-                Integer entered = Console.getIntegerInput("Enter your second integer: ");
28
-                if(entered != 0){
29
-                    CoreFunctions.divide(display, entered);
30
-                    display = display / entered;
31
-                }else { 
32
-                    System.out.println("Err");
13
+            //Created a try catch methtod to catch all errors of the loop.
14
+            //and tell you where the error origin.
15
+            try{
16
+                Double display = Console.getDoubleInput("Enter your first number: ");
17
+                String operator = Console.getStringInput("Enter the operation you'd like to perform or type\n'help' to see a list of commands.");
18
+                if(operator.equalsIgnoreCase("Add")){
19
+                    Integer entered = Console.getIntegerInput("Enter your second integer: ");
20
+                    CoreFunctions.add(display, entered);
21
+                    display = display + entered;
33 22
                 }
23
+                if(operator.equalsIgnoreCase("Subtract")){
24
+                    
25
+                    Integer entered = Console.getIntegerInput("Enter your second integer: ");
26
+                    CoreFunctions.subtract(display, entered);
27
+                    display = display - entered;
28
+                }
29
+                if(operator.equalsIgnoreCase("Multiply")){
30
+                    Integer entered = Console.getIntegerInput("Enter your second integer: ");
31
+                    CoreFunctions.multiply(display, entered);
32
+                    display = display * entered;
33
+                }
34
+                if(operator.equalsIgnoreCase("Divide")){
35
+                    Double entered = Console.getDoubleInput("Enter your second integer: ");
36
+                    if(entered != 0){
37
+                        CoreFunctions.divide(display, entered);
38
+                        display = display / entered;
39
+                    }else { 
40
+                        System.out.println("Err");
41
+                    }
42
+                }
43
+                if(operator.equalsIgnoreCase("Exponent")){ 
44
+                    Double entered = Console.getDoubleInput("Enter your second integer: ");
45
+                    CoreFunctions.exp(display, entered);
46
+                    display = (double) Math.pow(display,entered);
47
+                }
48
+                if(operator.equalsIgnoreCase("squared")){
49
+                    ScientificFunctions.sqr(display);
50
+                    display = display*display;
51
+                }
52
+                if(operator.equalsIgnoreCase("square root")){
53
+                    ScientificFunctions.sqRoot(display);
54
+                    display = ((double)Math.sqrt(display));
55
+                }
56
+                if(operator.equalsIgnoreCase("Inverse Fraction")){
57
+                    ScientificFunctions.invFraction(display);
58
+                    display = 1/display;
59
+                }
60
+                if(operator.equalsIgnoreCase("Inverse Sign")){
61
+                    ScientificFunctions.invSign(display);
62
+                    display = -1/display;
63
+                }
64
+                if(operator.equalsIgnoreCase("sine")){
65
+                    ScientificFunctions.sine(display);
66
+                    display = (double)Math.sin(display);
67
+                }
68
+                if(operator.equalsIgnoreCase("cosine")){
69
+                    ScientificFunctions.cosine(display);
70
+                    display = (double)Math.cos(display);
71
+                }
72
+                if(operator.equalsIgnoreCase("tangent")){
73
+                    ScientificFunctions.tan(display);
74
+                    display = (double)Math.tan(display);
75
+                }
76
+                if(operator.equalsIgnoreCase("inverse tangent")){
77
+                    ScientificFunctions.atan(display);
78
+                    display = (double)Math.atan(display);
79
+                }
80
+                if(operator.equalsIgnoreCase("inverse sin")){
81
+                    ScientificFunctions.asin(display);
82
+                    display = (double)Math.asin(display);
83
+                }
84
+                if(operator.equalsIgnoreCase("inverse cosine")){
85
+                    ScientificFunctions.acosin(display);
86
+                    display = (double)Math.acos(display);
87
+                }
88
+                if(operator.equalsIgnoreCase("M+")){
89
+                    ScientificFunctions.setMem(display);
90
+                }
91
+                if(operator.equalsIgnoreCase("MC")){
92
+                    ScientificFunctions.resetMem(display);
93
+                }
94
+                if(operator.equalsIgnoreCase("MRC")){
95
+                    ScientificFunctions.recallMem(display);
96
+                }
97
+                if(operator.equalsIgnoreCase("clear")){
98
+                    System.out.print('\u000C');
99
+                }
100
+                if(operator.equalsIgnoreCase("exit")){
101
+                    System.out.println("Thanks for using our Calculator!");    
102
+                    break;
103
+                }
104
+
105
+            }catch(Exception e){
106
+                System.err.println(e);
107
+                System.err.println("System Error: Try again");
108
+                continue;
109
+
34 110
             }
35
-            if(operator.equalsIgnoreCase("Exponent")){
36
-                Integer entered = Console.getIntegerInput("Enter your second integer: ");
37
-                CoreFunctions.exp(display, entered);
38
-                display = (int)Math.pow(display,entered);
39
-            }
40
-            if(operator.equalsIgnoreCase("squared")){
41
-                ScientificFunctions.sqr(display);
42
-                display = display*display;
43
-            }
44
-            if(operator.equalsIgnoreCase("square root")){
45
-                ScientificFunctions.sqRoot(display);
46
-                display = ((int)Math.sqrt(display));
47
-            }
48
-            if(operator.equalsIgnoreCase("Inverse Fraction")){
49
-                ScientificFunctions.invFraction(display);
50
-                display = 1/display;
51
-            }
52
-            if(operator.equalsIgnoreCase("Inverse Sign")){
53
-                ScientificFunctions.invSign(display);
54
-                display = -1/display;
55
-            }
56
-            if(operator.equalsIgnoreCase("sine")){
57
-                ScientificFunctions.sine(display);
58
-                display = (int)Math.sin(display);
59
-            }
60
-            if(operator.equalsIgnoreCase("cosine")){
61
-                ScientificFunctions.cosine(display);
62
-                display = (int)Math.cos(display);
63
-            }
64
-            if(operator.equalsIgnoreCase("tangent")){
65
-                ScientificFunctions.tan(display);
66
-                display = (int)Math.tan(display);
67
-            }
68
-            if(operator.equalsIgnoreCase("exit")){
69
-                System.out.println("Thanks for using our Calculator!");    
70
-                break;
71
-            }
72
-        }
73 111
 
112
+        }
74 113
     }
75 114
 
76 115
 }
77 116
 
78
-
79 117
 //Integer i = Console.getIntegerInput("Enter an integer");
80 118
 //Double d = Console.getDoubleInput("Enter a double.");
81 119
 

+ 59
- 33
ScientificFunctions.java Datei anzeigen

@@ -1,72 +1,98 @@
1
-
2
-public class ScientificFunctions
1
+//Changed all int to double. If a decimal was entered first, it would
2
+    //create an error.
3
+public class ScientificFunctions 
3 4
 {
4 5
     
5 6
     int display = 0;
6
-    int memory = 0;
7
+    private static double memory = 0;
8
+    
7 9
     
8
-    public static int sqr(int display) {
10
+    public static double sqr(double display) {
9 11
         System.out.println( display*display);
10 12
         return display = display*display;
11 13
     }
12 14
 
13
-    public static int sqRoot(int display) {
15
+    public static double sqRoot(double display) {
14 16
         System.out.println( Math.sqrt(display));
15
-        return display = ((int)Math.sqrt(display));
17
+        return display = ((double)Math.sqrt(display));
16 18
     }
17 19
 
18
-    public static int invFraction(int display) {
20
+    public static double invFraction(double display) {
19 21
         System.out.println( 1/display);
20 22
         return display = 1/display;
21 23
     }
22 24
 
23
-    public static int invSign(int display) {
25
+    public static double invSign(double display) {
24 26
         System.out.println( -1*display);
25 27
         return display = -1/display;
26 28
     }
27 29
 
28
-    public void setMem(int display) {
30
+    public static void setMem(double display) {
31
+        memory = display;
29 32
         System.out.println("Set to memory!");
30
-        display = memory;
33
+        
31 34
     }
32 35
 
33
-    public void resetMem(int display) {
36
+    public static void resetMem(double display) {
37
+        int memory;
34 38
         System.out.println("Reset memory!");
35 39
         memory = 0;
36 40
     }
37 41
 
38
-    public void recallMem(int display) {
39
-        System.out.println("Memory recalled!");
40
-        memory = display;
42
+    public static double recallMem(double display) {
43
+        return (double)memory;
44
+        //System.out.println("Memory recalled!");
45
+        
41 46
     }
42
-
43
-    public static int cosine(int display) {
44
-        System.out.println(Math.cos(display));
45
-        return display = (int)Math.cos(display);
47
+    //Fixed trig functions to convert to radians before printing the equation.
48
+    
49
+    public static double cosine(double display) {
50
+        double degrees = display;
51
+        double radians = Math.toRadians(degrees);
52
+        double cosValue = Math.sin(radians);
53
+        System.out.println("cos(" + degrees + ") = " + cosValue);
54
+        return display = (double)Math.cos(display);
46 55
     }
47 56
 
48
-    public static int sine(int display) {
49
-        System.out.println(sine(display));
50
-        return display = (int)Math.sin(display);
57
+    public static double sine(double display) {
58
+        double degrees = display;
59
+        double radians = Math.toRadians(degrees);
60
+        double sineValue = Math.sin(radians);
61
+        System.out.println("sin(" + degrees + ") = " + sineValue);
62
+        return display = (double)Math.sin(display);
51 63
     }
52 64
 
53
-    public static int tan(int display) {
54
-        System.out.println(tan(display));
55
-        return display = (int)Math.tan(display);
65
+    public static double tan(double display) {
66
+        double degrees = display;
67
+        double radians = Math.toRadians(degrees);
68
+        double tanValue = Math.sin(radians);
69
+        System.out.println("tan(" + degrees + ") = " + tanValue);
70
+        return display = (double)Math.tan(display);
56 71
     }
57 72
     
58
-    public static int acosine(int display) {
59
-        System.out.println(Math.acos(display));
60
-        return display = (int)Math.acos(display);
73
+    public static double acosin(double display) {
74
+        double degrees = display;
75
+        double radians = Math.toRadians(degrees);
76
+        double acosValue = Math.sin(radians);
77
+        System.out.println("acos(" + degrees + ") = " + acosValue);
78
+        return display = (double)Math.acos(display);
61 79
     }
62 80
     
63
-    public static int asin(int display) {
64
-        System.out.println(asin(display));
65
-        return display = (int)Math.asin(display);
81
+    public static double asin(double display) {
82
+        double degrees = display;
83
+        double radians = Math.toRadians(degrees);
84
+        double asinValue = Math.sin(radians);
85
+        System.out.println("asin(" + degrees + ") = " + asinValue);
86
+        return display = (double)Math.asin(display);
66 87
     }
67 88
     
68
-    public static int atan(int display) {
69
-        System.out.println(atan(display));
70
-        return display = (int)Math.atan(display);
89
+    public static double atan(double display) {
90
+        double degrees = display;
91
+        double radians = Math.toRadians(degrees);
92
+        double atanValue = Math.sin(radians);
93
+        System.out.println("atan(" + degrees + ") = " + atanValue);
94
+        return display = (double)Math.atan(display);
71 95
     }
96
+    
97
+    
72 98
 }

+ 16
- 16
package.bluej Datei anzeigen

@@ -10,18 +10,18 @@ dependency3.to=ScientificFunctions
10 10
 dependency3.type=UsesDependency
11 11
 editor.fx.0.height=709
12 12
 editor.fx.0.width=800
13
-editor.fx.0.x=307
14
-editor.fx.0.y=23
13
+editor.fx.0.x=375
14
+editor.fx.0.y=29
15 15
 objectbench.height=198
16
-objectbench.width=352
17
-package.divider.horizontal=0.5993322203672788
16
+objectbench.width=510
17
+package.divider.horizontal=0.5675082327113062
18 18
 package.divider.vertical=0.685099846390169
19 19
 package.editor.height=439
20
-package.editor.width=493
21
-package.editor.x=35
22
-package.editor.y=23
20
+package.editor.width=805
21
+package.editor.x=192
22
+package.editor.y=38
23 23
 package.frame.height=709
24
-package.frame.width=619
24
+package.frame.width=931
25 25
 package.numDependencies=3
26 26
 package.numTargets=4
27 27
 package.showExtends=true
@@ -37,26 +37,26 @@ target1.name=ScientificFunctions
37 37
 target1.showInterface=false
38 38
 target1.type=ClassTarget
39 39
 target1.width=140
40
-target1.x=160
41
-target1.y=170
40
+target1.x=560
41
+target1.y=110
42 42
 target2.height=50
43 43
 target2.name=Console
44 44
 target2.showInterface=false
45 45
 target2.type=ClassTarget
46 46
 target2.width=80
47
-target2.x=310
48
-target2.y=50
47
+target2.x=560
48
+target2.y=0
49 49
 target3.height=50
50 50
 target3.name=MainApplication
51 51
 target3.showInterface=false
52 52
 target3.type=ClassTarget
53 53
 target3.width=120
54
-target3.x=70
55
-target3.y=70
54
+target3.x=190
55
+target3.y=50
56 56
 target4.height=50
57 57
 target4.name=CoreFunctions
58 58
 target4.showInterface=false
59 59
 target4.type=ClassTarget
60 60
 target4.width=120
61
-target4.x=20
62
-target4.y=170
61
+target4.x=560
62
+target4.y=220