Jarrett Samuels преди 6 години
родител
ревизия
d97080d1bf
променени са 11 файла, в които са добавени 600 реда и са изтрити 71 реда
  1. BIN
      .DS_Store
  2. 0
    32
      Console.java
  3. 78
    0
      DisplayVal.java
  4. 0
    17
      MainApplication.java
  5. 18
    0
      Memory.java
  6. 29
    0
      NumSystemsConverter.java
  7. 243
    0
      Operations.java
  8. 0
    0
      README.md
  9. 98
    0
      SciCalculator.java
  10. 74
    0
      Units.java
  11. 60
    22
      package.bluej

BIN
.DS_Store Целия файл


+ 0
- 32
Console.java Целия файл

@@ -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
-}

+ 78
- 0
DisplayVal.java Целия файл

@@ -0,0 +1,78 @@
1
+import java.util.Scanner;
2
+import java.lang.*;
3
+
4
+public class DisplayVal{
5
+
6
+  private double value = 0;
7
+  private String numSys = "Decimal";
8
+  private String disVal = "0";
9
+  Scanner stdin = new Scanner(System.in);
10
+
11
+  NumSystemsConverter conv = new NumSystemsConverter();
12
+
13
+
14
+  //Scanner stdin = new Scanner(System.in);
15
+
16
+  public double getValue(){
17
+     return this.value;
18
+  }
19
+  public void setValue(double num){
20
+    this.value = num;
21
+  }
22
+
23
+  public void setNumSys(){
24
+    System.out.println("Select number system to use: (Enter # 1-4)");
25
+    System.out.println("(1)Decimal");
26
+    System.out.println("(2)Binary");
27
+    System.out.println("(3)Octal");
28
+    System.out.println("(4)Hexadecimal");
29
+    System.out.print("> ");
30
+    int num = stdin.nextInt();
31
+
32
+    switch(num){
33
+      case 1:
34
+              this.numSys = "Decimal";
35
+            break;
36
+      case 2:
37
+              this.numSys = "Binary";
38
+            break;
39
+      case 3:
40
+              this.numSys = "Octal";
41
+            break;
42
+      case 4:
43
+              this.numSys = "Hexadecimal";
44
+            break;
45
+  }
46
+  this.setDisVal(conv.updateDisplayNumber(this.value,num)); //not working
47
+}
48
+
49
+public void toggleNumSys(){
50
+  if(this.numSys == "Decimal"){
51
+    this.numSys = "Binary";
52
+  } else if(this.numSys == "Binary"){
53
+    this.numSys = "Octal";
54
+  } else if(this.numSys == "Octal"){
55
+    this.numSys = "Hexadecimal";
56
+  } else if(this.numSys == "Hexadecimal"){
57
+    this.numSys = "Decimal";
58
+  } else {
59
+    this.numSys = "Decimal";
60
+  }
61
+
62
+}
63
+
64
+public String getNumSys(){
65
+  return this.numSys;
66
+}
67
+
68
+public void setDisVal(String value){
69
+  this.disVal = value;
70
+}
71
+
72
+
73
+  public String valToString(double num){
74
+    String displayStr = new String();
75
+    displayStr += "" + num;
76
+    return displayStr;
77
+  }
78
+}

+ 0
- 17
MainApplication.java Целия файл

@@ -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
-}

+ 18
- 0
Memory.java Целия файл

@@ -0,0 +1,18 @@
1
+import java.util.Scanner;
2
+import java.lang.*;
3
+
4
+public class Memory{
5
+
6
+  Scanner stdin = new Scanner(System.in);
7
+  private double memNum = 0;
8
+
9
+  public double getMemNum(){
10
+     return this.memNum;
11
+  }
12
+  public void setMemNum(){
13
+    System.out.println("Enter number to store to memory");
14
+    System.out.print("> ");
15
+    double num = stdin.nextDouble();
16
+    this.memNum = num;
17
+  }
18
+}

