Thursday, May 23, 2013

application of Calculator using Applet.



Aim: Write an application of Calculator using Applet.

package bc;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class calc extends Applet implements ActionListener
{
Button keysArray[];
Panel keyPad;
TextField Field;
double result;
boolean first;
static boolean clearText;
int prevOpe;

public void init()
{
Field = new TextField(20);
keyPad = new Panel ();
keysArray = new Button[17];
result = 0.0;
prevOpe = 0;
first = true;
clearText = true;

//Create buttons
for (int i = 0; i <=9; i++)
keysArray[i] = new Button(String.valueOf(i));
keysArray[10] = new Button("/");
keysArray[11] = new Button("*");
keysArray[12] = new Button("-");
keysArray[13] = new Button("+");
keysArray[14] = new Button("=");
keysArray[15] = new Button(".");
keysArray[16] = new Button("CLR");

//Set panel layout manager
keyPad.setLayout(new GridLayout (4,4));

//Add button to keyPad panel
for (int i = 7; i <=10; i++) //adds Button 7,8,9, and divide to Panel
keyPad.add(keysArray[i]);

for (int i = 4; i <=6; i++)
keyPad.add(keysArray[i]);

keyPad.add(keysArray[11]);

for (int i = 1; i <= 3;i++)
keyPad.add(keysArray[i]);

keyPad.add(keysArray[12]);

keyPad.add(keysArray[0]);

for (int i = 15; i >=13; i--)
keyPad.add(keysArray[i]); //adds decimal point, equal, and add keys Panel

add(Field, BorderLayout.NORTH); //adds text field to top of Frame
add(keyPad, BorderLayout.CENTER); //center
add(keysArray[16], BorderLayout.EAST); //right side

for(int i = 0; i < keysArray.length; i++)
keysArray[i].addActionListener(this);
}

public void actionPerformed(ActionEvent e)
{

//Search for the key pressed
for (int i = 0; i < keysArray.length; i++)
{
if(e.getSource() == keysArray[i]) //key match found
{
            final double result1 = result;
switch(i)
{
case 0: case 1: case 2: case 3: case 4: //number buttons
case 5: case 6: case 7: case 8: case 9: //0-9
case 15:
    if (clearText)
    {
        Field.setText("");
        clearText = false;
    }
    Field.setText(Field.getText() + keysArray[i].getLabel());
    break;

case 10:// divide button
case 11:// multiply button
case 12:// minus button
case 13:// plus button
case 14:// equal button
    clearText = true;
    if (first) // First operand
    {
        if(Field.getText().length()==0)
            result = 0.0;
        else
            result = Double.valueOf(Field.getText()).doubleValue();
        first = false;
        prevOpe = i; //save previous operator
    }
    else //second operand already enter, so calculator total
    {
        switch(prevOpe)
        {
        case 10: //divide Button
            result /= Double.valueOf(Field.getText()).doubleValue();
            break;
        case 11: //multiply Button
            result *= Double.valueOf(Field.getText()).doubleValue();
            break;
        case 12: //minus button
            result -= Double.valueOf(Field.getText()).doubleValue();
            break;
        case 13: //plus button
            result += Double.valueOf(Field.getText()).doubleValue();
            break;
        }
        Field.setText(Double.toString(result));
        if (i==14)//equal button
            first = true;
        else
            prevOpe = i; //save previous opetator
    }
    break;

case 16://Clear button
    clearText = true;
    first = true;
    Field.setText("");
    result = 0.0;
    prevOpe = 0;
    break;
}
}
}
}
}


No comments:

Post a Comment