Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import javax.swing.JApplet;
- import javax.swing.JRadioButton;
- import javax.swing.ButtonGroup;
- import javax.swing.JLabel;
- import javax.swing.JTextField;
- import java.awt.Container;
- import java.awt.FlowLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- public class assem extends JApplet implements ActionListener {
- private static JTextField Input = new JTextField();
- private static JTextField Output = new JTextField();
- ButtonGroup radioGroup = new ButtonGroup();
- public void init(){
- Container display = getContentPane();
- display.setBackground(Color.PINK);
- display.setLayout(new FlowLayout());
- 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");
- display.add(instruct);
- JRadioButton bin = new JRadioButton("binary");
- radioGroup.add(bin);
- bin.addActionListener(this);
- display.add(bin);
- JRadioButton oct = new JRadioButton("octal");
- radioGroup.add(oct);
- oct.addActionListener(this);
- display.add(oct);
- JRadioButton hex = new JRadioButton("hexadecimal");
- radioGroup.add(hex);
- hex.addActionListener(this);
- display.add(hex);
- Input.setText("enter number here.");
- Output.setText("Result");
- display.add(Input);
- display.add(Output);
- }
- public void actionPerformed(ActionEvent e){
- String action = e.getActionCommand();
- if (action.equals("binary")){
- String runz = "";
- String Str_numb = Input.getText();
- int numb = Integer.parseInt(Str_numb);
- while (numb > 0){
- int y = numb%2;
- runz = runz + y + "";
- numb = numb/2;
- }
- String ans = new StringBuffer(runz).reverse().toString();
- Output.setText(ans);
- }
- else if(action.equals("octal")){
- String runz = "";
- String Str_numb = Input.getText();
- int numb = Integer.parseInt(Str_numb);
- while (numb > 0){
- int y = numb%8;
- runz = runz + y + "";
- numb = numb/8;
- }
- String ans = new StringBuffer(runz).reverse().toString();
- Output.setText(ans);
- }
- else if (action.equals("hexadecimal")){
- String runz = "";
- char add;
- String Str_numb = Input.getText();
- int numb = Integer.parseInt(Str_numb);
- while(numb > 0){
- int y = numb%16;
- if (y < 10){
- runz = runz + y +"";
- numb = numb/16;}
- if (y == 10){
- add = 'A';
- runz = runz+add;
- numb= numb/16;}
- else if (y == 11){
- add = 'B';
- runz = runz+add;
- numb = numb/16;}
- else if (y == 12){
- add = 'C';
- runz = runz+add;
- numb = numb/16;}
- else if (y == 13){
- add = 'D';
- runz = runz+add;
- numb = numb/16;}
- else if (y == 14){
- add = 'E';
- runz = runz+add;
- numb = numb/16;}
- else if (y == 15){
- add = 'F';
- runz = runz+add;
- numb = numb/16;}}
- String ans = new StringBuffer(runz).reverse().toString();
- Output.setText(ans);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement