How to Create A Basic Calculator In Java Applet

    Share and Enjoy !

    Objective:

    In my last post I have shown how to start with Applet.But in this post we will create a simple calculator.
    In this example we will understand more on

    Ads code goes here
    • this keyword
    • ActionListener
    • And how to create Applet with buttons
    • Layout

    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.
    Basic Structure Creation:

    Let us build this requirements first and create a basic structure.
    to create button we can use the code as

    Button button1=new Button("One")

    To add it to the container, we will be using

    add(button0);

    And to define action against a button we will be using

    button0.addActionListener(this);

    Now need to define inside the actionPerformed function how this function has to trigger when it is clicked..

    if(event.getSource()==button1)
    {
    text1.setText(text1.getText()+"1");
    //this step is to take more than one digit.
    }

    Similarly we will define what will happen , when we press “+”,”-“,”*” and “/” button.

    if(event.getSource()==button10){
    key1="plus";
    j=Integer.parseInt(text1.getText());
    text1.setText("");
    }
    if(event.getSource()==button11){
    key1="min";
    j=Integer.parseInt(text1.getText());
    text1.setText("");
    }
    if(event.getSource()==button12){
    key1="mul";
    j=Integer.parseInt(text1.getText());
    text1.setText("");
    }
    if(event.getSource()==button13){
    key1="div";
    j=Integer.parseInt(text1.getText());
    text1.setText("");
    }
    if(event.getSource()==button14){
    m=Integer.parseInt(text1.getText());
    if(key1.equals("plus"))
    {
    int res=j m;
    text1.setText("" +res);
    }
    if(key1.equals("min"))
    {
    int res=j-m;
    text1.setText(""+ res);
    }
    if(key1.equals("mul"))
    {
    int res=(j*m);
    text1.setText("" +res);
    }
    if(key1.equals("div") )
    {
    if(m!=0)
    {
    //this is to handle divide by zero error
    int res=(j/m);
    text1.setText("" +res);
    }
    else
    //if divide by zero error comes how to show the error message.
    text1.setText("Error");

    }

    }
    How the real implementation will happen?

    Let me write the complete code here..

    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    public class calculator extends Applet implements ActionListener
    {
    public TextField text1;
    //declare textfield
    public Button button1,button2,button3,button4,button5,button6,
    button7,button8,button9,button0;
    //declare buttons with numbers
    public Button button10,button11,button12,button13,button14,button15;
    int j=0,k=0,l=0,m=0;
    String key1;
    //button10 is special button performs operation
    //button11 is special button performs operation -
    //button12 is special button performs operation *
    //button13 is special button performs operation /
    public void init()
    {
    setLayout (new GridLayout(5, 3));
    //setting grid layout to show in a nice grid
    // Will create a layout with Grid structure with 5 rows and 3 columns but there will be no space
    text1=new TextField(20);
    //create a textfield of length 20
    add(text1);
    //adding textfield


    //in the below section I will create the buttons one by one. 
    //I will add the buttons to the container and add actions to it.

    button0=new Button("0");
    add(button0);
    button0.addActionListener(this);
    button1=new Button("1");
    add(button1);
    button1.addActionListener(this);
    button2=new Button("2");
    add(button2);
    button2.addActionListener(this);
    button3=new Button("3");
    add(button3);
    button3.addActionListener(this);
    button4=new Button("4");
    add(button4);
    button4.addActionListener(this);
    button5=new Button("5");
    add(button5);
    button5.addActionListener(this);
    button6=new Button("6");
    add(button6);
    button6.addActionListener(this);
    button7=new Button("7");
    add(button7);
    button7.addActionListener(this);
    button8=new Button("8");
    add(button8);
    button8.addActionListener(this);
    button9=new Button("9");
    add(button9);
    button9.addActionListener(this);
    button10=new Button(""+"+");
    add(button10);
    button10.addActionListener(this);
    button11=new Button("" +"-");
    add(button11);
    button11.addActionListener(this);
    button12=new Button("" +"*");
    add(button12);
    button12.addActionListener(this);
    button13=new Button("" +"/");
    add(button13);
    button13.addActionListener(this);
    button14=new Button("" +"=");
    add(button14);
    button14.addActionListener(this);
    button15=new Button("" +"CLR");
    add(button15);
    button15.addActionListener(this);
    }
    public void actionPerformed(ActionEvent event)
    {

    //this section is mostly getting the source of the event.
    //That is this funstion is triggered from which button.
    //Once the source is known then what should get as out put
    if(event.getSource()==button1){

    text1.setText(text1.getText()+ "1");
    //this step is to take more than one digit.
    }
    if(event.getSource()==button3){
    text1.setText(text1.getText()+"3");
    }
    if(event.getSource()==button2)
    {
    text1.setText(text1.getText()+"2");
    }
    if(event.getSource()==button4){
    text1.setText(text1.getText()+"4");
    }
    if(event.getSource()==button5){
    text1.setText(text1.getText()+"5");
    }
    if(event.getSource()==button6){
    text1.setText(text1.getText()+"6");
    }
    if(event.getSource()==button7){
    text1.setText(text1.getText()+"7");
    }
    if(event.getSource()==button8){
    text1.setText(text1.getText()+"8");
    }
    if(event.getSource()==button9){
    text1.setText(text1.getText()+"9");
    }
    if(event.getSource()==button0){
    text1.setText(text1.getText()+"0");
    }
    if(event.getSource()==button15){
    text1.setText("");
    }
    //Now we will look into the special button's actions
    //if this function is triggered from plus,minus,div,mul
    //then we must preserved the last keyed amount
    if(event.getSource()==button10){
    key1="plus";
    j=Integer.parseInt(text1.getText());
    text1.setText("");
    }
    if(event.getSource()==button11){
    key1="min";
    j=Integer.parseInt(text1.getText());
    text1.setText("");
    }
    if(event.getSource()==button12){
    key1="mul";
    j=Integer.parseInt(text1.getText());
    text1.setText("");
    }
    if(event.getSource()==button13){
    key1="div";
    j=Integer.parseInt(text1.getText());
    text1.setText("");
    }
    if(event.getSource()==button14){
    m=Integer.parseInt(text1.getText());
    if(key1.equals("plus"))
    {
    int res=j+m;
    text1.setText("" +res);
    }
    if(key1.equals("min"))
    {
    int res=j-m;
    text1.setText("" +res);
    }
    if(key1.equals("mul"))
    {
    int res=(j*m);
    text1.setText("" +res);
    }
    if(key1.equals("div") )
    {
    if(m!=0)
    {
    //this is to handle divide by zero error
    int res=(j/m);
    text1.setText("" +res);
    }
    else
    //if divide by zero error comes how to show the error message.
    text1.setText("Error");

    }
    }
    }
    }

    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.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.

    I am done from my side. enjoying.Happy Coding!!.You can also check the advanced coding here

    READ  How To Code Advanced Search In Java

    Share and Enjoy !

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

    Leave a Comment

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