Monday, January 9, 2012

Tutorial: Simple Calculator Program in Java

by eturo


I love doing experiments in programming on different programming languages I knew such as in Java. This is a simple calculator applet program I made last summer 2011.

As far as I know, an applet is a program which you can do in Java, and it can only executed or run in any Java compatible web browser such as Mozilla Firefox. An applet is also a Java program where you can build GUI applications such as the one below. (see image)

The Design:




The Program Source Code:


Here is the program source code I made to design or create the Calculator applet above. It's free guys so you can copy it and try to put in your Java text editor such as using BlueJ or NetBeans IDE. You can modify or change the code if you want too so see some different results.


//Import all library package needed by the program
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class Calculator extends Applet implements ActionListener
{
    //naming the objects
    Button btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn0;
    Button btnPlus,btnMinus,btnMultiply,btnDivide;
    Button equal,deci,clear,posneg;
    TextField txtInput;
    double num1,num2,result,posineg;
    int plus,minus,multiply,divide,decimal;
    Font font;

    public void init()
    {
        //initialization of objects or instances
        setLayout(null);
        setSize(340,450);
        setBackground(new Color(248,248,255));
        font = new Font("Tahoma", Font.BOLD, 32);
        setFont(font);
       
        txtInput = new TextField(10);
        txtInput.setEditable(false);
        txtInput.setBounds(20,40,300,50);
        txtInput.setFont(new Font("Arial",Font.BOLD,36));
        txtInput.setForeground(Color.red);
        add(txtInput);
       
        btn1 = new Button("7");
        btn1.setForeground(Color.blue);
        btn1.setBounds(20,100,70,50);
        add(btn1);
        btn2 = new Button("8");
        btn2.setForeground(Color.blue);
        btn2.setBounds(100,100,70,50);
        add(btn2);
        btn3 = new Button("9");
        btn3.setForeground(Color.blue);
        btn3.setBounds(180,100,70,50);
        add(btn3);
        btnPlus = new Button("+");
        btnPlus.setForeground(Color.red);
        btnPlus.setBounds(260,100,60,50);
        add(btnPlus);
       
        btn4 = new Button("4");
        btn4.setForeground(Color.blue);
        btn4.setBounds(20,160,70,50);
        add(btn4);
        btn5 = new Button("5");
        btn5.setForeground(Color.blue);
        btn5.setBounds(100,160,70,50);
        add(btn5);
        btn6 = new Button("6");
        btn6.setForeground(Color.blue);
        btn6.setBounds(180,160,70,50);
        add(btn6);
        btnMinus = new Button("-");
        btnMinus.setForeground(Color.red);
        btnMinus.setBounds(260,160,60,50);
        add(btnMinus);
       
        btn7 = new Button("1");
        btn7.setForeground(Color.blue);
        btn7.setBounds(20,220,70,50);
        add(btn7);
        btn8 = new Button("2");
        btn8.setForeground(Color.blue);
        btn8.setBounds(100,220,70,50);
        add(btn8);
        btn9 = new Button("3");
        btn9.setForeground(Color.blue);
        btn9.setBounds(180,220,70,50);
        add(btn9);
        btnMultiply= new Button("*");
        btnMultiply.setForeground(Color.red);
        btnMultiply.setBounds(260,220,60,50);
        add(btnMultiply);
       
        btn0 = new Button("0");
        btn0.setForeground(Color.blue);
        btn0.setBounds(20,280,70,50);
        add(btn0);
        posneg = new Button("-/+");
        posneg.setForeground(Color.green);
        posneg.setBounds(180,280,70,50);
        add(posneg);
        deci = new Button(".");
        deci.setForeground(Color.red);
        deci.setBounds(100,280,70,50);
        add(deci);         
        btnDivide = new Button("/");
        btnDivide.setForeground(Color.red);
        btnDivide.setBounds(260,280,60,50);
        add(btnDivide);
       
        clear = new Button("CLEAR");
        clear.setBounds(20,340,150,80);
        clear.setForeground(Color.red);
        add(clear);
        equal = new Button("=");
        equal.setBounds(180,340,140,80);
        equal.setForeground(Color.red);
        add(equal);
       
        //registering all the objects who will invoke methods
        btn1.addActionListener(this);
        btn2.addActionListener(this);
        btn3.addActionListener(this);
        btn4.addActionListener(this);
        btn5.addActionListener(this);
        btn6.addActionListener(this);
        btn7.addActionListener(this);
        btn8.addActionListener(this);
        btn9.addActionListener(this);
        btn0.addActionListener(this);
        equal.addActionListener(this);
        btnPlus.addActionListener(this);
        btnMinus.addActionListener(this);
        btnMultiply.addActionListener(this);
        btnDivide.addActionListener(this);
        deci.addActionListener(this);
        clear.addActionListener(this);
        posneg.addActionListener(this);
       
    }
   
    public void actionPerformed(ActionEvent e)
    {
        //print or display status in the applet window
        showStatus("Author: Vilchor G. Perdido");
       
        //actions or events when the buttons were clicked
        if(e.getSource()==btn1)
        {
            txtInput.setText("" + txtInput.getText() + "" + btn1.getLabel());         
        }
        else if(e.getSource()==btn2)
        {
             txtInput.setText("" + txtInput.getText() + "" + btn2.getLabel());
        }
        else if(e.getSource()==btn3)
        {
             txtInput.setText("" + txtInput.getText() + "" + btn3.getLabel());
        }
        else if(e.getSource()==btn4)
        {
             txtInput.setText("" + txtInput.getText() + "" + btn4.getLabel());
        }
        else if(e.getSource()==btn5)
        {
             txtInput.setText("" + txtInput.getText() + "" + btn5.getLabel());
        }
        else if(e.getSource()==btn6)
        {
             txtInput.setText("" + txtInput.getText() + "" + btn6.getLabel());
        }
        else if(e.getSource()==btn7)
        {
             txtInput.setText("" + txtInput.getText() + "" + btn7.getLabel());
        }
        else if(e.getSource()==btn8)
        {
             txtInput.setText("" + txtInput.getText() + "" + btn8.getLabel());
        }
        else if(e.getSource()==btn9)
        {
             txtInput.setText("" + txtInput.getText() + "" + btn9.getLabel());
        }
        else if(e.getSource()==btn0)
        {
             txtInput.setText("" + txtInput.getText() + "" + btn0.getLabel());
        }
        else if(e.getSource()==posneg)
        {
             posineg = Double.parseDouble(txtInput.getText());
             posineg = posineg * (-1);
             txtInput.setText(""+posineg);
        }
       
       
        if(e.getSource()==btnPlus)
        {
            num1 = Double.parseDouble(txtInput.getText());
            txtInput.setText("");
            plus=1;
            decimal=0;
           
        }
        else if(e.getSource()==btnMinus)
        {
            num1 = Double.parseDouble(txtInput.getText());
            txtInput.setText("");
            minus=1;
            decimal=0;
           
        }
        else if(e.getSource()==btnMultiply)
        {
            num1 = Double.parseDouble(txtInput.getText());
            txtInput.setText("");
            multiply=1;
            decimal=0;
           
        }
        else if(e.getSource()==btnDivide)
        {
            num1 = Double.parseDouble(txtInput.getText());
            txtInput.setText("");
            divide=1;
            decimal=0;
        }
        else if(e.getSource()==deci)
        {
            if(decimal==0)
            {
                txtInput.setText("" + txtInput.getText() + "" + deci.getLabel());
                decimal=1;
            }
        }
        else if(e.getSource()==clear)
        {
                txtInput.setText("");
                decimal = 0;
                num1 = 0;
                num2 = 0;
                result = 0;
        }

        if(e.getSource()==equal)
        {
            num2 =  Double.parseDouble(txtInput.getText());
            if(plus>0)
            {
                result = num1 + num2;
                txtInput.setText(""+result);
                num1=0;
                num2=0;
                plus=0;
                decimal = 1;
            }
           
            if(minus>0)
            {
                result = num1 - num2;
                txtInput.setText(""+result);
                num1=0;
                num2=0;
                minus=0;
                decimal = 1;
            }
            else if(multiply>0)
            {
                result = num1 * num2;
                txtInput.setText(""+result);
                num1=0;
                num2=0;
                multiply=0;
                decimal = 1;
            }
            else if(divide>0)
            {
                result = num1 / num2;
                txtInput.setText(""+result);
                num1=0;
                num2=0;
                divide=0;
                decimal = 1;
            }
        }
    }
}



Note: Using the BlueJ text editor in running your program, you can use an appletviewer to see the output of your applet program. The appletviewer is built-in, meaning that once you install the JDK for Java, it will automatically set as the default in running all applet programs.




Like Us on Facebook

Related Posts Plugin for WordPress, Blogger...