+ 29
- 0
NumSystemsConverter.java Целия файл

@@ -0,0 +1,29 @@
1
+public class NumSystemsConverter
2
+{
3
+    // display mode is sent along with displayNumber, could also be defined globally)
4
+    public String updateDisplayNumber(double displayNumber,int mode){
5
+        String str="";
6
+        switch(mode) {
7
+            case 1:{
8
+                str=Double.toString(displayNumber);
9
+                break;
10
+            }
11
+            case 2:{
12
+                str=Long.toBinaryString(Double.doubleToRawLongBits(displayNumber));
13
+                break;
14
+            }
15
+            case 3:{
16
+                str=Double.toHexString(displayNumber);
17
+                break;
18
+            }
19
+            case 4:{
20
+                double fraction = (displayNumber%1);
21
+                double integral = (displayNumber-fraction);
22
+                str=Integer.toOctalString((int)integral)+"."+Integer.toOctalString((int)(fraction+.5));
23
+                break;
24
+            }
25
+        }
26
+        return str;
27
+    }
28
+}
29
+

+ 243
- 0
Operations.java Целия файл

@@ -0,0 +1,243 @@
1
+import java.util.Scanner;
2
+import java.lang.*;
3
+
4
+public class Operations{
5
+
6
+  Scanner stdin = new Scanner(System.in);
7
+
8
+public double chooseOp(double num){
9
+
10
+  boolean isValid = false;
11
+  int choice;
12
+  double result = 0;
13
+
14
+  while(!isValid){
15
+    System.out.println("\n-------------------------------------------------");
16
+    System.out.println(num);
17
+    System.out.println("-------------------------------------------------");
18
+    System.out.println("Select type of operation to perform (enter # 1-3)");
19
+    System.out.println("(1) Core Functions");
20
+    System.out.println("(2) Trigonometry");
21
+    System.out.println("(3) Advanced Functions");
22
+    System.out.println("-------------------------------------------------");
23
+    System.out.print("> ");
24
+    choice = stdin.nextInt();
25
+
26
+    switch(choice){
27
+      case 1: result = coreFunc(num);
28
+              isValid = true;
29
+              break;
30
+      case 2: result = trigFunc(num);
31
+              isValid = true;
32
+              break;
33
+      case 3: result = advFunc(num);
34
+              isValid = true;
35
+              break;
36
+      default:
37
+              break;
38
+   }
39
+  }
40
+  return result;
41
+}
42
+
43
+public double coreFunc(double num){ //take displayNum as input parameter
44
+
45
+  boolean isValid = false;
46
+  int choice;
47
+  double entNum, result = 0;
48
+
49
+
50
+  while(!isValid){
51
+    System.out.println("\n-------------------------------------------------");
52
+    System.out.println(num);
53
+    System.out.println("-------------------------------------------------");
54
+    System.out.println("Select core function to perform (enter # 1-9)");
55
+    System.out.println("(1) Add");
56
+    System.out.println("(2) Subtract");
57
+    System.out.println("(3) Multiply");
58
+    System.out.println("(4) Divide");
59
+    System.out.println("(5) Square");
60
+    System.out.println("(6) Square Root");
61
+    System.out.println("(7) Exponentiation");
62
+    System.out.println("(8) Inverse (1/x)");
63
+    System.out.println("(9) Change sign (+/-)");
64
+    System.out.println("---------------------------------------------");
65
+    System.out.print("> ");
66
+    choice = stdin.nextInt();
67
+
68
+    switch(choice){
69
+      case 1: System.out.println("Enter number to add to " + num + ": ");
70
+              System.out.print("> ");
71
+              entNum = stdin.nextDouble();
72
+              result = num + entNum;
73
+              isValid = true;
74
+            break;
75
+      case 2: System.out.println("Enter number to subtract from " + num + ": ");
76
+              System.out.print("> ");
77
+              entNum = stdin.nextDouble();
78
+              result = num - entNum;
79
+              isValid = true;
80
+            break;
81
+      case 3: System.out.println("Enter number to multiply to " + num + ": ");
82
+              System.out.print("> ");
83
+              entNum = stdin.nextDouble();
84
+              result = num * entNum;
85
+              isValid = true;
86
+            break;
87
+      case 4: System.out.println("Enter number to divide from " + num + ": ");
88
+              System.out.print("> ");
89
+              entNum = stdin.nextDouble();
90
+              result = num / entNum; //use Double.isNaN(x)
91
+              isValid = true;
92
+            break;
93
+      case 5: System.out.println("Squaring number on display (" + num + "^2) ");
94
+              System.out.print("> ");
95
+              result = num * num;
96
+              isValid = true;
97
+            break;
98
+      case 6: System.out.println("Taking square root of " + num);//cant take negative #
99
+              System.out.print("> ");
100
+              result = Math.sqrt(num);
101
+              isValid = true;
102
+            break;
103
+      case 7: System.out.println("Enter exponent to raise " + num + " to (" + num + "^n)");
104
+              System.out.print("> ");
105
+              entNum = stdin.nextDouble();
106
+              result = Math.pow(num, entNum);
107
+              isValid = true;
108
+            break;
109
+      case 8: System.out.println("Calculating inverse of " + num + " (1/" + num + ")");
110
+              System.out.print("> ");
111
+              result = 1/num;
112
+              isValid = true;
113
+            break;
114
+      case 9: System.out.println("Calculating negation of  " + num + " (n -> -n , -n -> n)");
115
+              System.out.print("> ");
116
+              result = num * -1;
117
+              isValid = true;
118
+            break;
119
+      default:
120
+            break;
121
+    }
122
+  }
123
+  return result;
124
+}
125
+
126
+
127
+public double trigFunc(double num){ //take displayNum as input parameter
128
+
129
+  boolean isValid = false;
130
+  int choice;
131
+  double result = -0;
132
+
133
+  while(!isValid){
134
+    System.out.println("\n-------------------------------------------------");
135
+    System.out.println(num);
136
+    System.out.println("-------------------------------------------------");
137
+    System.out.println("Select trigonemtric function (enter # 1-6)");
138
+    System.out.println("(1) Sin");
139
+    System.out.println("(2) Cos");
140
+    System.out.println("(3) Tangent");
141
+    System.out.println("(4) ArcSin");
142
+    System.out.println("(5) ArcCos");
143
+    System.out.println("(6) ArcTan");
144
+    System.out.println("--------------------------------------------");
145
+    System.out.print("> ");
146
+    choice = stdin.nextInt();
147
+
148
+    switch(choice){
149
+      case 1: System.out.println("Calculating sine of " + num);
150
+              result = Math.sin(num);
151
+              isValid = true;
152
+              break;
153
+      case 2: System.out.println("Calculating cosine of " + num);
154
+              result = Math.cos(num);
155
+              isValid = true;
156
+              break;
157
+      case 3: System.out.println("Calculating tangent of " + num);
158
+              result = Math.tan(num);
159
+              isValid = true;
160
+              break;
161
+      case 4: System.out.println("Calculating inverse sine (arcSin) of " + num);
162
+              result = Math.asin(num);
163
+              isValid = true;
164
+              break;
165
+      case 5: System.out.println("Calculating inverse cosine (arcCos) of " + num);
166
+              result = Math.acos(num);
167
+              isValid = true;
168
+              break;
169
+      case 6: System.out.println("Calculating inverse tangent (arcTan) of " + num);
170
+              result = Math.atan(num);
171
+              isValid = true;
172
+              break;
173
+
174
+    }
175
+  }
176
+  return result;
177
+}
178
+
179
+public double advFunc(double num){ //take displayNum as input parameter
180
+
181
+  boolean isValid = false;
182
+  int choice;
183
+  double result = 0;
184
+
185
+  while(!isValid){
186
+    System.out.println("\n-------------------------------------------------");
187
+    System.out.println(num);
188
+    System.out.println("-------------------------------------------------");
189
+    System.out.println("Select advanced function to perform (enter # 1-7)");
190
+    System.out.println("(1) Factorial");
191
+    System.out.println("(2) Log");
192
+    System.out.println("(3) Inverse log (10^x)");
193
+    System.out.println("(4) Natural log (Ln x)");
194
+    System.out.println("(5) Inverse Natural log (e^x)");
195
+    System.out.println("(6) Cubed root"); //add custom 1
196
+    System.out.println("(7) Absolute value");//add custom 2
197
+    System.out.println("-------------------------------------------------");
198
+    System.out.print("> ");
199
+    choice = stdin.nextInt();
200
+
201
+    switch(choice){
202
+      case 1: System.out.println("Calculating factorial of " + num);
203
+              int i;
204
+              double fct = 1;
205
+              for(i=1; i<=num; i++){
206
+                fct *= i;
207
+              }
208
+              result = fct;
209
+              isValid = true;
210
+              break;
211
+      case 2: System.out.println("Calculating log of " + num);
212
+              result = Math.log10(num);
213
+              isValid = true;
214
+              break;
215
+      case 3: System.out.println("Calculating inverse log (10^x) of " + num);
216
+              result = Math.pow(10 , Math.log10(num));
217
+              isValid = true;
218
+              break;
219
+      case 4: System.out.println("Calculating natural log (Ln) of " + num);
220
+              result = Math.log(num);
221
+              isValid = true;
222
+              break;
223
+      case 5: System.out.println("Calculating inverese natural log (e^x) of " + num);
224
+              result = Math.exp(num);
225
+              isValid = true;
226
+              break;
227
+      case 6: System.out.println("Calculating cubed root of " + num);
228
+              result = Math.cbrt(num);
229
+              isValid = true;
230
+              break;
231
+      case 7: System.out.println("Calculating absolute value of " + num);
232
+              result = Math.abs(num);
233
+              isValid = true;
234
+              break;
235
+      default:
236
+            break;
237
+
238
+    }
239
+  }
240
+  return result;
241
+}
242
+
243
+}

+ 0
- 0
README.md Целия файл


+ 98
- 0
SciCalculator.java Целия файл

@@ -0,0 +1,98 @@
1
+import java.util.Scanner;
2
+import java.lang.*;
3
+
4
+public class SciCalculator{
5
+
6
+  public static void main(String[] args){
7
+
8
+    Scanner stdin = new Scanner(System.in);
9
+
10
+    boolean isOn = true;
11
+    int choice;
12
+
13
+    Memory memNumber = new Memory();
14
+    DisplayVal val = new DisplayVal();
15
+    Units unit = new Units();
16
+
17
+    while(isOn){
18
+        //display prompt
19
+      System.out.println("\n******************************************");
20
+      System.out.println(String.format("%"+30+"s", "Scientific Calculator"));
21
+      System.out.println("------------------------------------------");
22
+      System.out.println("Number System: " + val.getNumSys());
23
+      System.out.println("------------------------------------------");
24
+      System.out.println(val.valToString(val.getValue()));
25
+      System.out.println("==========================================");
26
+      System.out.println("Please choose a function (enter # 1-6)");
27
+      System.out.println("(1) Input value to calculator");
28
+      System.out.println("(2) Execute calculation");
29
+      System.out.println("(3) Access memory functions (M+, MC, MRC)");
30
+      System.out.println("(4) Change number system "); //may link to class
31
+      System.out.println("(5) Change units"); //may link to class
32
+      System.out.println("(6) Clear display");
33
+      System.out.println("------------------------------------------");
34
+      System.out.println("(7)Quit program");
35
+      System.out.println("******************************************");
36
+      System.out.print("> ");
37
+
38
+      choice = stdin.nextInt();
39
+
40
+      switch(choice){
41
+        case 1:
42
+                System.out.println("Enter the value you want to input to calculator:");//call change units function
43
+                System.out.print("> ");
44
+                double inVal = stdin.nextDouble();
45
+                val.setValue(inVal);
46
+              break;
47
+        case 2:
48
+                Operations op = new Operations();
49
+                val.setValue(op.chooseOp(val.getValue()));//call operations function
50
+              break;
51
+        case 3:
52
+                System.out.println("\n------------------------------------------");
53
+                System.out.println("Choose memory function (enter 1 or 2)");
54
+                System.out.println("(1) Recall value in memory");
55
+                System.out.println("(2) Update value in memory");
56
+                System.out.println("------------------------------------------");
57
+                System.out.print("> ");
58
+                int mem = stdin.nextInt();
59
+                if(mem == 1){
60
+                  val.setValue(memNumber.getMemNum());
61
+                }
62
+                else if(mem == 2){
63
+                  memNumber.setMemNum();
64
+                }
65
+                else{
66
+                  System.out.println("Value in memory is: " + memNumber.getMemNum());
67
+                }
68
+                break;
69
+        case 4:
70
+                System.out.println("\n------------------------------------------");
71
+                System.out.println("Select number system option (enter 1 or 2)");
72
+                System.out.println("(1) Toggle Number System");
73
+                System.out.println("(2) Choose Number System");
74
+                System.out.println("------------------------------------------");
75
+                System.out.print("> ");
76
+                int select = stdin.nextInt();
77
+                if(select == 1){
78
+                  val.toggleNumSys();
79
+                } else{
80
+                  val.setNumSys();
81
+                }
82
+              break;
83
+        case 5:
84
+                val.setValue(unit.choice(val.getValue()));//clear display
85
+              break;
86
+        case 6:
87
+                val.setValue(0.0);
88
+              break;
89
+        case 7:
90
+                System.out.println("Goodbye");//clear display
91
+                isOn = false;
92
+              break;
93
+        default:
94
+              break;
95
+      }
96
+    }
97
+  }
98
+}

