Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. import java.awt.Color;
  2. import javax.swing.JApplet;
  3. import javax.swing.JRadioButton;
  4. import javax.swing.ButtonGroup;
  5. import javax.swing.JLabel;
  6. import javax.swing.JTextField;
  7. import java.awt.Container;
  8. import java.awt.FlowLayout;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.event.ActionListener;
  11. public class assem extends JApplet implements ActionListener {
  12. private static JTextField Input = new JTextField();
  13. private static JTextField Output = new JTextField();
  14. ButtonGroup radioGroup = new ButtonGroup();
  15.  
  16. public void init(){
  17. Container display = getContentPane();
  18. display.setBackground(Color.PINK);
  19. display.setLayout(new FlowLayout());
  20. JLabel instruct = new JLabel("Enter a number\n then click on 2, 8, 16\n to convert the number to its binary, octal and hexadecimal value respectively");
  21. display.add(instruct);
  22. JRadioButton bin = new JRadioButton("binary");
  23. radioGroup.add(bin);
  24. bin.addActionListener(this);
  25. display.add(bin);
  26. JRadioButton oct = new JRadioButton("octal");
  27. radioGroup.add(oct);
  28. oct.addActionListener(this);
  29. display.add(oct);
  30. JRadioButton hex = new JRadioButton("hexadecimal");
  31. radioGroup.add(hex);
  32. hex.addActionListener(this);
  33. display.add(hex);
  34.  
  35.  
  36.  
  37. Input.setText("enter number here.");
  38. Output.setText("Result");
  39. display.add(Input);
  40. display.add(Output);
  41. }
  42.  
  43. public void actionPerformed(ActionEvent e){
  44. String action = e.getActionCommand();
  45. if (action.equals("binary")){
  46. String runz = "";
  47. String Str_numb = Input.getText();
  48. int numb = Integer.parseInt(Str_numb);
  49. while (numb > 0){
  50. int y = numb%2;
  51. runz = runz + y + "";
  52. numb = numb/2;
  53. }
  54. String ans = new StringBuffer(runz).reverse().toString();
  55. Output.setText(ans);
  56. }
  57. else if(action.equals("octal")){
  58. String runz = "";
  59. String Str_numb = Input.getText();
  60. int numb = Integer.parseInt(Str_numb);
  61. while (numb > 0){
  62. int y = numb%8;
  63. runz = runz + y + "";
  64. numb = numb/8;
  65. }
  66. String ans = new StringBuffer(runz).reverse().toString();
  67. Output.setText(ans);
  68. }
  69. else if (action.equals("hexadecimal")){
  70. String runz = "";
  71. char add;
  72. String Str_numb = Input.getText();
  73. int numb = Integer.parseInt(Str_numb);
  74. while(numb > 0){
  75. int y = numb%16;
  76. if (y < 10){
  77. runz = runz + y +"";
  78. numb = numb/16;}
  79. if (y == 10){
  80. add = 'A';
  81. runz = runz+add;
  82. numb= numb/16;}
  83. else if (y == 11){
  84. add = 'B';
  85. runz = runz+add;
  86. numb = numb/16;}
  87. else if (y == 12){
  88. add = 'C';
  89. runz = runz+add;
  90. numb = numb/16;}
  91. else if (y == 13){
  92. add = 'D';
  93. runz = runz+add;
  94. numb = numb/16;}
  95. else if (y == 14){
  96. add = 'E';
  97. runz = runz+add;
  98. numb = numb/16;}
  99. else if (y == 15){
  100. add = 'F';
  101. runz = runz+add;
  102. numb = numb/16;}}
  103. String ans = new StringBuffer(runz).reverse().toString();
  104. Output.setText(ans);
  105.  
  106.  
  107. }
  108. }
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement