Wednesday, 20 July 2011

// Programmer: Simbongile and Lesetsa

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;


public class Converter4 extends JFrame implements ActionListener
{
    public static final int WIDTH = 400;
    public static final int HEIGHT = 200;
    public static final int NUMBER_OF_DIGITS = 1;
   
    private JTextField txt1;
    private JTextField txt2;
    private JTextField txt3;
    private JTextField txt4;
    private JTextField txt5;
    private JTextField txt6;
    private JTextField txt7;
    private JTextField txt8;
    private JTextField txt9;
    private JTextField txt10;
    private JTextField txt11;
    private JTextField txt12;
    private JTextField txt13;
    private JTextField txt14;
    private JTextField txt15;
    private JTextField txt16;
    private JTextField input;
    private JTextField output;
    private String result = " ", rem;
    private int ans = 0, xme =0;
   
    public static void main(String[] args)
    {
        Converter4 aConv = new Converter4();
        aConv.setVisible(true);
    }
   
    public Converter4()
    {
        setTitle("Converter");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(WIDTH, HEIGHT);
        setLayout(new GridLayout(5, 1));
       
         // Menu
         JMenuBar mb = new JMenuBar();
       JMenu mnuFile = new JMenu("File");
         JMenuItem mnuItemQuit = new JMenuItem("Quit");
         JMenu mnuHelp = new JMenu("Help");
         JMenuItem mnuItemAbout = new JMenuItem("About");
       
          setJMenuBar(mb);
        mnuFile.add(mnuItemQuit); 
        mnuHelp.add(mnuItemAbout);
        mb.add(mnuFile);      
        mb.add(mnuHelp);
         
              // Allows the Swing App to be closed
        addWindowListener(new ListenCloseWdw());
       
        //Add Menu listener
        mnuItemQuit.addActionListener(new ListenMenuQuit());
         
        JPanel jp1 = new JPanel();
        jp1.setLayout(new FlowLayout());
       
        JPanel jp2 = new JPanel();
        jp2.setLayout(new FlowLayout());
       
        JPanel jp3 = new JPanel();
        jp3.setLayout(new FlowLayout());
       
        JPanel jp4 = new JPanel();
        jp4.setLayout(new FlowLayout());

        JPanel jp5 = new JPanel();
        jp4.setLayout(new FlowLayout());
       
        JLabel base = new JLabel("Click Base>>");
        JLabel num = new JLabel("Enter number");
       
        txt1 = new JTextField("", NUMBER_OF_DIGITS);
        txt1.setBackground(Color.WHITE);
        txt2 = new JTextField(" ", NUMBER_OF_DIGITS);
        txt2.setBackground(Color.WHITE);
        txt3 = new JTextField(" ", NUMBER_OF_DIGITS);
        txt3.setBackground(Color.WHITE);
        txt4 = new JTextField(" ", NUMBER_OF_DIGITS);
        txt4.setBackground(Color.WHITE);
        txt5 = new JTextField(" ", NUMBER_OF_DIGITS);
        txt5.setBackground(Color.WHITE);
        txt6 = new JTextField(" ", NUMBER_OF_DIGITS);
        txt6.setBackground(Color.WHITE);
        txt7 = new JTextField(" ", NUMBER_OF_DIGITS);
        txt7.setBackground(Color.WHITE);
        txt8 = new JTextField(" ", NUMBER_OF_DIGITS);
        txt8.setBackground(Color.WHITE);
        txt9 = new JTextField(" ", NUMBER_OF_DIGITS);
        txt9.setBackground(Color.WHITE);
        txt10 = new JTextField(" ", NUMBER_OF_DIGITS);
        txt10.setBackground(Color.WHITE);
        txt11 = new JTextField(" ", NUMBER_OF_DIGITS);
        txt11.setBackground(Color.WHITE);
        txt12 = new JTextField(" ", NUMBER_OF_DIGITS);
        txt12.setBackground(Color.WHITE);
        txt13 = new JTextField(" ", NUMBER_OF_DIGITS);
        txt13.setBackground(Color.WHITE);
        txt14 = new JTextField(" ", NUMBER_OF_DIGITS);
        txt14.setBackground(Color.WHITE);
        txt15 = new JTextField(" ", NUMBER_OF_DIGITS);
        txt15.setBackground(Color.WHITE);
        txt16 = new JTextField(" ", NUMBER_OF_DIGITS);
        input = new JTextField(" ", 20);
        txt16.setBackground(Color.WHITE);
        input.setBackground(Color.WHITE);
        output = new JTextField(" ", 10);
        output.setBackground(Color.WHITE);

        JButton move = new JButton("MOVE");
        JButton sub = new JButton("SUB");
        JButton add = new JButton("ADD");
        JButton Do = new JButton("DO");
        JButton clr = new JButton("Clear");
   
        move.addActionListener(this);
        sub.addActionListener(this);
        add.addActionListener(this);
        Do.addActionListener(this);
        clr.addActionListener(this);
       
        jp1.add(txt1);
        jp1.add(txt2);
        jp1.add(txt3);
        jp1.add(txt4);
        jp1.add(txt5);
        jp1.add(txt6);
        jp1.add(txt7);
        jp1.add(txt8);
        jp1.add(txt9);
        jp1.add(txt10);
        jp1.add(txt11);
        jp1.add(txt12);
        jp1.add(txt13);
        jp1.add(txt14);
        jp1.add(txt15);
        jp1.add(txt16);
   
    //    jp2.add(move);
        jp2.add(sub);
        jp2.add(add);
       
        jp3.add(num);
        jp3.add(input);

        jp4.add(output);
       
        jp5.add(Do);
        jp5.add(clr);
       
        getContentPane().add(jp3);
        getContentPane().add(jp1);
        getContentPane().add(jp4);
        getContentPane().add(jp2);
        getContentPane().add(jp5);
    }
     public class ListenMenuQuit implements ActionListener{
        public void actionPerformed(ActionEvent e){
            System.exit(0);        
        }
    }
   
    public class ListenCloseWdw extends WindowAdapter{
        public void windowClosing(WindowEvent e){
            System.exit(0);        
        }
    }  
     public void assumingCorrectNumberFormats(ActionEvent e)
    {
        String actionCommand = e.getActionCommand();
       
        if(actionCommand.equals("DO"))
        {
            String s = input.getText();
            rem = s;
            ans = Integer.parseInt(s);
            xme = ans;
            //ans = k;   
            result = Integer.toBinaryString((ans));
            input.setText(s);
            if(result.length() <= 16)
            {
                int j = result.length();
                output.setText("Underflow");
               
                for(int i=0; i<=j; i++)
                {
                    char b = result.charAt(i);
                    String a = Character.toString(b);
                   
                    if(i==0)
                        txt1.setText(a);
                    else if(i==1)
                        txt2.setText(a);
                    else if(i==2)
                        txt3.setText(a);
                    else if(i==3)
                        txt4.setText(a);
                    else if(i==4)
                        txt5.setText(a);
                    else if(i==5)
                        txt6.setText(a);
                    else if(i==6)
                        txt7.setText(a);
                    else if(i==7)
                        txt8.setText(a);
                    else if(i==8)
                        txt9.setText(a);
                    else if(i==9)
                        txt10.setText(a);
                    else if(i==10)
                        txt11.setText(a);
                    else if(i==11)
                        txt12.setText(a);
                    else if(i==12)
                        txt13.setText(a);
                    else if(i==13)
                        txt14.setText(a);
                    else if(i==14)
                        txt15.setText(a);
                    else if(i==15)
                        txt16.setText(a);
                    else{}
                        
                    }
               
            }
            else
                output.setText("Overflow");
        }

        else if(actionCommand.equals("ADD"))
        {
            //input.setText("");
           
            String i = input.getText();
           
            int j = Integer.parseInt(i);
            ans = ans + j;
            xme = ans;   
            result = Integer.toBinaryString(ans);
            //input.setText(result);
            doAgain(xme);
           
        }
        else if(actionCommand.equals("SUB"))
        {
            String i = input.getText();
            int j = Integer.parseInt(i);
            ans = ans - j;
            xme = xme + ans;   
            result = Integer.toBinaryString(ans);
            //input.setText(result);
            doAgain(ans);
        }
        else if(actionCommand.equals("Convert"))
        {
            //int i = stringToInt(i);
            input.setText(result);
        }
        else if(actionCommand.equals("Clear"))
        {
            result = " ";
            ans = 0;
            output.setText(" ");
            input.setText("");
            txt1.setText(" ");
            txt2.setText(" ");
            txt3.setText(" ");
            txt4.setText(" ");
            txt5.setText(" ");
            txt6.setText(" ");
            txt7.setText(" ");
            txt8.setText(" ");
            txt9.setText(" ");
            txt10.setText(" ");
            txt11.setText(" ");
            txt12.setText(" ");
            txt13.setText(" ");
            txt14.setText(" ");
            txt15.setText(" ");
            txt16.setText(" ");
        }
        else
            input.setText("Unexperted error.");
    }
    public void actionPerformed(ActionEvent e)
    {
        try
        {
            assumingCorrectNumberFormats(e);
        }
        catch (NumberFormatException e2)
        {
            input.setText("Are sure? please re-enter #");
        }
    }
    private static double stringToInt(String stringObject)
    {
        return Integer.parseInt(stringObject);//stringObject.trim()
    }

    public void doAgain (int res){
        result = Integer.toBinaryString((res));
        txt1.setText(" ");
            txt2.setText(" ");
            txt3.setText(" ");
            txt4.setText(" ");
            txt5.setText(" ");
            txt6.setText(" ");
            txt7.setText(" ");
            txt8.setText(" ");
            txt9.setText(" ");
            txt10.setText(" ");
            txt11.setText(" ");
            txt12.setText(" ");
            txt13.setText(" ");
            txt14.setText(" ");
            txt15.setText(" ");
            txt16.setText(" ");
        if(result.length() <= 16)
            {
                int j = result.length();
                output.setText("Underflow");
               
                for(int i=0; i<=j; i++)
                {
                    char b = result.charAt(i);
                    String a = Character.toString(b);
                   
                    if(i==0)
                        txt1.setText(a);
                    else if(i==1)
                        txt2.setText(a);
                    else if(i==2)
                        txt3.setText(a);
                    else if(i==3)
                        txt4.setText(a);
                    else if(i==4)
                        txt5.setText(a);
                    else if(i==5)
                        txt6.setText(a);
                    else if(i==6)
                        txt7.setText(a);
                    else if(i==7)
                        txt8.setText(a);
                    else if(i==8)
                        txt9.setText(a);
                    else if(i==9)
                        txt10.setText(a);
                    else if(i==10)
                        txt11.setText(a);
                    else if(i==11)
                        txt12.setText(a);
                    else if(i==12)
                        txt13.setText(a);
                    else if(i==13)
                        txt14.setText(a);
                    else if(i==14)
                        txt15.setText(a);
                    else if(i==15)
                        txt16.setText(a);
                    else{}
                        
                    }
               
            }
            else
                output.setText("Overflow");
    }
   
}
   

No comments:

Post a Comment