Question

In: Computer Science

I need the java code for a 4-function calculator app on android studio (do this on...

I need the java code for a 4-function calculator app on android studio (do this on android studio). Please make sure all the requirements shown below are followed (such as the error portion and etc). The topic of this app is to simply create a 4 function calculator which calculates math expressions (add, subtract, multiply, and divide numbers).

The requirements are the following :

- The only buttons needed are 0-9, *, /, +, -, a clear, and enter button

- Implement the onclicklistener on the main activity

- The calcuator should use order of operations (PEMDAS)

- It should be able to continue from a previous answer (Ex: If you type 2+6 the calculator will display 8. If you then multiple by 2 the current result will simply be multiplied by 2)

- It should read the entire sequence of numbers and operators and save them into a String. The information can be read out of the String using a StringTokenizer. The delimiters will be the operators on the calculator.

- The entire mathematical expression will be displayed on the screen before the equal button is clicked.

- The equation will simply be evaluated in the order that the user types in the numbers rather than the precedence

- If the user enters an equation with invalid syntax, the output should display “error”. The calculator should also display “error” for any mathematical calculations that are impossible.

Solutions

Expert Solution

  1. TextView
  2. Buttons

That’s it. We use the textView to show users the numbers they have written and also to display the result of the calculations we are going to do.

Each of the numbers will have a different button, from 0-9 and we will have four additional 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 to clear the display.

Requirements :

1.EditText: To display numbers and the result.
2. Button: Numbers 0-9
3. Button: Operational buttons (+, -, *, /, =)
4. Button: Decimal button
5. Button: Clear display button.

Algorithm :

Step 1: Creating objects for our elements.

Step 2: Fetching values from the elements into the working of our app.

Step 3: Changing the EditText value by pressing the buttons.

Step 4: The functional buttons.

Step 5: Calculating, the actual functionality.

Full Code for the User Interface for Android 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.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 Tutorial for the Functionality of Android Calculator App

Remember to change your package name to what you are using!

package com.example.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("");
            }
        });
    }

}

OUTPUT :

I have added all the requirements you want.If anything needed you can ask.I have wriitened in java only and i have checked the code.I have also included the algorthim to understand it clearly.


Related Solutions

I need the java code for a 4 function calculator app on android studio (do this...
I need the java code for a 4 function calculator app on android studio (do this on android studio) - The requirements are the following : - The only buttons needed are 0-9, *, /, +, -, a clear, and enter button - Implement the onclicklistener on the main activity - The calcuator should use order of operations (PEMDAS) - It should be able to continue from a previous answer (Ex: If you type 2+6 the calculator will display 8....
I need the JAVA code for a 4 function calculator app on andriod studio - The...
I need the JAVA code for a 4 function calculator app on andriod studio - The requirements are the following : - The only buttons needed are 0-9, *, /, +, -, a clear, and enter button - Implement the onclicklistener on the main activity - The calcuator should use order of operations (PEMDAS) - It should be able to continue from a previous answer (Ex: If you type 2+6 the calculator will display 8. If you then multiple by...
I am doing a 4 function calculator app in java on android studio which calculates math...
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,...
Hi! I need it in android studio and in java Design a game app “BouncingBall ”...
Hi! I need it in android studio and in java Design a game app “BouncingBall ” in which the user’s goal is to prevent a bouncing ball from falling off the bottom of the screen. When the user presses the start button, a ball bounces off the top, left and right sides (the “walls”) of the screen. A horizontal bar on the bottom of the screen serves as a paddle to prevent the ball from hitting the bottom of the...
Hi! I need it in android studio and in java Design a game app “BouncingBall ”...
Hi! I need it in android studio and in java Design a game app “BouncingBall ” in which the user’s goal is to prevent a bouncing ball from falling off the bottom of the screen. When the user presses the start button, a ball bounces off the top, left and right sides (the “walls”) of the screen. A horizontal bar on the bottom of the screen serves as a paddle to prevent the ball from hitting the bottom of the...
On Android Studio, create a 4 function calculator. The only buttons you need are 0-9, *,...
On Android Studio, create a 4 function calculator. The only buttons you need are 0-9, *, /, +, -, a clear, and enter button. Use StringTokenizer or String.split for the calculator code.
On Android Studio, create a 4 function calculator. The only buttons you need are 0-9, *,...
On Android Studio, create a 4 function calculator. The only buttons you need are 0-9, *, /, +, -, a clear, and enter button. Having this java code below, I need the XML code that organizes the layout of the calculator (using either linearLayout or relativeLayout). As shown in the JAVA Code - Button button0, button1, button2, button3, button4, button5, button6, button7, button8, button9, buttonadd, buttonsubtract, buttonmultiply, buttondivide, buttonequals, buttonclear and TextView display - is what is needed in the...
On Android Studio, create a 4 function calculator. The only buttons you need are 0-9, *,...
On Android Studio, create a 4 function calculator. The only buttons you need are 0-9, *, /, +, -, a clear, and enter button. Having this java code, I need the XML code that organizes the layout of the calculator. JAVA CODE - package com.example.10012698.calculatorproject; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.util.ArrayList; import java.util.StringTokenizer; public class MainActivity extends AppCompatActivity implements View.OnClickListener { Button button0, button1, button2, button3, button4, button5, button6, button7, button8, button9, buttonadd, buttonsubtract,...
Android Studio Code: Provide a working android studio code i.e java and xml code for the...
Android Studio Code: Provide a working android studio code i.e java and xml code for the activity below: Develop an application that is capable to turn pages back and forth. Detailed Instructions: For the main activity, create a layout that represents a fictitious title and author. The activity should include a clickable button on the bottom right that allows you to go forward to the next activity. The next activity will simply be an image above simple text that can...
Using Android Studio, create a one java android app that has 4 buttons:- Change Color Button...
Using Android Studio, create a one java android app that has 4 buttons:- Change Color Button - When the Change Color button is clicked, it changes the current activity background to a randomly selected color Speak Button - When the Speak button is clicked, it opens a new activity named SpeakActivity. On the SpeakActivity there are three controls: EditText, Button (Speak) and Button (Back). The Speak button uses the Text to Speech service to say the text entered in EditText....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT