Browse Source

binary to int

main
fdai6499 2 years ago
committed by fdai6040
parent
commit
6db654459d
  1. 59
      src/main/java/src/TasksFrame.java

59
src/main/java/src/TasksFrame.java

@ -116,7 +116,7 @@ public class TasksFrame extends JFrame {
}
private void setupAtoi(){
JPanel jPanel = setupPanel("atoi");
JPanel jPanel = setupPanel("Interger / Binary Parsing");
JLabel jLabel = new JLabel();
jLabel.setVisible(true);
@ -134,9 +134,9 @@ public class TasksFrame extends JFrame {
output.setBounds(5, 60, 200, 30);
jPanel.add(output);
JButton jButton = new JButton("iton");
JButton jButton = new JButton("int to binary");
jButton.setVisible(true);
jButton.setBounds(210, 30, 100, 30);
jButton.setBounds(210, 30, 160, 30);
jPanel.add(jButton);
jButton.addActionListener(new ActionListener() {
@Override
@ -144,30 +144,49 @@ public class TasksFrame extends JFrame {
String text = input.getText();
int value = Integer.valueOf(text);
String binary = calcAtoi(value);
String binary = calcItoB(value);
output.setText("");
output.setText(binary);
}
});
// JButton jButton2 = new JButton("ntoi");
// jButton2.setVisible(true);
// jButton2.setBounds(210, 30, 100, 30);
// jPanel.add(jButton2);
// jButton2.addActionListener(new ActionListener() {
// @Override
// public void actionPerformed(ActionEvent e) {
// String text = output.getText();
// int value = Integer.valueOf(text);
//
// String binary = calcAtoi(value);
// output.setText(binary);
//
// }
// });
JButton jButton2 = new JButton("binary to int");
jButton2.setVisible(true);
jButton2.setBounds(210, 60, 160, 30);
jPanel.add(jButton2);
jButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String text = output.getText();
int value = Integer.valueOf(text);
String binary = calcBinaryToInt(value);
input.setText("");
input.setText(binary);
}
});
}
public String calcBinaryToInt(int value) {
LinkedList<Integer> integers = new LinkedList<>();
int sum = 0;
int i = 0;
while (value > 0) {
int rest = value % 10;
sum += rest * Math.pow(2, i++);
value/=10;
}
return String.valueOf(sum);
}
public String calcAtoi(int value) {
public String calcItoB(int value) {
return Integer.toBinaryString(value);
}

Loading…
Cancel
Save