-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLabOneNumberConversions.java
More file actions
257 lines (186 loc) · 6.66 KB
/
LabOneNumberConversions.java
File metadata and controls
257 lines (186 loc) · 6.66 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Sukhamrit Singh
// Number Conversion
/*
This is a program where you would enter a number
(binary, octal, hex, or decimal), and it would convert
it to the other three forms of the number entered. The user first enters
the number, and then enters the type of number. Then it displays
results.
*/
import java.util.Scanner;
public class LabOneNumberConversions {
public static void main(String[] args) {
boolean run = true;
while ( run ) {
Scanner input = new Scanner(System.in);
// Get number
System.out.print("Please enter a number: ");
String numberString = input.nextLine();
// Get number Base
System.out.print("\nPlease enter the Base " +
"(Binary = 2, Octal = 8, Hexadecimal = 16, " +
"Decimal = 10, Exit = 0): ");
String base = input.nextLine();
int type = 0;
try {
type = Integer.parseInt(base);
} catch(NumberFormatException e) {
// If invalid Base, then start over
System.out.println(" Invalid Base ... "+base);
System.out.println("");
continue;
}
System.out.println("");
// Handle Exit
if ( type == 0 ) {
run = false;
}
// Handle Binary
else if (type == 2) {
try {
int num = Integer.parseInt(numberString, 2);
System.out.println(numberString + " Base("+type+") is:");
binaryToOctal(numberString);
binaryToHex(numberString);
binaryToDecimal(numberString);
} catch(NumberFormatException e) {
System.out.println(" Invalid Binary Number ... "+numberString);
}
}
// Handle Octal
else if (type == 8) {
try {
int num = Integer.parseInt(numberString, 8);
System.out.println(numberString + " Base("+type+") is:");
octalToBinary(numberString);
octalToHex(numberString);
octalToDecimal(numberString);
} catch(NumberFormatException e) {
System.out.println(" Invalid Octal Number ... "+numberString);
}
}
// Handle Hexadecimal
else if (type == 16) {
try {
int num = Integer.parseInt(numberString, 16);
System.out.println(numberString + " Base("+type+") is:");
hexToBinary(numberString);
hexToOctal(numberString);
hexToDecimal(numberString);
} catch(NumberFormatException e) {
System.out.println(" Invalid Hexadecimal Number ... "+numberString);
}
}
// Handle Decimal
else if (type == 10) {
try {
int num = Integer.parseInt(numberString, 10);
System.out.println(numberString + " Base("+type+") is:");
decimalToBinary(numberString);
decimalToOctal(numberString);
decimalToHex(numberString);
} catch(NumberFormatException e) {
System.out.println(" Invalid Decimal Number ... "+numberString);
}
}
// Unsupported Base
else {
System.out.println(" Base not supported");
}
System.out.println("");
}
}
/*
Convert Binary to Octal
*/
public static void binaryToOctal (String number) {
int decNum = Integer.parseInt(number, 2);
String octNum = Integer.toString(decNum, 8);
System.out.println(" Octal: "+octNum);
}
/*
Convert Binary to Hexadecimal
*/
public static void binaryToHex (String number) {
int decNum = Integer.parseInt(number, 2);
String hexNum = Integer.toString(decNum, 16);
System.out.println(" Hexadecimal: "+hexNum);
}
/*
Convert Binary to Decimal
*/
public static void binaryToDecimal (String number) {
int decNum = Integer.parseInt(number, 2);
System.out.println(" Decimal: "+decNum);
}
/*
Convert Octal to Binary
*/
public static void octalToBinary (String number) {
int decNum = Integer.parseInt(number, 8);
String binNum = Integer.toString(decNum, 2);
System.out.println(" Binary: "+binNum);
}
/*
Convert Octal to Hexadecimal
*/
public static void octalToHex (String number) {
int decNum = Integer.parseInt(number, 8);
String hexNum = Integer.toString(decNum, 16);
System.out.println(" Hexadecimal: "+hexNum);
}
/*
Convert Octal to Decimal
*/
public static void octalToDecimal (String number) {
int decNum = Integer.parseInt(number, 8);
System.out.println(" Decimal: "+decNum);
}
/*
Convert Hexadecimal to Binary
*/
public static void hexToBinary (String number) {
int decNum = Integer.parseInt(number, 16);
String binNum = Integer.toString(decNum, 2);
System.out.println(" Binary: "+binNum);
}
/*
Convert Hexadecimal to Octal
*/
public static void hexToOctal (String number) {
int decNum = Integer.parseInt(number, 16);
String octNum = Integer.toString(decNum, 8);
System.out.println(" Octal: "+octNum);
}
/*
Convert Hexadecimal to Decimal
*/
public static void hexToDecimal (String number) {
int decNum = Integer.parseInt(number, 16);
System.out.println(" Decimal: "+decNum);
}
/*
Convert Decimal to Binary
*/
public static void decimalToBinary (String number) {
int decNum = Integer.parseInt(number, 10);
String binNum = Integer.toString(decNum, 2);
System.out.println(" Binary: "+binNum);
}
/*
Convert Decimal to Octal
*/
public static void decimalToOctal (String number) {
int decNum = Integer.parseInt(number, 10);
String octNum = Integer.toString(decNum, 8);
System.out.println(" Octal: "+octNum);
}
/*
Convert Decimal to Hexadecimal
*/
public static void decimalToHex (String number) {
int decNum = Integer.parseInt(number, 10);
String hexNum = Integer.toString(decNum, 16);
System.out.println(" Hexadecimal: "+hexNum);
}
}