In: Computer Science
I am doing a 4 function calculator app in java on android studio which calculates math expressions (add, subtract, multiply, and divide).
I ONLY NEED THE CODE THE EQUAL BUTTON.
When the user clicks the equal button, it should take the entire sequence of numbers and operators on the screen, such as 1+2*5 , and save them into a String [I am pretty sure this can be done using the toString() call]. Then, the String should be split using StringTokenizer, with the delimiters being "+-/*" . After that to display the correct answer, I am pretty sure Double.parseDouble needs to be called to covert the String tokens to double values. After that, the correct operation should be done to get the correct answer.
Note -
- The calcuator should use order of operations (PEMDAS)
- It should be able to continue from a previous answer (Ex: If you type 1+5 and press equals, the calculator will display 4. If you then multiply by 2, the current result will simply be multiplied by 2)
Solution:
We are using
text view - To show users the numbers which they have been written and also used to display their calculation result which we are going to do.
buttons - 0-9 number have a different buttons, in addition we are adding 4 buttons for addition, subtraction, multiplication and division. other than this we require a button to calculate the operation, a button for decimals and a button for clearing the display.
Requirements needed:
1. Edit text : To display the numbers and the result.
2. Button : number (0-9) , operational button (+,-,*,/,=) , decimal button (.) , clear display button
User Interface for Android Simple Calculator App
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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="com.example.a84.calculator.MainActivity"> <RelativeLayout android:layout_width="368dp" android:layout_height="495dp" android:layout_marginBottom="8dp" android:layout_marginEnd="8dp" android:layout_marginTop="8dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent"> <Button android:id="@+id/btn_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_below="@+id/edText1" android:layout_marginTop="60dp" android:onClick="PressOne" android:text="1" android:textSize="18sp" /> <Button android:id="@+id/btn_0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/btn_8" android:layout_toEndOf="@+id/btn_7" android:layout_toRightOf="@+id/btn_7" android:text="0" android:textSize="18sp" /> <Button android:id="@+id/btn_9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/btn_6" android:layout_toEndOf="@+id/btn_5" android:layout_toRightOf="@+id/btn_5" android:text="9" android:textSize="18sp" /> <Button android:id="@+id/btn_8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/btn_5" android:layout_toEndOf="@+id/btn_7" android:layout_toRightOf="@+id/btn_7" android:text="8" android:textSize="18sp" /> <Button android:id="@+id/btn_7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/btn_4" android:layout_alignStart="@+id/btn_4" android:layout_below="@+id/btn_4" android:text="7" android:textSize="18sp" /> <Button android:id="@+id/btn_6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/btn_5" android:layout_alignBottom="@+id/btn_5" android:layout_toEndOf="@+id/btn_5" android:layout_toRightOf="@+id/btn_5" android:text="6" android:textSize="18sp" /> <Button android:id="@+id/btn_5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/btn_2" android:layout_toEndOf="@+id/btn_4" android:layout_toRightOf="@+id/btn_4" android:text="5" android:textSize="18sp" /> <Button android:id="@+id/btn_4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/btn_1" android:layout_alignStart="@+id/btn_1" android:layout_below="@+id/btn_1" android:text="4" android:textSize="18sp" /> <Button android:id="@+id/btn_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/btn_2" android:layout_alignBottom="@+id/btn_2" android:layout_toEndOf="@+id/btn_2" android:layout_toRightOf="@+id/btn_2" android:text="3" android:textSize="18sp" /> <Button android:id="@+id/btn_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/btn_1" android:layout_alignBottom="@+id/btn_1" android:layout_toEndOf="@+id/btn_1" android:layout_toRightOf="@+id/btn_1" android:text="2" android:textSize="18sp" /> <Button android:id="@+id/btn_Add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/btn_6" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:backgroundTint="@android:color/darker_gray" android:text="+" android:textColor="@android:color/background_light" android:textSize="18sp" /> <Button android:id="@+id/btn_Sub" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/btn_Add" android:layout_alignStart="@+id/btn_Add" android:layout_below="@+id/btn_Add" android:backgroundTint="@android:color/darker_gray" android:text="-" android:textColor="@android:color/background_light" android:textSize="18sp" /> <Button android:id="@+id/btn_Mul" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/btn_Sub" android:layout_alignStart="@+id/btn_Sub" android:layout_below="@+id/btn_6" android:backgroundTint="@android:color/darker_gray" android:text="*" android:textColor="@android:color/background_light" android:textSize="18sp" /> <Button android:id="@+id/btn_Div" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/btn_Mul" android:layout_alignStart="@+id/btn_Mul" android:layout_below="@+id/btn_9" android:backgroundTint="@android:color/darker_gray" android:text="/" android:textColor="@android:color/background_light" android:textSize="18sp" /> <EditText android:id="@+id/edText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_marginTop="22dp" android:ems="10" android:inputType="textPersonName" android:textAlignment="textEnd" android:textSize="24sp" /> <Button android:id="@+id/btn_calc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/btn_0" android:layout_toEndOf="@+id/btn_0" android:layout_toRightOf="@+id/btn_0" android:backgroundTint="@android:color/holo_green_light" android:text="=" android:textColor="@android:color/background_light" android:textSize="18sp" /> <Button android:id="@+id/btn_dec" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/btn_7" android:layout_toLeftOf="@+id/btn_8" android:layout_toStartOf="@+id/btn_8" android:text="." android:textSize="18sp" /> <Button android:id="@+id/btn_clear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_below="@+id/btn_Div" android:backgroundTint="@android:color/holo_blue_dark" android:text="clear" android:textColor="@android:color/background_light" android:textSize="18sp" /> </RelativeLayout> </android.support.constraint.ConstraintLayout>
Full Code for the Functionality of Android Calculator App
(Note: Remember to change your package name to what you are using)
package com.example.a84.calculator; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends AppCompatActivity { Button btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9,btn_0,btn_Add,btn_Sub,btn_Mul,btn_Div,btn_calc,btn_dec,btn_clear; EditText ed1; float Value1, Value2; boolean mAddition, mSubtract, mMultiplication, mDivision ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_0 = (Button) findViewById(R.id.btn_0); btn_1 = (Button) findViewById(R.id.btn_1); btn_2 = (Button) findViewById(R.id.btn_2); btn_3 = (Button) findViewById(R.id.btn_3); btn_4 = (Button) findViewById(R.id.btn_4); btn_5 = (Button) findViewById(R.id.btn_5); btn_6 = (Button) findViewById(R.id.btn_6); btn_7 = (Button) findViewById(R.id.btn_7); btn_8 = (Button) findViewById(R.id.btn_8); btn_9 = (Button) findViewById(R.id.btn_9); btn_Add = (Button) findViewById(R.id.btn_Add); btn_Div = (Button) findViewById(R.id.btn_Div); btn_Sub = (Button) findViewById(R.id.btn_Sub); btn_Mul = (Button) findViewById(R.id.btn_Mul); btn_calc = (Button) findViewById(R.id.btn_calc); btn_dec = (Button) findViewById(R.id.btn_dec); btn_clear = (Button) findViewById(R.id.btn_clear); ed1 = (EditText) findViewById(R.id.edText1); btn_0.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ed1.setText(ed1.getText()+"0"); } }); btn_1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ed1.setText(ed1.getText()+"1"); } }); btn_2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ed1.setText(ed1.getText()+"2"); } }); btn_3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ed1.setText(ed1.getText()+"3"); } }); btn_4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ed1.setText(ed1.getText()+"4"); } }); btn_5.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ed1.setText(ed1.getText()+"5"); } }); btn_6.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ed1.setText(ed1.getText()+"6"); } }); btn_7.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ed1.setText(ed1.getText()+"7"); } }); btn_8.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ed1.setText(ed1.getText()+"8"); } }); btn_9.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ed1.setText(ed1.getText()+"9"); } }); btn_dec.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ed1.setText(ed1.getText()+"."); } }); btn_Add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (ed1 == null){ ed1.setText(""); }else { Value1 = Float.parseFloat(ed1.getText() + ""); mAddition = true; ed1.setText(null); } } }); btn_Sub.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Value1 = Float.parseFloat(ed1.getText() + ""); mSubtract = true ; ed1.setText(null); } }); btn_Mul.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Value1 = Float.parseFloat(ed1.getText() + ""); mMultiplication = true ; ed1.setText(null); } }); btn_Div.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Value1 = Float.parseFloat(ed1.getText()+""); mDivision = true ; ed1.setText(null); } }); btn_calc.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Value2 = Float.parseFloat(ed1.getText() + ""); if (mAddition == true){ ed1.setText(Value1 + Value2 +""); mAddition=false; } if (mSubtract == true){ ed1.setText(Value1 - Value2 +""); mSubtract=false; } if (mMultiplication == true){ ed1.setText(Value1 * Value2 + ""); mMultiplication=false; } if (mDivision == true){ ed1.setText(Value1 / Value2+""); mDivision=false; } } }); btn_clear.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ed1.setText(""); } }); } }