-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSciCalcTest.java
More file actions
127 lines (110 loc) · 5.12 KB
/
Copy pathSciCalcTest.java
File metadata and controls
127 lines (110 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import Scientific.*;
import java.util.Scanner;
public class SciCalcTest {
// Define the menu options as an enum
public enum Options {
DECIMAL_TO_BINARY(1, "Convert Decimal to Binary"),
BINARY_TO_DECIMAL(2, "Convert Binary to Decimal"),
DECIMAL_TO_HEX(3, "Convert Decimal to Hexadecimal"),
DECIMAL_TO_IEEE754(4, "Convert Decimal to IEEE754"),
DECIMAL_TO_SCIENTIFIC(5, "Convert Decimal to Scientific Notation"),
EXIT(6, "Exit");
private final int code;
private final String description;
Options(int code, String description) {
this.code = code;
this.description = description;
}
public int getCode() {
return code;
}
public String getDescription() {
return description;
}
public static Options fromCode(int code) {
for (Options option : values()) {
if (option.getCode() == code) {
return option;
}
}
return null;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
SciCalc calc = new SciCalc();
while (true) {
System.out.println("\n==== SCIENTIFIC CALCULATOR ====");
for (Options option : Options.values()) {
System.out.printf("%d. %s%n", option.getCode(), option.getDescription());
}
System.out.print("Choose an option: ");
try {
int choice = Integer.parseInt(scanner.nextLine());
Options selectedOption = Options.fromCode(choice);
if (selectedOption == null) {
System.out.println("Invalid option. Please choose a valid number.");
continue;
}
switch (selectedOption) {
case DECIMAL_TO_BINARY:
System.out.print("Enter a decimal number: ");
try {
int decimalNumber = Integer.parseInt(scanner.nextLine());
String binary = calc.base10ToBase2(decimalNumber);
System.out.println("Binary Representation: " + binary);
} catch (NumberFormatException e) {
System.out.println("Invalid input. Please enter a valid integer.");
}
break;
case BINARY_TO_DECIMAL:
System.out.print("Enter a binary number: ");
String binaryInput = scanner.nextLine();
try {
int decimal = calc.base2ToBase10(binaryInput);
System.out.println("Decimal Representation: " + decimal);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
break;
case DECIMAL_TO_HEX:
System.out.print("Enter a decimal number: ");
try {
int decimalNumber = Integer.parseInt(scanner.nextLine());
String hex = calc.decimalToHex(decimalNumber);
System.out.println("Hexadecimal Representation: " + hex);
} catch (NumberFormatException e) {
System.out.println("Invalid input. Please enter a valid integer.");
}
break;
case DECIMAL_TO_IEEE754:
System.out.print("Enter a decimal number: ");
try {
float number = Float.parseFloat(scanner.nextLine());
String ieee754 = calc.decimalToIEEE754(number);
System.out.println("IEEE 754 Representation: " + ieee754);
} catch (NumberFormatException e) {
System.out.println("Invalid input. Please enter a valid number.");
}
break;
case DECIMAL_TO_SCIENTIFIC:
System.out.print("Enter a decimal number: ");
try {
double number = Double.parseDouble(scanner.nextLine());
String scientific = calc.decimalToScientific(number);
System.out.println("Scientific Notation: " + scientific);
} catch (NumberFormatException e) {
System.out.println("Invalid input. Please enter a valid number.");
}
break;
case EXIT:
System.out.println("Exiting... Goodbye!");
scanner.close();
return;
}
} catch (NumberFormatException e) {
System.out.println("Invalid input. Please enter a valid number.");
}
}
}
}