Khalil Saboor 6 jaren geleden
bovenliggende
commit
35c18d574d
5 gewijzigde bestanden met toevoegingen van 221 en 83 verwijderingen
  1. 6
    6
      BasicMath.java
  2. 10
    6
      Console.java
  3. 173
    2
      MainApplication.java
  4. 8
    8
      TrigFunctions.java
  5. 24
    61
      package.bluej

+ 6
- 6
BasicMath.java Bestand weergeven

@@ -13,22 +13,22 @@ public class BasicMath
13 13
    public static double add(double a,double b) {  
14 14
             return a + b;
15 15
         }
16
-    public static double Subtract(double a,double b){
16
+   public static double Subtract(double a,double b){
17 17
             return a - b;
18 18
         }
19
-    public static double multiply(double a,double b){
19
+   public static double multiply(double a,double b){
20 20
             return a * b;
21 21
         }
22
-    public static double divide(double a,double b){
22
+   public static double divide(double a,double b){
23 23
             return a / b;
24 24
         }
25
-    public static double power(double a, double b){
25
+   public static double power(double a, double b){
26 26
             return Math.pow(a,b);
27 27
         } 
28
-    public static double squareRoot(double a){
28
+   public static double squareRoot(double a){
29 29
             return Math.sqrt(a);
30 30
         }   
31
-    public static double inverse(double a){
31
+   public static double inverse(double a){
32 32
         return 1/a;
33 33
     }
34 34
 }

+ 10
- 6
Console.java Bestand weergeven

@@ -1,10 +1,8 @@
1
- 
2
-
3
-import java.util.Scanner;
4
-
5 1
 /**
6 2
  * Created by leon on 2/9/18.
7 3
  */
4
+import java.util.Scanner;
5
+
8 6
 public class Console {
9 7
 
10 8
     public static void print(String output, Object... args) {
@@ -23,10 +21,16 @@ public class Console {
23 21
     }
24 22
 
25 23
     public static Integer getIntegerInput(String prompt) {
26
-        return null;
24
+        Scanner scanner = new Scanner(System.in);
25
+        println(prompt);
26
+        int userInput = scanner.nextInt();
27
+        return userInput;
27 28
     }
28 29
 
29 30
     public static Double getDoubleInput(String prompt) {
30
-        return null;
31
+        Scanner scanner = new Scanner(System.in);
32
+        println(prompt);
33
+        double userInput = scanner.nextDouble();
34
+        return userInput;
31 35
     }
32 36
 }

+ 173
- 2
MainApplication.java Bestand weergeven

@@ -1,17 +1,188 @@
1
- 
1
+import java.util.*;
2 2
 
3 3
 /**
4 4
  * Created by leon on 2/9/18.
5 5
  */
6 6
 public class MainApplication {
7 7
     public static void main(String[] args) {
8
+
9
+        double value = 0;//answer
10
+        double memoryValue = 0;
11
+
8 12
         Console.println("Welcome to my calculator!");
13
+        /*
9 14
         String s = Console.getStringInput("Enter a string");
10 15
         Integer i = Console.getIntegerInput("Enter an integer");
11 16
         Double d = Console.getDoubleInput("Enter a double.");
12 17
 
13 18
         Console.println("The user input %s as a string", s);
14 19
         Console.println("The user input %s as a integer", i);
15
-        Console.println("The user input %s as a d", d);
20
+        Console.println("The user input %s as a double", d);
21
+        `*/
22
+        Scanner input = new Scanner(System.in);
23
+        // Calculator calc = new ....
24
+        while (true) {
25
+            System.out.print("Enter a command: ");
26
+            String command = input.next();
27
+            if (command.equalsIgnoreCase("exit"))  {
28
+                Console.println("Thanks for calculating with us!");
29
+                break;
30
+            }
31
+            if (command.equalsIgnoreCase("add"))  {
32
+                // read first number
33
+                // read second number
34
+                // call the add method on calc and display
35
+                double x = Console.getDoubleInput("Input your first number");
36
+                double y = Console.getDoubleInput("Input your second number");
37
+                double answer = BasicMath.add(x,y);
38
+                value = answer;
39
+                Console.println(String.valueOf(answer));
40
+            }
41
+            if(command.equalsIgnoreCase("Subtract")){
42
+                double x = Console.getDoubleInput("Input your first number");
43
+                double y = Console.getDoubleInput("Input your second number");
44
+                double answer = BasicMath.Subtract(x,y);
45
+                value = answer;
46
+                Console.println(String.valueOf(answer));
47
+            }
48
+            if (command.equalsIgnoreCase("multiply"))  {
49
+                double x = Console.getDoubleInput("Input your first number");
50
+                double y = Console.getDoubleInput("Input your second number");
51
+                double answer = BasicMath.multiply(x,y);
52
+                value = answer;
53
+                Console.println(String.valueOf(answer));
54
+            }
55
+            if (command.equalsIgnoreCase("divide"))  {
56
+                // call the square method on calc and display
57
+                double x = Console.getDoubleInput("Input your first number");
58
+                double y = Console.getDoubleInput("Input your second number");
59
+                double answer = BasicMath.divide(x,y);
60
+                value = answer; 
61
+                Console.println(String.valueOf(answer));
62
+            }
63
+            if (command.equalsIgnoreCase("power"))  {
64
+                // call the square method on calc and display
65
+                double x = Console.getDoubleInput("Input your first number");
66
+                double y = Console.getDoubleInput("Input your exponent");
67
+                double answer = BasicMath.power(x,y);
68
+                value = answer; 
69
+                Console.println(String.valueOf(answer));
70
+            }
71
+            if (command.equalsIgnoreCase("inverse"))  {
72
+                // call the square method on calc and display
73
+                double x = Console.getDoubleInput("Input your number");
74
+                double answer = BasicMath.inverse(x);
75
+                value = answer;
76
+                Console.println(String.valueOf(answer));
77
+            }
78
+            if (command.equalsIgnoreCase("square"))  {
79
+                // read first number
80
+                // call the method on calc and display
81
+                double x = Console.getDoubleInput("Input your number");
82
+                double answer = BasicMath.squareRoot(x);
83
+                value = answer;
84
+                Console.println(String.valueOf(answer));
85
+            }
86
+            if (command.equalsIgnoreCase("hex"))  {
87
+                int no = (int)value;
88
+                String hex = Integer.toHexString(no);
89
+                Console.println(hex);
90
+            }
91
+            if (command.equalsIgnoreCase("binary"))  {
92
+                int no = (int)value;
93
+                String binary = Integer.toBinaryString(no);
94
+                Console.println(binary);
95
+            }
96
+            if(command.equalsIgnoreCase("octal")){
97
+                int no = (int)value;
98
+                String octal = Integer.toOctalString(no);
99
+                Console.println(octal);
100
+            }
101
+
102
+            if (command.equalsIgnoreCase("sin"))  {
103
+                double x = Console.getDoubleInput("Input your number");
104
+                double answer = TrigFunctions.sin(x);
105
+                value = answer;
106
+                Console.println(String.valueOf(answer));
107
+            }
108
+
109
+            if (command.equalsIgnoreCase("cos"))  {
110
+                double x = Console.getDoubleInput("Input your number");
111
+                double answer = TrigFunctions.cos(x);
112
+                value = answer;
113
+                Console.println(String.valueOf(answer));
114
+            }
115
+            if (command.equalsIgnoreCase("tan"))  {
116
+                double x = Console.getDoubleInput("Input your number");
117
+                double answer = TrigFunctions.tan(x);
118
+                value = answer;
119
+                Console.println(String.valueOf(answer));
120
+            }
121
+            if (command.equalsIgnoreCase("invsin"))  {
122
+                double x = Console.getDoubleInput("Input your number");
123
+                double answer = TrigFunctions.invsin(x);
124
+                value = answer;
125
+                Console.println(String.valueOf(answer));
126
+            }
127
+            if (command.equalsIgnoreCase("invcos"))  {
128
+                double x = Console.getDoubleInput("Input your number");
129
+                double answer = TrigFunctions.invcos(x);
130
+                value = answer;
131
+                Console.println(String.valueOf(answer));
132
+            }
133
+            if (command.equalsIgnoreCase("invtan"))  {
134
+                double x = Console.getDoubleInput("Input your number");
135
+                double answer = TrigFunctions.invtan(x);
136
+                value = answer;
137
+                Console.println(String.valueOf(answer));
138
+            }
139
+            if (command.equalsIgnoreCase("deg"))  {
140
+                double x = Console.getDoubleInput("Input your number");
141
+                double answer = TrigFunctions.deg(x);
142
+                value = answer;
143
+                Console.println(String.valueOf(answer));
144
+            }
145
+            if (command.equalsIgnoreCase("rad"))  {
146
+                double x = Console.getDoubleInput("Input your number");
147
+                double answer = TrigFunctions.rad(x);
148
+                value = answer;
149
+                Console.println(String.valueOf(answer));
150
+            }
151
+            if(command.equalsIgnoreCase("negpos")){
152
+                if(value > 0){
153
+                    value = value * (-1);
154
+                    Console.println(String.valueOf(value));
155
+                }
156
+                else{
157
+                    Console.println("ERROR");
158
+                }
159
+            }
160
+            if(command.equalsIgnoreCase("clear")){
161
+                value = 0;
162
+                Console.println(String.valueOf(value));
163
+            }
164
+            if(command.equalsIgnoreCase("memory")){
165
+                memoryValue = Console.getDoubleInput("Input the number you want to save:");
166
+                Console.println("SAVED!!!");
167
+                Console.println(String.valueOf(0));
168
+            }// and on and on
169
+            if(command.equalsIgnoreCase("recall")){
170
+                Console.println(String.valueOf(memoryValue));
171
+            }
172
+            // Pythagorean theorem
173
+            if(command.equalsIgnoreCase("custom1")){
174
+                double x = Console.getDoubleInput("Input your first number");
175
+                double y = Console.getDoubleInput("Input your exponent");
176
+                double answer = Math.sqrt(Math.pow(x, 2)+ Math.pow(y, 2));
177
+                value = answer; 
178
+                Console.println(String.valueOf(value));
179
+            }
180
+            if(command.equalsIgnoreCase("PI")){
181
+                value = Math.PI;
182
+                Console.println("PIEEEEEEEEEEEEEEEEEEEEE");
183
+                Console.println(String.valueOf(value));
184
+            }
185
+        }
186
+
16 187
     }
17 188
 }

+ 8
- 8
TrigFunctions.java Bestand weergeven

@@ -22,43 +22,43 @@ public class TrigFunctions
22 22
         ;
23 23
     }
24 24
 
25
-    public double sin(int number){
25
+    public static double sin(double number){
26 26
      double value = Math.sin(number);
27 27
      return value;
28 28
     }
29 29
     
30 30
     
31
-    public double cos(int number){
31
+    public static double cos(double number){
32 32
         double value = Math.cos(number);
33 33
         return value;
34 34
     }
35 35
     
36
-    public double tan(int number){
36
+    public static double tan(double number){
37 37
         double value = Math.tan(number);
38 38
         return value;   
39 39
     }
40 40
     
41
-    public double invsin(int number){
41
+    public static double invsin(double number){
42 42
         double value = Math.asin(number);
43 43
         return value;
44 44
     }
45 45
     
46
-    public double invcos(int number){
46
+    public static double invcos(double number){
47 47
         double value = Math.acos(number);
48 48
         return value;
49 49
     }
50 50
     
51
-    public double invtan(int number){
51
+    public static double invtan(double number){
52 52
         double value = Math.atan(number);
53 53
         return value;   
54 54
     }
55 55
     
56
-    public double deg(int number){
56
+    public static double deg(double number){
57 57
         double value = Math.toDegrees(number);
58 58
         return value;
59 59
     }
60 60
     
61
-    public double rad(int number){
61
+    public static double rad(double number){
62 62
         double value = Math.toRadians(number);
63 63
         return value;   
64 64
     }

+ 24
- 61
package.bluej Bestand weergeven

@@ -2,43 +2,28 @@
2 2
 dependency1.from=BasicMathTest
3 3
 dependency1.to=BasicMath
4 4
 dependency1.type=UsesDependency
5
-<<<<<<< HEAD
6
-dependency2.from=BasicMathTest
7
-dependency2.to=BasicMath
5
+dependency2.from=TrigFunctionsTest
6
+dependency2.to=TrigFunctions
8 7
 dependency2.type=UsesDependency
9
-editor.fx.0.height=0
10
-editor.fx.0.width=0
11
-editor.fx.0.x=0
12
-editor.fx.0.y=0
13
-=======
14
-dependency2.from=MainApplication
15
-dependency2.to=Console
16
-dependency2.type=UsesDependency
17
-dependency3.from=TrigFunctionsTest
18
-dependency3.to=TrigFunctions
8
+dependency3.from=MainApplication
9
+dependency3.to=Console
19 10
 dependency3.type=UsesDependency
20 11
 editor.fx.0.height=722
21 12
 editor.fx.0.width=800
22 13
 editor.fx.0.x=155
23 14
 editor.fx.0.y=23
24
->>>>>>> 7d1ea52de1c614ccaeb20bec6a6c3ced4ce15339
25 15
 objectbench.height=202
26
-objectbench.width=352
27
-package.divider.horizontal=0.5993322203672788
16
+objectbench.width=536
17
+package.divider.horizontal=0.56153050672182
28 18
 package.divider.vertical=0.6852409638554217
29 19
 package.editor.height=448
30
-package.editor.width=493
20
+package.editor.width=861
31 21
 package.editor.x=35
32 22
 package.editor.y=23
33 23
 package.frame.height=722
34
-package.frame.width=619
35
-<<<<<<< HEAD
36
-package.numDependencies=2
37
-package.numTargets=5
38
-=======
24
+package.frame.width=987
39 25
 package.numDependencies=3
40 26
 package.numTargets=7
41
->>>>>>> 7d1ea52de1c614ccaeb20bec6a6c3ced4ce15339
42 27
 package.showExtends=true
43 28
 package.showUses=true
44 29
 project.charset=UTF-8
@@ -53,72 +38,50 @@ target1.name=BasicMath
53 38
 target1.showInterface=false
54 39
 target1.type=ClassTarget
55 40
 target1.width=90
56
-<<<<<<< HEAD
57
-target1.x=320
58
-target1.y=130
59
-=======
60
-target1.x=220
61
-target1.y=160
62
->>>>>>> 7d1ea52de1c614ccaeb20bec6a6c3ced4ce15339
41
+target1.x=270
42
+target1.y=120
63 43
 target2.height=50
64 44
 target2.name=MainApplicationTest
65 45
 target2.showInterface=false
66 46
 target2.type=UnitTestTargetJunit4
67 47
 target2.width=120
68
-target2.x=100
69
-target2.y=40
48
+target2.x=60
49
+target2.y=80
70 50
 target3.height=50
71 51
 target3.name=Console
72 52
 target3.showInterface=false
73 53
 target3.type=ClassTarget
74 54
 target3.width=80
75
-target3.x=80
76
-target3.y=200
77
-<<<<<<< HEAD
78
-target4.height=50
79
-target4.name=BasicMathTest
80
-target4.showInterface=false
81
-target4.type=UnitTestTargetJunit4
82
-target4.width=90
83
-target4.x=350
84
-target4.y=100
85
-target5.association=MainApplicationTest
86
-target5.height=50
87
-target5.name=MainApplication
88
-target5.showInterface=false
89
-target5.type=ClassTarget
90
-target5.width=120
91
-target5.x=70
92
-target5.y=70
93
-=======
55
+target3.x=250
56
+target3.y=180
94 57
 target4.association=TrigFunctionsTest
95 58
 target4.height=50
96 59
 target4.name=TrigFunctions
97 60
 target4.showInterface=false
98 61
 target4.type=ClassTarget
99 62
 target4.width=110
100
-target4.x=40
101
-target4.y=300
63
+target4.x=50
64
+target4.y=240
65
+target5.association=MainApplicationTest
102 66
 target5.height=50
103 67
 target5.name=BasicMathTest
104 68
 target5.showInterface=false
105 69
 target5.type=UnitTestTargetJunit4
106
-target5.width=120
107
-target5.x=330
108
-target5.y=170
70
+target5.width=90
71
+target5.x=300
72
+target5.y=90
109 73
 target6.height=50
110 74
 target6.name=TrigFunctionsTest
111 75
 target6.showInterface=false
112 76
 target6.type=UnitTestTargetJunit4
113 77
 target6.width=110
114
-target6.x=70
115
-target6.y=270
78
+target6.x=80
79
+target6.y=210
116 80
 target7.association=MainApplicationTest
117 81
 target7.height=50
118 82
 target7.name=MainApplication
119 83
 target7.showInterface=false
120 84
 target7.type=ClassTarget
121 85
 target7.width=120
122
-target7.x=70
123
-target7.y=70
124
->>>>>>> 7d1ea52de1c614ccaeb20bec6a6c3ced4ce15339
86
+target7.x=30
87
+target7.y=110