-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertor.java
More file actions
233 lines (196 loc) · 6.27 KB
/
Copy pathConvertor.java
File metadata and controls
233 lines (196 loc) · 6.27 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
/*
BASE N - BASE N CONVERTOR IN JAVA
GUI based program in Java to convert numbers from one number system to another.
*/
import java.awt.event.*;
import java.awt.Color;
import javax.swing.*;
public class Convertor
{
// Function to convert number from one base to another
static String baseToBase(String num, int base1, int base2)
{
int no = convFrmBaseToDeci(num, base1);
return convFrmDecToBase(no, base2);
}
// Function to convert from Decimal to Base N
static String convFrmDecToBase(int num, int base)
{
String res = "";
int rem;
// Convert input number is given base by repeatedly
// dividing it by base and taking remainder
while (num > 0) {
rem = num % base;
if (base == 16) {
if (rem == 10)
res += 'A';
else if (rem == 11)
res += 'B';
else if (rem == 12)
res += 'C';
else if (rem == 13)
res += 'D';
else if (rem == 14)
res += 'E';
else if (rem == 15)
res += 'F';
else
res += rem;
} else
res += rem;
num /= base;
}
// Reverse the result
return new StringBuffer(res).reverse().toString();
}
// Function to convert from Base N to Decimal
static int convFrmBaseToDeci(String num, int base)
{
if (base < 2 || (base > 10 && base != 16))
return -1;
int val = 0;
int power = 1;
for (int i = num.length() - 1; i >= 0; i--) {
int digit = digitToVal(num.charAt(i));
if (digit < 0 || digit >= base)
return -1;
// Decimal equivalent is str[len-1]*1 +
// str[len-1]*base + str[len-1]*(base^2) + ...
val += digit * power;
power = power * base;
}
return val;
}
// Function to find the int value of characters eg. A = 10
static int digitToVal(char c)
{
if (c >= '0' && c <= '9')
return (int) c - '0';
else
return (int) c - 'A' + 10;
}
// Function to check weather the input is a valid Base N number
static boolean isValid(int base, String num)
{
if (num.length() == 0)
return false;
switch (base)
{
case 2:
for (int i = 0; i < num.length(); i++)
{
if (num.charAt(i) != '0' && num.charAt(i) != '1')
return false;
}
break;
case 10:
for (int i = 0; i < num.length(); i++)
{
if (!Character.isDigit(num.charAt(i)))
return false;
}
break;
case 8:
for (int i =0; i< num.length(); i++)
{
if (!Character.isDigit(num.charAt(i)) || num.charAt(i) == '8' || num.charAt(i) == '9')
return false;
}
break;
case 16:
for (int i = 0; i <num.length(); i++)
{
if (!Character.isDigit(num.charAt(i)))
if (!"ABDCEF".contains(String.valueOf(num.charAt(i))))
return false;
}
break;
}
return true;
}
public static void main(String args[])
{
// Creating instance of JFrame with the title
JFrame frame = new JFrame("Base N Convertor");
// Creating the title label (instacne of JLabel)
JLabel title = new JLabel("<html><h1><b>Base N Convertor</b></h1></html>");
// Setting bounds of the JComponent: x axis, y axis, width, height
title.setBounds(0,50,400,30);
// Setting the text alignment to center
title.setHorizontalAlignment(SwingConstants.CENTER);
// Creating the to label (instacne of JLabel)
JLabel label1 = new JLabel("<html><h3><b>to</b></h3></html>");
// Setting bounds of the JComponent: x axis, y axis, width, height
label1.setBounds(0,150,400,30);
// Setting the text alignment to center
label1.setHorizontalAlignment(SwingConstants.CENTER);
String system[]={"Decimal","Binary","Octal","Hexadecimal"};
JComboBox combobox1 = new JComboBox(system);
combobox1.setBounds(25,150,150,30);
JComboBox combobox2 = new JComboBox(system);
combobox2.setBounds(225,150,150,30);
// Creating the Text Field for the input (instance of JTextField)
JTextField textfield1 = new JTextField();
// Setting bounds of the JComponent: x axis, y axis, width, height
textfield1.setBounds(125, 230, 150, 30);
// Setting the text alignment to right
textfield1.setHorizontalAlignment(SwingConstants.RIGHT);
// Creating the Calculate button (instance of JButton)
JButton button1 = new JButton("Convert");
// Setting bounds of the JComponent: x axis, y axis, width, height
button1.setBounds(150, 280, 100, 40);
// Creting the results label (instance of JLabel)
JLabel result = new JLabel();
// Setting bounds of the JComponent: x axis, y axis, width, height
result.setBounds(20,300,360,100);
// Setting the text alignment to center
result.setHorizontalAlignment(SwingConstants.CENTER);
// Making the label invisible (initially)
result.setVisible(false);
// Adding an ActionListener to the button to detect clicks
button1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int baseArray[] = {10,2,8,16};
int base1 = baseArray[combobox1.getSelectedIndex()];
int base2 = baseArray[combobox2.getSelectedIndex()];
String num = textfield1.getText().toUpperCase();
String resultString = "";
if (isValid(base1, num))
{
resultString = "<html><h2><b>" +
baseToBase(num, base1, base2) +
"</b></h2></html>";
}
else
{
resultString = "<html><h2><b>" +
"Invalid " + combobox1.getItemAt(combobox1.getSelectedIndex()) + " number" +
"</b></h2></html>";
}
result.setVisible(true);
result.setText(resultString);
}
});
// Adding all the components to the frame
frame.add(title);
frame.add(label1);
frame.add(combobox1);
frame.add(combobox2);
frame.add(textfield1);
frame.add(button1);
frame.add(result);
// Setting the size of the frame to 400 width and 500 height
frame.setSize(400,500);
// Setting the background color to yellow
frame.getContentPane().setBackground(Color.gray);
// Setting the Calculate button as the default button
frame.getRootPane().setDefaultButton(button1);
// Using no layout managers
frame.setLayout(null);
// Making the frame visible
frame.setVisible(true);
}
}