Calculator App with Source Code | CodeTextPro

Calculator App

OBJECTIVE:
Our project design for a simple calculator will be as follows-
Enter your first digit
Select Arithmetic operation(+,-,/,*)
Enter your second digit
The results……..

Our calculator also handles memory functions, means m, m+, m-, mc.
We can also calculate the percentage of any digits.
We can also handle floating numbers by using points.



DESCRIPTION:


In this project, we have tried to make a simple calculator using an android studio. The calculator has the following functions: addition, subtraction, multiplication, division, memory button, memory addition, memory subtraction, memory clear, percentage, clear and equal. The code by which we have made the application is as follows.


cl.2
<resources>
    <string name="app_name">Simple Calculator</string>
    <string name="menu_settings">Settings</string>
    <string name="action_settings">Settings</string>
    <string name="button0">0</string>
    <string name="button1">1</string>
    <string name="button2">2</string>
    <string name="button3">3</string>
    <string name="button4">4</string>
    <string name="button5">5</string>
    <string name="button6">6</string>
    <string name="button7">7</string>
    <string name="button8">8</string>
    <string name="button9">9</string>
    <string name="buttonAdd">+</string>
    <string name="buttonSubtract">-</string>
    <string name="buttonMultiply">*</string>
    <string name="buttonDivide">/</string>

    <string name="buttonDecimalPoint">.</string>

    <string name="buttonEquals">=</string>
    <string name="buttonClear">C</string>
    <string name="buttonClearMemory">MC</string>
    <string name="buttonAddToMemory">M+</string>
    <string name="buttonSubtractFromMemory">M-</string>

</resources>


cl.3
package com.example.user.myapplication;

import android.app.Activity;

import android.os.Bundle;
import java.text.DecimalFormat;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener{

    private TextView mCalculatorDisplay;
    private Boolean userIsInTheMiddleOfTypingANumber = false;
    private CalculatorBrain mCalculatorBrain;
    private static final String DIGITS = "0123456789.";

    DecimalFormat df = new DecimalFormat("@###########");


    @SuppressLint("NewApi")

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // hide the window title.

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // hide the status bar and other OS-level chrome
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        mCalculatorBrain = new CalculatorBrain();

        mCalculatorDisplay = (TextView) findViewById(R.id.textView1);

        df.setMinimumFractionDigits(0);

        df.setMinimumIntegerDigits(1);
        df.setMaximumIntegerDigits(8);

        findViewById(R.id.button0).setOnClickListener(this);

        findViewById(R.id.button1).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
        findViewById(R.id.button3).setOnClickListener(this);
        findViewById(R.id.button4).setOnClickListener(this);
        findViewById(R.id.button5).setOnClickListener(this);
        findViewById(R.id.button6).setOnClickListener(this);
        findViewById(R.id.button7).setOnClickListener(this);
        findViewById(R.id.button8).setOnClickListener(this);
        findViewById(R.id.button9).setOnClickListener(this);

        findViewById(R.id.buttonAdd).setOnClickListener(this);

        findViewById(R.id.buttonSubtract).setOnClickListener(this);
        findViewById(R.id.buttonMultiply).setOnClickListener(this);
        findViewById(R.id.buttonDivide).setOnClickListener(this);

        findViewById(R.id.buttonDecimalPoint).setOnClickListener(this);

        findViewById(R.id.buttonEquals).setOnClickListener(this);
        findViewById(R.id.buttonClear).setOnClickListener(this);
        findViewById(R.id.buttonClearMemory).setOnClickListener(this);
        findViewById(R.id.buttonAddToMemory).setOnClickListener(this);
        findViewById(R.id.buttonSubtractFromMemory).setOnClickListener(this);




    }


    @Override

    public void onClick(View v) {

        String buttonPressed = ((Button) v).getText().toString();


        if (DIGITS.contains(buttonPressed)) {


            // digit was pressed

            if (userIsInTheMiddleOfTypingANumber) {

                if (buttonPressed.equals(".") && mCalculatorDisplay.getText().toString().contains(".")) {

                    // ERROR PREVENTION
                    // Eliminate entering multiple decimals
                } else {
                    mCalculatorDisplay.append(buttonPressed);
                }

            } else {


                if (buttonPressed.equals(".")) {

                    // ERROR PREVENTION
                    // This will avoid error if only the decimal is hit before an operator, by placing a leading zero
                    // before the decimal
                    mCalculatorDisplay.setText(0 + buttonPressed);
                } else {
                    mCalculatorDisplay.setText(buttonPressed);
                }

                userIsInTheMiddleOfTypingANumber = true;

            }

        } else {

            // operation was pressed
            if (userIsInTheMiddleOfTypingANumber) {

                mCalculatorBrain.setOperand(Double.parseDouble(mCalculatorDisplay.getText().toString()));

                userIsInTheMiddleOfTypingANumber = false;
            }

            mCalculatorBrain.performOperation(buttonPressed);

            mCalculatorDisplay.setText(df.format(mCalculatorBrain.getResult()));

        }


    }


    @Override

    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // Save variables on screen orientation change
        outState.putDouble("OPERAND", mCalculatorBrain.getResult());
        outState.putDouble("MEMORY", mCalculatorBrain.getMemory());
    }

    @Override

    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        // Restore variables on screen orientation change
        mCalculatorBrain.setOperand(savedInstanceState.getDouble("OPERAND"));
        mCalculatorBrain.setMemory(savedInstanceState.getDouble("MEMORY"));
        mCalculatorDisplay.setText(df.format(mCalculatorBrain.getResult()));
    }

}



cl.java
package com.example.user.myapplication;
public class CalculatorBrain {
    // 3 + 6 = 9
    // 3 & 6 are called the operand.
    // The + is called the operator.
    // 9 is the result of the operation.
    private double mOperand;
    private double mWaitingOperand;
    private String mWaitingOperator;
    private double mCalculatorMemory;

    // operator types

    public static final String ADD = "+";
    public static final String SUBTRACT = "-";
    public static final String MULTIPLY = "*";
    public static final String DIVIDE = "/";

    public static final String CLEAR = "C";

    public static final String CLEARMEMORY = "MC";
    public static final String ADDTOMEMORY = "M+";
    public static final String SUBTRACTFROMMEMORY = "M-";


    // public static final String EQUALS = "=";


    // constructor

    public CalculatorBrain() {
        // initialize variables upon start
        mOperand = 0;
        mWaitingOperand = 0;
        mWaitingOperator = "";
        mCalculatorMemory = 0;
    }

    public void setOperand(double operand) {

        mOperand = operand;
    }

    public double getResult() {

        return mOperand;
    }

    // used on screen orientation change

    public void setMemory(double calculatorMemory) {
        mCalculatorMemory = calculatorMemory;
    }

    // used on screen orientation change

    public double getMemory() {
        return mCalculatorMemory;
    }

    public String toString() {

        return Double.toString(mOperand);
    }

    protected double performOperation(String operator) {


        /*

         * If you are using Java 7, then you can use switch in place of if statements
         *
         *     switch (operator) {
         *     case CLEARMEMORY:
         *         calculatorMemory = 0;
         *         break;
         *     case ADDTOMEMORY:
         *         calculatorMemory = calculatorMemory + operand;
         *         break;
         *     etc...
         *     }
         */

        if (operator.equals(CLEAR)) {

            mOperand = 0;
            mWaitingOperator = "";
            mWaitingOperand = 0;
            // mCalculatorMemory = 0;
        } else if (operator.equals(CLEARMEMORY)) {
            mCalculatorMemory = 0;
        } else if (operator.equals(ADDTOMEMORY)) {
            mCalculatorMemory = mCalculatorMemory + mOperand;
        } else if (operator.equals(SUBTRACTFROMMEMORY)) {
            mCalculatorMemory = mCalculatorMemory - mOperand;
        } else {
            performWaitingOperation();
            mWaitingOperator = operator;
            mWaitingOperand = mOperand;
        }

        return mOperand;

    }

    protected void performWaitingOperation() {


        if (mWaitingOperator.equals(ADD)) {

            mOperand = mWaitingOperand + mOperand;
        } else if (mWaitingOperator.equals(SUBTRACT)) {
            mOperand = mWaitingOperand - mOperand;
        } else if (mWaitingOperator.equals(MULTIPLY)) {
            mOperand = mWaitingOperand * mOperand;
        }
          else if(mWaitingOperand.equals(PERCENT))  {
            ***************************************************
        }

         else if (mWaitingOperator.equals(DIVIDE)) {

            if (mOperand != 0) {
                mOperand = mWaitingOperand / mOperand;
            }
        }

    }


}




Simple Android Calculator App


Source Code:

xml file's code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="ExtraText">
<EditText
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#00f"
        android:textColor="#f2f7f9"
android:id="@+id/edt1" />
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/b1"
        android:layout_marginTop="94dp"
        android:layout_marginLeft="50dp"
        android:text="1"
        android:textStyle="bold"/>
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/b2"
        android:layout_toRightOf="@id/b1"
        android:layout_marginTop="94dp"
        android:text="2"
        android:textStyle="bold"/>
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/b3"
        android:layout_toRightOf="@id/b2"
        android:layout_marginTop="94dp"
        android:text="3"
        android:textStyle="bold"/>
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/b4"
         android:layout_toRightOf="@id/b3"
        android:layout_marginTop="94dp"
        android:layout_marginLeft="45dp"
        android:text="+"
        android:textStyle="bold"/>
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/b5"
        android:layout_below="@id/b1"
        android:layout_marginLeft="50dp"
        android:text="4"
        android:textStyle="bold"/>
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/b6"
        android:text="5"
        android:layout_toRightOf="@id/b5"
        android:layout_below="@id/b2"
        android:textStyle="bold"/>
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/b7"
        android:layout_toRightOf="@id/b6"
        android:layout_below="@id/b3"
        android:text="6"
        android:textStyle="bold"/>
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/b8"
        android:layout_toRightOf="@id/b7"
        android:layout_marginLeft="45dp"
        android:layout_below="@id/b4"
        android:text="-"
        android:textStyle="bold"/>
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/b9"
        android:layout_marginLeft="50dp"
        android:layout_below="@id/b5"
        android:text="7"
        android:textStyle="bold"/>
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/b10"
        android:layout_toRightOf="@id/b9"
        android:layout_below="@id/b6"
        android:text="8"
        android:textStyle="bold"/>
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/b10"
        android:layout_below="@id/b7"
        android:text="9"
        android:id="@+id/b11"
        android:textStyle="bold"/>
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/b12"
        android:layout_toRightOf="@id/b11"
        android:layout_marginLeft="45dp"
        android:layout_below="@id/b8"
        android:text="*"
        android:textStyle="bold"/>
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/b13"
        android:layout_marginLeft="50dp"
        android:layout_below="@id/b9"
        android:text="."
        android:textStyle="bold"
        android:textSize="25sp"/>
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:layout_toRightOf="@id/b13"
        android:layout_below="@id/b10"
        android:id="@+id/b14"
        android:text="0"
        android:textStyle="bold"/>
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/b15"
        android:layout_below="@id/b11"
        android:layout_toRightOf="@id/b14"
        android:text="c"
        android:textStyle="bold"/>
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/b16"
        android:layout_toRightOf="@id/b15"
        android:layout_marginLeft="45dp"
        android:layout_below="@id/b12"
       android:text="/"
        android:textStyle="bold"/>
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/b17"
        android:layout_marginLeft="50dp"
        android:layout_below="@id/b13"
        android:text="M+"
        android:textStyle="bold"/>
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/b18"
        android:layout_toRightOf="@id/b17"
        android:layout_below="@id/b14"
        android:text="M-"/>
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:layout_toRightOf="@id/b18"
        android:layout_below="@id/b15"
        android:text="MC"
        android:id="@+id/b19"
        android:textStyle="bold"/>
<Button
        android:id="@+id/b20"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="130dp"
        android:text="="
        android:background="#f9b31b"
        android:textStyle="bold"
        android:layout_below="@id/b17"
        android:layout_marginTop="45dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        />
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/b21"
        android:layout_below="@id/b16"
        android:layout_toRightOf="@id/b19"
        android:layout_marginLeft="45dp"
        android:text="%"
        android:textStyle="bold"/>

</RelativeLayout>




java file's code:
package com.example.user.calculator2;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;
public class MainActivity extends Activity {
 Button  button1 , button2 , button3 , button4 , button5 , button6 ,
 button7 , button8 , button9 , buttonAdd , buttonSub , buttonDivision ,
            buttonMul,button10,button11,buttonC buttonEqual,buttonMadd,buttonMsub,buttonMcan,buttonPer ;          
  EditText edt1 ;
float ValueOne , ValueTwo ;
 boolean Addition , Subtract ,Multiplication ,Division,percentage ;
 ArrayList<Double> tempfigure = new ArrayList<Double>();
 double tempfigure1;
 double memory1=0;
 @Override
  protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
        edt1=(EditText) findViewById(R.id.edt1);
        button1 = (Button) findViewById(R.id.b1);
        button2 = (Button) findViewById(R.id.b2);
        button3 = (Button) findViewById(R.id.b3);
        buttonAdd = (Button) findViewById(R.id.b4);
        button4 = (Button) findViewById(R.id.b5);
        button5 = (Button) findViewById(R.id.b6);
        button6 = (Button) findViewById(R.id.b7);
        buttonSub = (Button) findViewById(R.id.b8);
        button7 = (Button) findViewById(R.id.b9);
        button8 = (Button) findViewById(R.id.b10);
        button9 = (Button) findViewById(R.id.b11);
        buttonMul = (Button) findViewById(R.id.b12);
        button10 = (Button) findViewById(R.id.b13);
        button11 = (Button) findViewById(R.id.b14);
        buttonC = (Button) findViewById(R.id.b15);
        buttonDivision = (Button) findViewById(R.id.b16);
        buttonMadd = (Button) findViewById(R.id.b17);
        buttonMsub = (Button) findViewById(R.id.b18);
        buttonMcan = (Button) findViewById(R.id.b19);
        buttonEqual = (Button) findViewById(R.id.b20);
        buttonPer = (Button) findViewById(R.id.b21);
   button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt1.setText(edt1.getText()+"1");
 }
        });
 button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt1.setText(edt1.getText()+"2")
 }
        });
   button3.setOnClickListener(new View.OnClickListener() {
            @Override
 public void onClick(View v) {
                edt1.setText(edt1.getText()+"3");
            }
        });
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
          edt1.setText(edt1.getText()+"4");
            }
        });
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt1.setText(edt1.getText()+"5");
            }
        });
        button6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt1.setText(edt1.getText()+"6");
            }
        });
        button7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt1.setText(edt1.getText()+"7");
            }
        });
        button8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt1.setText(edt1.getText()+"8");
            }
        });
        button9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt1.setText(edt1.getText()+"9");
            }
        });
        button11.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt1.setText(edt1.getText()+"0");
            }
        });
        buttonPer.setOnClickListener(new View.OnClickListener() {
      @Override
            public void onClick(View v) {
                if (edt1 == null) {
                    edt1.setText("");
                } else {
                    ValueOne = Float.parseFloat(edt1.getText() + "");
                    percentage = (true);
                    edt1.setText(null);
                }
            }
        });
        buttonAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (edt1 == null) {
                    edt1.setText("");
                } else {
                    ValueOne = Float.parseFloat(edt1.getText() + "");
                    Addition = (true);
                    edt1.setText(null);
              }
            }
        });
        buttonSub.setOnClickListener(new View.OnClickListener() {
            @Override
public void onClick(View v) {
                ValueOne = Float.parseFloat(edt1.getText()+"");
                Subtract = (true);
                edt1.setText(null);
            }
        });
        buttonMul.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ValueOne = Float.parseFloat(edt1.getText()+"");
                Multiplication = (true);
                edt1.setText(null);
            }
        });
        buttonDivision.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ValueOne = Float.parseFloat(edt1.getText()+"");
                Division = (true);
                edt1.setText(null);
            }
        });
        buttonC.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt1.setText("");
            }
        });
        button10.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt1.setText(edt1.getText()+".");
            }
        });
        buttonEqual.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ValueTwo = Float.parseFloat(edt1.getText()+"");
                if(Addition==true)
                {
                    edt1.setText(ValueOne+ValueTwo+"");
                    Addition = false;
                }
                if(Subtract==true)
                {
                    edt1.setText(ValueOne-ValueTwo+"");
                    Subtract = false;
                }
                if(Multiplication==true)
                {
                    edt1.setText(ValueOne*ValueTwo+"");
                    Multiplication = false;
                }
                if(Division==true)
                {
                    edt1.setText(ValueOne/ValueTwo+"");
                    Division = false;
                }
                if(percentage==true)
                {

                    edt1.setText(ValueOne*ValueTwo/100+"%");
                    percentage=false;
                }
            }
        });
        buttonMadd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tempfigure.add(Double.parseDouble(edt1.getText().toString()));
                memory1=memory1+tempfigure.get(0);
                tempfigure1=0;
                tempfigure.removeAll(tempfigure);
                edt1.setText("0");
                if (memory1 >0) { edt1.setText(""+memory1);}
}
        });
        buttonMsub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tempfigure.add(Double.parseDouble(edt1.getText().toString()));
                memory1=memory1-tempfigure.get(0);
                tempfigure1=0;
                tempfigure.removeAll(tempfigure);
                edt1.setText("0");
                if (memory1 >0) { edt1.setText(""+memory1);}
            }
        });
        buttonMcan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                memory1=0;
                tempfigure1=0;
                tempfigure.removeAll(tempfigure);
                edt1.setText("0");
                if (memory1 >0) { edt1.setText("Memory cleared");}
            }
        });



Result:  








CONCLUSION:
Android is now stepping up in the next level of mobile internet. There are chances of Android Mobile sales in the whole becomes more than the iPhone. There are chances of android may become the widely used operating system in the world as it has found its application in many applications such as washing machines, microwave ovens, cameras, TVs etc. Google may launch another version of android that starts because Google is launching all the android versions in the alphabetical order. Android is truly open, a free development platform based on Linux and open source. Android is open to all: industry, developer and user. Participating in many of the successful open source projects. Aims to be as easy to build for as the web. Google Android is stepping into the next level of Mobile Internet.



Post a Comment

0 Comments