+ 74
- 0
Units.java Целия файл

@@ -0,0 +1,74 @@
1
+import java.util.Scanner;
2
+
3
+public class Units
4
+{
5
+
6
+Scanner units = new Scanner(System.in);
7
+
8
+    public double choice (double num) {
9
+
10
+        boolean isValid = false;
11
+        int modechoice = 0;
12
+        double result = 0;
13
+
14
+    while(!isValid){
15
+    System.out.println("\n-------------------------------------------------");
16
+    System.out.println(num);
17
+    System.out.println("-------------------------------------------------");
18
+    System.out.println("Select which unit you would like to convert to (enter #1 or #2)");
19
+    System.out.println("(1) Degrees");
20
+    System.out.println("(2) Radians");
21
+    System.out.println("--------------------------------------------");
22
+    System.out.print("> ");
23
+    modechoice = units.nextInt();
24
+
25
+    switch(modechoice){
26
+        case 1: result = this.degrees(num);
27
+        isValid = true;
28
+        break;
29
+        case 2: result = this.radians(num);
30
+        isValid = true;
31
+        break; }
32
+    }
33
+    return result;
34
+    }
35
+
36
+    public double degrees (double num) {
37
+
38
+        System.out.println("Please enter your value for degree conversion");
39
+
40
+          boolean isValid = false;
41
+          double output = 0;
42
+          double input = units.nextInt();
43
+
44
+          if (!Double.isNaN(num)) {
45
+              System.out.println("Calculating degrees of " + input);
46
+              num = Math.toDegrees(input);
47
+              System.out.println(num);}
48
+          else {
49
+              System.out.println("Please enter your value");
50
+                      }
51
+          return num;
52
+    };
53
+    public double radians (double num) {
54
+
55
+        System.out.println("Please enter your value for radian conversion");
56
+
57
+    boolean isValid = true;
58
+    double output = 0;
59
+    double input = units.nextInt();
60
+
61
+        if (!Double.isNaN(num)) {
62
+              System.out.println("Calculating radians of " + input);
63
+              num = Math.toRadians(input);
64
+              System.out.println(num);}
65
+        else {
66
+            System.out.println("Please enter your value");
67
+        }
68
+        return num;
69
+
70
+}
71
+
72
+
73
+
74
+}

+ 60
- 22
package.bluej Целия файл

@@ -2,22 +2,25 @@
2 2
 dependency1.from=MainApplication
3 3
 dependency1.to=Console
4 4
 dependency1.type=UsesDependency
5
-editor.fx.0.height=722
6
-editor.fx.0.width=800
7
-editor.fx.0.x=640
5
+dependency2.from=UpdateDisplay
6
+dependency2.to=SwitchModes
7
+dependency2.type=UsesDependency
8
+editor.fx.0.height=717
9
+editor.fx.0.width=1280
10
+editor.fx.0.x=0
8 11
 editor.fx.0.y=23
9
-objectbench.height=214
10
-objectbench.width=595
12
+objectbench.height=92
13
+objectbench.width=1256
11 14
 package.divider.horizontal=0.6
12
-package.divider.vertical=0.6847360912981455
13
-package.editor.height=473
14
-package.editor.width=493
15
-package.editor.x=35
16
-package.editor.y=60
17
-package.frame.height=759
18
-package.frame.width=619
19
-package.numDependencies=1
20
-package.numTargets=2
15
+package.divider.vertical=0.8495440729483282
16
+package.editor.height=552
17
+package.editor.width=1154
18
+package.editor.x=0
19
+package.editor.y=23
20
+package.frame.height=716
21
+package.frame.width=1280
22
+package.numDependencies=2
23
+package.numTargets=7
21 24
 package.showExtends=true
