Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JOptionPane;
- public class New_Binary
- {
- public static void main(String[] args)
- { // TODO Auto-generated method stub
- String input;
- int num;
- Object convertOptions [] = {"Decimal to Binary", "Binary to Decimal", "Decimal or Binary to Octaldecimal", "Decimal or Binary to Hexadecimal"};
- final Object choice = (Object) JOptionPane.showOptionDialog(null, "What would you like to convert to?", null, JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, convertOptions, null);
- if (choice.equals(0))
- {
- input = JOptionPane.showInputDialog("Enter a decimal number: ");
- System.out.println(input);
- num = Integer.parseInt(input);
- System.out.println("Answer: ");
- while (num > 0)
- {
- int binary = 0;
- int remainder = num % 2;
- binary = remainder + binary;
- num = num / 2;
- System.out.print(binary);
- }
- num++;
- }
- if (choice.equals(1))
- {
- input = JOptionPane.showInputDialog("Enter a binary number: ");
- System.out.println(input);
- num = Integer.parseInt(input);
- int decimal = 0;
- int power = 0;
- while(num > 0)
- {
- int remainder = num % 10;
- decimal += (remainder*Math.pow(2, power));
- num= num/10;
- power++;
- }
- System.out.println("Answer:" + decimal);
- }
- if (choice.equals(2))
- {
- }
- if (choice.equals(3))
- {
- input = JOptionPane.showInputDialog("Enter a decimal number: ");
- System.out.println(input);
- num = Integer.parseInt(input);
- char hex [] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
- String answer = " ";
- while(num > 0)
- {
- int remainder = 0;
- remainder = num % 16;
- answer = hex [remainder] + answer;
- num = num / 16;
- }
- System.out.print("Answer: ");
- System.out.print(answer);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement