Link to home
Start Free TrialLog in
Avatar of perdoname_
perdoname_

asked on

Code modification

Hey Experts,
Can anyone tell me which modifications can i do to the code below so not to look same as the original but to be the same effective ?????


Thanks in advance
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
 
public class Calculator extends Applet implements ActionListener
{
 
 
    private TextField   Panel;     
    private Button B0,   
                B1,    
                B2,    
                B3,    
                B4,    
                B5,    
                B6,    
                B7,    
                B8,    
                B9,    
                BDiv,  
                BMul,  
                BAdd,  
                BSub,  
                BResult,  
                B1p, 
                B2p, 
                BPoint,   
                BClear;    
    private int state, error;   
 
 
 
 
    public void init()
    {
        setBackground(Color.white); 
        setLayout(null);
       //resize(500, 500);
 
        state = 0;
        error = 0;
 
        Panel = new TextField();
        Panel.setBounds( 20, 20, 165, 25);
        add(Panel);
        B0 = new Button("0");
        B0.setBounds (20, 160, 25, 25);
        add(B0);
        B1 = new Button("1");
        B1.setBounds (20, 125, 25, 25);
        add(B1);
        B2 = new Button("2");
        B2.setBounds (55, 125, 25, 25);
        add(B2);
        B3 = new Button("3");
        B3.setBounds (90, 125, 25, 25);
        add(B3);
        B4 = new Button("4");
        B4.setBounds (20, 90, 25, 25);
        add(B4);
        B5 = new Button("5");
        B5.setBounds (55, 90, 25, 25);
        add(B5);
        B6 = new Button("6");
        B6.setBounds (90, 90, 25, 25);
        add(B6);
        B7 = new Button("7");
        B7.setBounds (20, 55, 25, 25);
        add(B7);
        B8 = new Button("8");
        B8.setBounds (55, 55, 25, 25);
        add(B8);
        B9 = new Button("9");
        B9.setBounds (90, 55, 25, 25);
        add(B9);
        BDiv = new Button("/");
        BDiv.setBounds (160, 90, 25, 25);
        add(BDiv);
        BMul = new Button("*");
        BMul.setBounds (125, 90, 25, 25);
        add(BMul);
        BAdd = new Button("+");
        BAdd.setBounds (125, 55, 25, 25);
        add(BAdd);
        BSub = new Button("-");
        BSub.setBounds (160, 55, 25, 25);
        add(BSub);
        BResult = new Button("=");
        BResult.setBounds (90, 160, 25, 25);
        add(BResult);
        B1p = new Button("(");
        B1p.setBounds (125, 125, 25, 25);
        add(B1p);
        B2p = new Button(")");
        B2p.setBounds (160, 125, 25, 25);
        add(B2p);
        BPoint = new Button(".");
        BPoint.setBounds (55, 160, 25, 25);
        add(BPoint);
        BClear = new Button("Clear");
        BClear.setBounds (115, 170, 70, 25);
        add(BClear);
 
        Panel.addActionListener(this);
        B0.addActionListener(this);
        B1.addActionListener(this);
        B2.addActionListener(this);
        B3.addActionListener(this);
        B4.addActionListener(this);
        B5.addActionListener(this);
        B6.addActionListener(this);
        B7.addActionListener(this);
        B8.addActionListener(this);
        B9.addActionListener(this);
        BDiv.addActionListener(this);
        BMul.addActionListener(this);
        BAdd.addActionListener(this);
        BSub.addActionListener(this);
        BResult.addActionListener(this);
        B1p.addActionListener(this);
        B2p.addActionListener(this);
        BPoint.addActionListener(this);
        BClear.addActionListener(this);
    }
 
 
 
/*
    Method:     paint
    Purpose:    Draw the outline of calculator *(this method can auto. draw when develop Applet, but it isn't Applet's method)
    Parameters: Graphics
    Returns:    None 
    */
   
    public void paint(Graphics graphic)
    {
       graphic.setColor(Color.black);
        
        graphic.fillRoundRect( 5, 5, 195, 200, 15, 15);
     }
 
    
    
    public void actionPerformed(ActionEvent event)
    {
        if (event.getSource() instanceof Button)
        {
            Button clickedButton = (Button) event.getSource();
            if (clickedButton == BClear)
            {
                ClearPanel();
                state = 0;
            }
                else if (clickedButton == BResult)
                {
                    error = 0;
                    Compute();
                    state = 1;
                }
                    else
                    {
                        if (state == 1)
                        {
                            ClearPanel();   //clear panel after calculation
                            state = 0;
                        }
                        DisplayEx(clickedButton);
                    }
        }
        else
        {
            error = 0;
            Compute();
            state = 1;
        }
    }
 
 
    /*
    Method:     ClearPanel
    Purpose:    Clear Panel TextField objects
    Parameters: None
    Returns:    None
    */
 
    private void ClearPanel()
    {
        Panel.setText("");
    }
 
 
    /*
    Method:     DisplayEx
    Purpose:    Display the whole expression that the user enters
    Parameters: clickedButton
    Returns:    None
    */
 
    private void DisplayEx(Button ClickedButton)
    {
        String tmpstr;
        tmpstr = Panel.getText();
        if (ClickedButton == B0)   Panel.setText(tmpstr+"0");
        if (ClickedButton == B1)   Panel.setText(tmpstr+"1");
        if (ClickedButton == B2)   Panel.setText(tmpstr+"2");
        if (ClickedButton == B3)   Panel.setText(tmpstr+"3");
        if (ClickedButton == B4)   Panel.setText(tmpstr+"4");
        if (ClickedButton == B5)   Panel.setText(tmpstr+"5");
        if (ClickedButton == B6)   Panel.setText(tmpstr+"6");
        if (ClickedButton == B7)   Panel.setText(tmpstr+"7");
        if (ClickedButton == B8)   Panel.setText(tmpstr+"8");
        if (ClickedButton == B9)   Panel.setText(tmpstr+"9");
        if (ClickedButton == BDiv) Panel.setText(tmpstr+"/");
        if (ClickedButton == BMul) Panel.setText(tmpstr+"*");
        if (ClickedButton == BAdd) Panel.setText(tmpstr+"+");
        if (ClickedButton == BSub) Panel.setText(tmpstr+"-");
        if (ClickedButton == B1p)Panel.setText(tmpstr+"(");
        if (ClickedButton == B2p)Panel.setText(tmpstr+")");
        if(ClickedButton == BPoint)Panel.setText(tmpstr+".");
    }
 
 
    /*
    Method:     Compute
    Purpose:    Calculate the expression and shown the result, when the user hit return key or "=" button.
    Parameters: The whole expression
    Returns:    None
    */
 
    private void Compute()
    {
        String  Tmpstr, Result="";
        char    ch;
        int i, no_of_char,
            no_of_par = 0;
 
        Tmpstr      = Panel.getText();
        no_of_char  = Tmpstr.length();
 
        //Expression brief checking
        for (i = 0; i < no_of_char; i++)
        {
            ch = Tmpstr.charAt(i);
            if (ch == ')') no_of_par--;
            if (no_of_par < 0) error = 1;
            if (ch == '(') no_of_par++;
            if (ch < '(' || ch > '9' || ch == ',') error = 2;
            if (ch == '.' && (i+1 < Tmpstr.length()) )
                for ( int j = i+1; (j < Tmpstr.length()) && ((Character.isDigit(Tmpstr.charAt(j))) || ((Tmpstr.charAt(j))) == '.'); j++ )
                    if (Tmpstr.charAt(j) == '.') error = 3;
                                //If an operand has more than one point return error
        }//End of expression brief checking
 
        if (no_of_par != 0) error = 1;        //If open and close parentheses do not match return error
 
        if (error != 0) Err_msg(error);     //An error perform to prompt error message
        else Result = Calculate(Tmpstr);        //No error perform to calculate expression
        if (error != 0) Err_msg(error);     //An error perform to prompt error message
        else Panel.setText(Result);         //No error show result
    }
 
 
    /*
    Method:     Calculate
    Purpose:    Implement the expression
    Parameters: Expression
    Returns:    None
    */
 
    private String Calculate(String expression)
    {
        String  result = expression, f_operand, r_operand;
        char    cha;
        int index, f_index, r_index,
            no_of_cha = result.length(),
            no_of_pare = 0, pare_match = 0, op_index = 0, cp_index = 0;
 
        if (error == 0)
        {
        //Checking Parentheses
        for (index = 0; index < no_of_cha; index++)
        {
            cha = result.charAt(index);
 
            if (cha == '(')
            {
                if (pare_match == 0)    op_index = index;
                pare_match ++;
                no_of_pare ++;
            }
 
            if (cha == ')')
            {
                pare_match --;
                if (pare_match == 0)    cp_index = index;
            }
        }//End of checking Parentheses
 
        if (op_index+1 == cp_index) error = 3;
 
        //Recursive Calculate, when parentheses existed
        if (error == 0 && no_of_pare > 0)
        {
        if ((op_index == 0) && (cp_index == (no_of_cha - 1)) && (op_index != cp_index)) result = Calculate(result.substring(op_index + 1, cp_index));
            else if (op_index == 0 && cp_index > 0)
            {
                if ( (Character.isDigit(result.charAt(cp_index+1))) ) error = 3;
                else
                {
                    result = Calculate(result.substring(op_index + 1, cp_index)) + result.substring(cp_index + 1);
                    no_of_pare--;
                    while(no_of_pare != 0)
                    {
                        result = Calculate(result);
                        no_of_pare--;
                    }
                }
            }
                else if ((op_index > 0) && (cp_index > 0) && (cp_index != no_of_cha -1))
                {
                   
                    if ( (Character.isDigit(result.charAt(cp_index+1))) || (Character.isDigit(result.charAt(op_index-1))) ) error = 3;
                    else
                    {
                        result = result.substring(0, op_index) + Calculate(result.substring(op_index +1, cp_index)) + result.substring(cp_index +1);
                        no_of_pare--;
                        while(no_of_pare != 0)
                        {
                            result = Calculate(result);
                            no_of_pare--;
                        }
                    }
                }
                    else if (cp_index == no_of_cha -1 && op_index > 0)
                    {
                        if ( (Character.isDigit(result.charAt(op_index-1))) ) error = 3;
                        else
                        {
                            result = result.substring(0, op_index) + Calculate(result.substring(op_index + 1, cp_index));
                            no_of_pare--;
                            while(no_of_pare != 0)
                            {
                                result = Calculate(result);
                                no_of_pare--;
                            }
                        }
                    }
        }//End of recursive Calculate statement
 
 
        //Implement algorithm
        if (no_of_pare == 0 && error == 0)
        {
            if ( (!(Character.isDigit(result.charAt(0))) && (result.charAt(0) != '-')) || !(Character.isDigit(result.charAt(result.length()-1))) ) error = 3;
 
            //Implement multiply and divide first
            for (index = 0; index < result.length() && (error == 0); index++)
            {
                cha = result.charAt(index);
 
                if (cha == '*' || cha == '/')
                {
                    if ( !(Character.isDigit(result.charAt(index-1))) || ( !(Character.isDigit(result.charAt(index+1))) && (result.charAt(index+1) != '-') ) ) error = 3;
                    if (result.charAt(index+1) == '-')
                        if ( !(Character.isDigit(result.charAt(index+2))) ) error = 3;
                    if (error == 0)
                    {
                        f_index = index - 1;
 
                        if (f_index > 2)
                            if ( ((result.charAt(f_index-1)) == '-') && ((result.charAt(f_index-2)) == 'E') )
                                f_index = f_index - 2;
 
                        while ( (f_index > 0) && ((Character.isDigit(result.charAt(f_index-1))) || ((result.charAt(f_index-1)) == '.') || ((result.charAt(f_index-1)) == 'E')) )
                        {
                            f_index--;
                        }
                        if (f_index ==1)
                            if ((result.charAt(f_index-1)) == '-')
                                f_index--;
                        if (f_index > 2)
                            if ( ((result.charAt(f_index-1)) == '-') && !(Character.isDigit(result.charAt(f_index-2))) )
                                f_index--;
                        f_operand = result.substring(f_index, index);
 
                        r_index = index + 1;
                        while ( (r_index < result.length()-1) && ((Character.isDigit(result.charAt(r_index+1))) || ((result.charAt(r_index+1)) == '.') || ((result.charAt(r_index+1)) == 'E')) )
                        {
                            r_index++;
                            if (r_index < result.length()-2)
                                if ( ((result.charAt(r_index)) == 'E') && ((result.charAt(r_index+1)) == '-') )
                                    r_index++;
                        }
                        r_operand = result.substring(index+1, r_index+1);
 
                        if ( (f_index != 0) && (r_index != result.length()-1) )
                        {
                            if (Algorithm(cha, f_operand, r_operand).charAt(0) == 'N') error = 4;             //If an answer is not a number return error
                            if (Algorithm(cha, f_operand, r_operand).charAt(0) == 'I') error = 5;             //If an answer is Infinity return error
 
                            result = result.substring(0, f_index) + Algorithm(cha, f_operand, r_operand) + result.substring(r_index+1);
                            index = 0;
                        }
                            else if ( (f_index == 0) && (r_index == result.length()-1) )
                            {
                                if (Algorithm(cha, f_operand, r_operand).charAt(0) == 'N') error = 4;         //If an answer is not a number return error
                                if (Algorithm(cha, f_operand, r_operand).charAt(0) == 'I') error = 5;         //If an answer is Infinity return error
 
                                result = Algorithm(cha, f_operand, r_operand);
                            }
                                else if (f_index == 0)
                                {
                                    if (Algorithm(cha, f_operand, r_operand).charAt(0) == 'N') error = 4;     //If an answer is not a number return error
                                    if (Algorithm(cha, f_operand, r_operand).charAt(0) == 'I') error = 5;     //If an answer is Infinity return error
 
                                    result = Algorithm(cha, f_operand, r_operand) + result.substring(r_index+1);
                                    index = 0;
                                }
                                    else if (r_index == result.length()-1)
                                    {
                                        if (Algorithm(cha, f_operand, r_operand).charAt(0) == 'N') error = 4; //If an answer is not a number return error
                                        if (Algorithm(cha, f_operand, r_operand).charAt(0) == 'I') error = 5; //If an answer is Infinity return error
 
                                        result = result.substring(0, f_index) + Algorithm(cha, f_operand, r_operand);
                                    }
                    }
                }
            }//End of implement multiply and divide
 
 
            //Implement add and subtract
            for (index = 0; index < result.length() && (error == 0); index++)
            {
                if (index == 0 && result.charAt(index) == '-') index = 1;
 
                if (index > 0)
                    if ( ((result.charAt(index)) == 'E') && ((result.charAt(index+1)) == '-') )
                        index = index + 2;
 
                cha = result.charAt(index);
 
                if (cha == '+' || cha == '-')
                {
                    if ( !(Character.isDigit(result.charAt(index-1))) || ( !(Character.isDigit(result.charAt(index+1))) && (result.charAt(index+1) != '-') ) ) error = 3;
                    if (result.charAt(index+1) == '-')
                        if ( !(Character.isDigit(result.charAt(index+2))) ) error = 3;
                    if (error == 0)
                    {
                        f_index = 0;
                        f_operand = result.substring(f_index, index);
 
                        r_index = index + 1;
                        while ( (r_index < result.length()-1) && ((Character.isDigit(result.charAt(r_index+1))) || ((result.charAt(r_index+1)) == '.') || ((result.charAt(r_index+1)) == 'E')) )
                        {
                            r_index++;
                            if (r_index < result.length()-2)                         
                                if ( ((result.charAt(r_index)) == 'E') && ((result.charAt(r_index+1)) == '-') )
                                    r_index++;
                        }
                        r_operand = result.substring(index+1, r_index+1);
                        result = Algorithm(cha, f_operand, r_operand) + result.substring(r_index+1);
                        index = 0;
                    }
                }
            }//End of implement add and subtract
 
        }//End of implement algorithm
 
        }
        return result;
    }
 
 
    /*
    Method:     Algorithm
    Purpose:    Implement the simple expression
    Parameters: Operator, front value and rear value
    Returns:    None
    */
 
    private String Algorithm(char Operator, String F_operand, String R_operand)
    {
        Double  F, R;
        double  f, r,
            ans = 0;
        String  res;
 
        F = new Double(F_operand);
        R = new Double(R_operand);
        f = F.doubleValue();
        r = R.doubleValue();
 
        if (Operator == '+') ans = f + r;
        if (Operator == '-') ans = f - r;
        if (Operator == '*') ans = f * r;
        if (Operator == '/') ans = f / r;
 
        res = Double.toString(ans);
 
        return res;
    }
 
 
    /*
    Method:     Err_msg
    Purpose:    Prompt error message
    Parameters: Error level
    Returns:    None
    */
 
    private void Err_msg(int error)
    {
        switch(error)
        {
            case 1:
                Panel.setText("Parentheses do not match");
                break;
            case 2:
                Panel.setText("Invalid input");
                break;
            case 3:
                Panel.setText("Invalid expression");
                break;
            case 4:
                Panel.setText("Not a number exist");
                break;
            case 5:
                Panel.setText("Infinity exist");
                break;
            default:
                Panel.setText("Unknow error");
                break;
        }
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of sciuriware
sciuriware

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of krakatoa
Bit of homework workaround?
Avatar of sciuriware
sciuriware

Homework? Where do they teach you how to ..............................

;JOOP!