-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberConvertor.java
More file actions
284 lines (214 loc) · 9.99 KB
/
NumberConvertor.java
File metadata and controls
284 lines (214 loc) · 9.99 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import java.util.Scanner;
class NumberConvertor{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
boolean running=true;
//Figure 1 – icalc Number Converter Home Page
System.out.println(" __ ______ __ ");
System.out.println(" | \\/ \\ | \\ ");
System.out.println(" \\$$| $$$$$$\\ _____ | $$ _______ ");
System.out.println(" | \\| $$ \\$$ | \\ | $$ / \\ ");
System.out.println(" | $$| $$ \\$$$$$\\| $$| $$$$$$$ ");
System.out.println(" | $$| $$ __ / $$| $$| $$ ");
System.out.println(" | $$| $$__/ \\| $$$$$$$| $$| $$_____ ");
System.out.println(" | $$ \\$$ $$\\$$ $$| $$ \\$$ \\ ");
System.out.println(" \\$$ \\$$$$$$ \\$$$$$$$ \\$$ \\$$$$$$$ ");
System.out.println(" ");
System.out.println(" _ _ _ _____ _ ");
System.out.println("| \\ | | | | / ____| | | ");
System.out.println("| \\| | _ _ _ __ ___ | |__ ___ _ __ | | ___ _ __ __ __ ___ _ __ | |_ ___ _ __ ");
System.out.println("| . ` | | | | | | '_ ` _ \\ | '_ \\ / _ \\ | '__| | | / _ \\ | '_ \\ \\ \\ / / / _ \\ | '__| | __| / _ \\ | '__| ");
System.out.println("| |\\ | | |_| | | | | | | | | |_) | | __/ | | | |____ | (_) | | | | | \\ v / | __/ | | | |_ | __/ | | ");
System.out.println("|_| \\_| \\__,_| |_| |_| |_| |_.__/ \\___| |_| \\_____| \\___/ |_| |_| \\_/ \\___| |_| \\__| \\___| |_| ");
System.out.println(" ");
System.out.println("=================================================================================================================================== ");
System.out.println(" ");
//
while(running){
System.out.println(" [01] Decimal Converter ");
System.out.println(" [02] Binary Converter ");
System.out.println(" [03] Octal Converter ");
System.out.println(" [04] Hexadecimal Converter ");
System.out.println(" [05] Roman Number Converter ");
System.out.println(" ");
System.out.print("Enter Option->" );
int option=input.nextInt();
switch(option){
case 1:
DecimalConverter(input);
break;
case 2:
BinaryConverter(input);
break;
case 3:
OctalConverter(input);
break;
case 4:
HexadecimalConverter(input);
break;
case 5:
RomanNumberConverter(input);
break;
default:
System.out.println("Invalid Option");
}
System.out.print("Do you want to go to homepage (Y/N) ->" );
String choice=input.next();
if(!choice.equalsIgnoreCase("Y")){
running = false;
}
}
input.close();
}
//Decimal Converter
public static void DecimalConverter(Scanner input){
System.out.println(" ");
System.out.println("+--------------------------------------------------------------------+");
System.out.println("| Decimal Converter |");
System.out.println("+--------------------------------------------------------------------+ ");
System.out.println(" ");
System.out.print("Enter an Decimal number:" );
int decimal=input.nextInt();
System.out.println(" ");
if(decimal>=0){
String binary=Integer.toBinaryString(decimal);
System.out.println(" Binary number:"+binary);
String Octal=Integer.toOctalString(decimal);
System.out.println(" Octal number:"+ Octal);
String hexa=Integer.toHexString(decimal);
System.out.println(" Hexadecimal number:"+ hexa.toUpperCase());
System.out.println(" ");
}else{
System.out.println(" Invalid input..." );
System.out.println(" ");
}
}
//Binary Converter
public static void BinaryConverter(Scanner input){
System.out.println(" ");
System.out.println("+--------------------------------------------------------------------+");
System.out.println("| Binary Converter |");
System.out.println("+--------------------------------------------------------------------+ ");
System.out.println(" ");
System.out.print("Enter an Binary number:" );
String Binary=input.next();
System.out.println(" ");
if(Binary.matches("[01]+")){
int decimal=Integer.parseInt(Binary,2);
System.out.println(" Decimal number:"+decimal);
String Octal=Integer.toOctalString(decimal);
System.out.println(" Octal number:"+ Octal);
String hexa=Integer.toHexString(decimal);
System.out.println(" Hexadecimal number:"+ hexa.toUpperCase());
System.out.println(" ");
}else{
System.out.println(" Invalid input..." );
System.out.println(" ");
}
}
//Octal Converter
public static void OctalConverter(Scanner input){
System.out.println(" ");
System.out.println("+--------------------------------------------------------------------+");
System.out.println("| Octal Converter |");
System.out.println("+--------------------------------------------------------------------+ ");
System.out.println(" ");
System.out.print("Enter an Octal number:" );
String Octal=input.next();
System.out.println(" ");
if(Octal.matches("[0-7]+")){
int decimal=Integer.parseInt(Octal,8);
System.out.println(" Decimal Number:"+decimal);
String Binary=Integer.toBinaryString(decimal);
System.out.println(" Binary number:"+ Binary);
String hexa=Integer.toHexString(decimal).toUpperCase();
System.out.println(" Hexadecimal number:"+ hexa);
System.out.println(" ");
}else{
System.out.println(" Invalid input..." );
System.out.println(" ");
}
}
//Hexadecimal Converter
public static void HexadecimalConverter(Scanner input){
System.out.println(" ");
System.out.println("+--------------------------------------------------------------------+");
System.out.println("| Hexadecimal Converter |");
System.out.println("+--------------------------------------------------------------------+ ");
System.out.println(" ");
System.out.print("Enter an Hexadecimal number:" );
String Hexadecimal=input.next();
System.out.println(" ");
if(Hexadecimal.matches("[0-9A-Fa-f]+")){
int decimal=Integer.parseInt(Hexadecimal,16);
System.out.println(" Decimal number:"+decimal);
String Binary=Integer.toBinaryString(decimal);
System.out.println(" Binary number:"+ Binary);
String Octal=Integer.toOctalString(decimal);
System.out.println(" Octal number:"+ Octal);
System.out.println(" ");
}else{
System.out.println(" Invalid input..." );
System.out.println(" ");
}
}
//Roman Number
public static void RomanNumberConverter(Scanner input){
boolean running=true;
System.out.println(" ");
System.out.println("+--------------------------------------------------------------------+");
System.out.println("| Roman Number Converter |");
System.out.println("+--------------------------------------------------------------------+ ");
System.out.println(" ");
while(running){
System.out.println(" [01] Decimal Number to Roman Number Converter ");
System.out.println(" [02] Roman Number to Decimal Number Converter ");
System.out.println(" ");
System.out.print("Enter an option:" );
int option=input.nextInt();
switch(option){
case 1:
DecimalNumbertoRomanNumberConverter(input);
break;
default:
System.out.println("Invalid Option");
}
System.out.print("Do you want to go to homepage (Y/N) ->" );
String choice=input.next();
if(!choice.equalsIgnoreCase("Y")){
running = false;
}
}
input.close();
}
//– Decimal - Roman
public static void DecimalNumbertoRomanNumberConverter(Scanner input){
System.out.println(" ");
System.out.println("+--------------------------------------------------------------------+");
System.out.println("| Decimal Number to Roman Number Converter |");
System.out.println("+--------------------------------------------------------------------+ ");
System.out.println(" ");
System.out.print("Enter an Decimal Number:" );
int num1=input.nextInt();
System.out.println(" ");
if(num1>0 && num1<=3999){
String[] romanSymbols=
{"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
int[] values=
{1000,900,500,400,100,90,50,40,10,9,5,4,1};
StringBuilder roman=new StringBuilder();
for(int i=0;i<values.length;i++){
while(num1>=values[i]){
num1-=values[i];
roman.append(romanSymbols[i]);
}
}
System.out.println(" Roman Number:"+ roman.toString());
System.out.println(" ");
}else{
System.out.println(" Invalid input..." );
System.out.println(" ");
}
}
}
//Roman - Decimal