How to Create A Basic Calculator In Java Applet In Advanced Way

    Share and Enjoy !

    Objective of this Approach:

    In my last post I have shown How to Create A Basic Calculator In Java Applet.
    That was too basic. The objective was to gain more knowledge on..

    Ads code goes here
    • how to add buttons
    • how to add controls
    • how to work with action listener

    In this post we are going in but advanced way..


    For creating a calculator, we need to define the requirement first.. These requirements will keep on adding whenever required. We will be cleared about how prototype model works.
    So will create a prototype first and keep implementing the calculator.

    Basic Requirements:

    The requirements are ..

    • There will be buttons with number 1 to 9
    • There will be simple arithmetic operation button like-‘+’,’-‘,’*’ and ‘/’
    • There will an equal(‘=’) button
    • The resultant will be displayed in a text box.

    How to Code:


     How to Create A Basic Calculator In Java Applet Here I have told the basics. Now in this post I will show how to code in a compact manner.
    For adding button 0 to 9.

    for(int i=0;i<10;i++  )
    {
    button[i]=new Button(""+i);
    //Naming of the button
    add(button[i]);
    //Adding the buttons
    button[i].addActionListener(this);
    //adding action listeners to those buttons
    }

    Now on the action performed section-

    //This section is bit tricky here we are finding from which button 
    //we are getting the request
    String output=event.getActionCommand();
    char firstCharacter= output.charAt(0);
    if(Character.isDigit(firstCharacter))
    text1.setText(text1.getText()+ output);
    //if the incoming character is digit we need to append
    //with the existing digits
    if(output.equals("+ "))
    {
    m=Integer.parseInt(text1.getText());
    //taking the number provided as first number
    key1="add";
    //Setting the key based on that later we will
    //perform operations
    text1.setText("");
    //Performing clean up activity
    }
    else if(output.equals("-"))
    {
    m=Integer.parseInt(text1.getText());
    key1="min";
    text1.setText("");
    }
    else if(output.equals("*"))
    {
    m=Integer.parseInt(text1.getText());
    key1="mul";
    text1.setText("");
    }
    else if(output.equals("/"))
    {
    m=Integer.parseInt(text1.getText());
    key1="div";
    text1.setText("");
    }
    else if(output.equals("C"))
    {
    //This is for clearing the screen.
    text1.setText("");
    }

    The working process of equals button.

    • It will take the second number
    • based on the key1  value it will determine the operation
    • It should also check the divide rule..the second number should not be zero.It should print error if second number is zero.
    • Later it should setup the resultant error.
    if(output.equals("="))
    {
    n=Integer.parseInt(text1.getText());
    //Getting the second number
    if(key1=="add")
    {
    //if key is add then add operation will be performed
    //on first number and second number
    res=m+n;
    }
    if(key1=="min")
    {
    res=m-n;
    }
    if(key1=="mul")
    {
    res=m*n;
    }
    if(key1=="div")
    {
    //Checking if the second number is 0
    //or not to avoid divided by zero error
    if(n!=0)
    res=m/n;
    else
    //if second number is zero then it will print error
    res1="Error";
    }
    if(res1.equals(""))
    //Setting up the result
    text1.setText("" +res);
    else
    text1.setText("" +res1);
    }
    }

    How real implementation will take place?:

    Let me write the complete code here..

    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    public class calculator_updated extends Applet implements ActionListener
    {
    //Initialize the text field
    TextField text1;
    //Create array of buttons
    Button button[]=new Button[10];
    //Create the buttons for special programs
    Button add,sub,mul,div,equalto,clr;
    String key1="",res1="";
    int m=0,res=0,n=0;
    public void init()
    {
    text1=new TextField(20);
    //craete a layout
    GridLayout layout=new GridLayout(4,5);
    setLayout(layout);
    add(text1);
    //adding the textfield
    for(int i=0;i<10;i++ )
    {
    button[i]=new Button("" +i);
    //Naming of the button
    add(button[i]);
    //Adding the buttons
    button[i].addActionListener(this);
    //adding action listeners to those buttons
    }
    add=new Button("+" " + ");
    sub=new Button("+" "-");
    mul=new Button("+" "*");
    div=new Button("+" "/");
    equalto=new Button("+" "=");
    clr=new Button("+" "C");
    //Adding the special buttons and adding action listeners to it
    add(add);
    add.addActionListener(this);
    add(sub);
    sub.addActionListener(this);
    add(mul);
    mul.addActionListener(this);
    add(div);
    div.addActionListener(this);
    add(equalto);
    equalto.addActionListener(this);
    add(clr);
    clr.addActionListener(this);

    }
    public void actionPerformed(ActionEvent event)
    {
    //This section is bit tricky here we are finding from which button
    //we are getting the request
    String output=event.getActionCommand();
    char firstCharacter= output.charAt(0);
    if(Character.isDigit(firstCharacter))
    text1.setText(text1.getText() +output);
    //if the incoming character is digit we need to append
    //with the existing digits
    if(output.equals("+ "))
    {
    m=Integer.parseInt(text1.getText());
    //taking the number provided as first number
    key1="add";
    //Setting the key based on that later we will
    //perform operations
    text1.setText("");
    //Performing clean up activity
    }
    else if(output.equals("-"))
    {
    m=Integer.parseInt(text1.getText());
    key1="min";
    text1.setText("");
    }
    else if(output.equals("*"))
    {
    m=Integer.parseInt(text1.getText());
    key1="mul";
    text1.setText("");
    }
    else if(output.equals("/"))
    {
    m=Integer.parseInt(text1.getText());
    key1="div";
    text1.setText("");
    }
    else if(output.equals("C"))
    {
    //This is for clearing the screen.
    text1.setText("");
    }


    if(output.equals("="))
    {
    n=Integer.parseInt(text1.getText());
    //Getting the second number
    if(key1=="add")
    {
    //if key is add then add operation will be performed
    //on first number and second number
    res=m+n;
    }
    if(key1=="min")
    {
    res=m-n;
    }
    if(key1=="mul")
    {
    res=m*n;
    }
    if(key1=="div")
    {
    //Checking if the second number is 0
    //or not to avoid divided by zero error
    if(n!=0)
    res=m/n;
    else
    //if second number is zero then it will print error
    res1="Error";
    }
    if(res1.equals(""))
    //Setting up the result
    text1.setText("" +res);
    else
    text1.setText("" +res1);
    }
    }
    }

    How to Call this from HTML?


    Well I have already explained this in my last post.Hence not going that much deep.
    The HTML code will look like-

    <HTML>
    <HEAD>
    </HEAD>
    <BODY>
    <div >
    <applet width=300 height=300 code="calculator_updated.class"> </applet>
    </APPLET>
    </div>
    </BODY>
    </HTML>
    Output??

    I tried to perform 2+3 by clicking on the 2 , then 3 and lastly =
    I have got 5.

    READ  Class DatagramPacket in Java

    Share and Enjoy !

    1 thought on “How to Create A Basic Calculator In Java Applet In Advanced Way”

    Leave a Comment

    Your email address will not be published. Required fields are marked *