22 25
 package.showUses=true
23 26
 project.charset=UTF-8
@@ -27,16 +30,51 @@ readme.width=47
27 30
 readme.x=10
28 31
 readme.y=10
29 32
 target1.height=50
30
-target1.name=Console
33
+target1.name=UpdateDisplay
31 34
 target1.showInterface=false
32 35
 target1.type=ClassTarget
33
-target1.width=80
34
-target1.x=80
35
-target1.y=200
36
+target1.width=120
37
+target1.x=130
38
+target1.y=130
36 39
 target2.height=50
37
-target2.name=MainApplication
40
+target2.name=Memory
38 41
 target2.showInterface=false
39 42
 target2.type=ClassTarget
40
-target2.width=120
41
-target2.x=70
42
-target2.y=70
43
+target2.width=80
44
+target2.x=190
45
+target2.y=10
46
+target3.height=50
47
+target3.name=Console
48
+target3.showInterface=false
49
+target3.type=ClassTarget
50
+target3.width=80
51
+target3.x=80
52
+target3.y=200
53
+target4.height=50
54
+target4.name=SwitchModes
55
+target4.showInterface=false
56
+target4.type=ClassTarget
57
+target4.width=110
58
+target4.x=10
59
+target4.y=130
60
+target5.height=50
61
+target5.name=NumSystemsConverter
62
+target5.showInterface=false
63
+target5.type=ClassTarget
64
+target5.width=170
65
+target5.x=10
66
+target5.y=320
67
+target6.height=50
68
+target6.name=SciCalculator
69
+target6.showInterface=false
70
+target6.type=ClassTarget
71
+target6.width=110
72
+target6.x=70
73
+target6.y=10
74
+target7.height=50
75
+target7.name=MainApplication
76
+target7.showInterface=false
77
+target7.type=ClassTarget
78
+target7.width=120
79
+target7.x=70
80
+target7.y=70