Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.73 KB | None | 0 0
  1. import java.applet.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5.  
  6. import java.awt.Color;
  7. import javax.swing.JApplet;
  8. import javax.swing.JRadioButton;
  9. import javax.swing.ButtonGroup;
  10. import javax.swing.JLabel;
  11. import javax.swing.JTextArea;
  12. import java.awt.Container;
  13. import java.awt.FlowLayout;
  14. import java.awt.event.ActionEvent;
  15. import java.awt.event.ActionListener;
  16. public class trick extends JApplet implements ActionListener{
  17. public static int line = 1;
  18. public static int char_per_line = 5;
  19. private TextField[] txtF = new TextField[16];
  20. private JTextArea input = new JTextArea(line, char_per_line);
  21. private JTextArea input2 = new JTextArea(line, char_per_line);
  22. private JTextArea error = new JTextArea(5, 60);
  23. public static String finals;
  24. public void init(){
  25. Container display = getContentPane();
  26. display.setBackground(Color.ORANGE);
  27. display.setLayout(new FlowLayout());
  28. JLabel intro = new JLabel("CHEEZEE'S BINARY REGISTER\n");
  29. intro.setForeground(Color.RED);
  30. display.add(intro);
  31.  
  32.  
  33. ButtonGroup RadioGroup = new ButtonGroup();
  34. ButtonGroup RadioGroup2 = new ButtonGroup();
  35. JRadioButton add = new JRadioButton("ADD");
  36. RadioGroup.add(add);
  37. add.addActionListener(this);
  38.  
  39. JRadioButton sub = new JRadioButton("SUBTRACT");
  40. RadioGroup.add(sub);
  41. sub.addActionListener(this);
  42.  
  43.  
  44. JRadioButton Do = new JRadioButton("DO");
  45. RadioGroup2.add(Do);
  46. Do.addActionListener(this);
  47.  
  48. JRadioButton clear = new JRadioButton("CLEAR");
  49. RadioGroup2.add(clear);
  50. clear.addActionListener(this);
  51.  
  52.  
  53. JLabel Enter = new JLabel("Enter a binary number");
  54. display.add(Enter);
  55. display.add(input);
  56. display.add(add);
  57. display.add(sub);
  58. JLabel Enter2 = new JLabel("Enter another binary number");
  59. display.add(Enter2);
  60. display.add(input2);
  61.  
  62. display.add(Do);
  63.  
  64. display.add(clear);
  65. display.add(error);
  66. for (int y = 0; y < 16; y++){
  67. txtF[y] = new TextField();
  68. }
  69. display.add(txtF[0]);
  70. display.add(txtF[1]);
  71. display.add(txtF[2]);
  72. display.add(txtF[3]);
  73. display.add(txtF[4]);
  74. display.add(txtF[5]);
  75. display.add(txtF[6]);
  76. display.add(txtF[7]);
  77. display.add(txtF[8]);
  78. display.add(txtF[9]);
  79. display.add(txtF[10]);
  80. display.add(txtF[11]);
  81. display.add(txtF[12]);
  82. display.add(txtF[13]);
  83. display.add(txtF[14]);
  84. display.add(txtF[15]);
  85.  
  86. }
  87.  
  88.  
  89. public void actionPerformed(ActionEvent e){
  90. String click = e.getActionCommand();
  91. if (click.equals("ADD")){
  92. String one = input.getText();
  93. long chan = Long.parseLong(one);
  94. long rem;
  95. while (chan > 0){
  96. rem = chan % 10;
  97. chan = chan/10;
  98. if (rem != 0 && rem != 1){
  99. error.setText("Please make sure you enter a binary number");
  100. }
  101. }
  102. int dec = Integer.parseInt(one,2);
  103. String two = input2.getText();
  104. long chan2 = Long.parseLong(two);
  105. long rem2;
  106. while (chan > 0){
  107. rem2 = chan2 % 10;
  108. chan2 = chan2/10;
  109. if (rem2 != 0 && rem2 != 1){
  110. error.setText("Please make sure you enter a binary number");
  111. }
  112. }
  113. int dec2 = Integer.parseInt(two,2);
  114. int ans = dec + dec2;
  115. if (ans > 32768){
  116. error.setText("Overflow the convert only has a limit of 16 bits");
  117. }
  118. String runz = "";
  119. while (ans > 0){
  120. int y = ans%2;
  121. runz = runz + y + "";
  122. ans = ans/2;
  123. }
  124. finals = new StringBuffer(runz).reverse().toString();
  125. for (int u = 0; u < 16; u++){
  126. txtF[u].setText("");
  127. }
  128.  
  129. }
  130. else if(click.equals("SUBTRACT")){
  131. String one = input.getText();
  132. long chan = Long.parseLong(one);
  133. long rem;
  134. while (chan > 0){
  135. rem = chan % 10;
  136. chan = chan/10;
  137. if (rem != 0 && rem != 1){
  138. error.setText("Please make sure you enter a binary number");
  139. }
  140. }
  141. int dec = Integer.parseInt(one,2);
  142. String two = input2.getText();
  143. long chan2 = Long.parseLong(two);
  144. long rem2;
  145. while (chan > 0){
  146. rem2 = chan2 % 10;
  147. chan2 = chan2/10;
  148. if (rem2 != 0 && rem2 != 1){
  149. error.setText("Please make sure you enter a binary number");
  150. }
  151. }
  152. int dec2 = Integer.parseInt(two,2);
  153. int ans = dec - dec2;
  154. if(ans <0){
  155. error.setText("Underflow make sure you are not producing a neagtive number!");}
  156. if (ans > 32768){
  157. error.setText("Overflow the convert only has a limit of 16 bits");}
  158. String runz = "";
  159. while (ans > 0){
  160. int y = ans%2;
  161. runz = runz + y + "";
  162. ans = ans/2;
  163. }
  164. finals = new StringBuffer(runz).reverse().toString();
  165. for (int u = 0; u < 16; u++){
  166. txtF[u].setText("");
  167. }
  168. }
  169. else if (click.equals("DO")){
  170. int size = finals.length();
  171. for (int y = 0;y < size;y++ ){
  172. String single = Character.toString(finals.charAt(y));
  173. txtF[y].setText(single);
  174. }
  175. }
  176. else if (click.equals("CLEAR")){
  177. input.setText("");
  178. input2.setText("");
  179. for (int r = 0; r < 16; r++){
  180. txtF[r].setText("");
  181. }
  182. error.setText("");
  183. }
  184.  
  185. }
  186.  
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement