Question

In: Computer Science

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, 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)

Solutions

Expert Solution

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("");
            }
        });
    }

}

Related Solutions

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...
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.
android studio Begin a new app and create a Java class for products with data: productName,...
android studio Begin a new app and create a Java class for products with data: productName, productCode and price and use a file of objects to store product data and read back and display on screen. Do this by creating a simple form using EditTexts, buttons or ActionBar items and display output using an Alert.
Create a Scorekeeper app in android studio by using java language - Layouts | Widgets Create...
Create a Scorekeeper app in android studio by using java language - Layouts | Widgets Create the layout for your score keeping app. The app should have: Two team names (TextViews) Two scores (TextViews) Buttons to increase/ decrease the scores An amount to change the score by (RadioButtons) You must have at least two score options The scores can be changed by anything you want American football: 1, 2, 3, 6 Basketball: 1, 2, 3 Freestyle wrestling: 1, 2, 3,...
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...
IN ANDROID STUDIO, you will create a mortgage calculator appthat allows the user to enter...
IN ANDROID STUDIO, you will create a mortgage calculator app that allows the user to enter a purchase price, down-payment amount, and an interest rate.Based on these values, the app should calculate the loan amount (purchase price minus down payment) and display the monthly payment for 10, 20, and 30-year loans.Allow the user to select a custom loan duration (in years) by using a SeekBar and display the monthly payment for that custom loan duration.Assignment deliverables (all in a ZIP...
use android studio or MIT inventor Create a Presidents Quiz App for your project with the...
use android studio or MIT inventor Create a Presidents Quiz App for your project with the following requirements: You must have images Buttons Label Conditional Statement Homepage with instructions Sound (music, buzzer, etc.) - Ensure you are using the correct layout: Gravity(Android Studio), Horizontal or Vertical Arrangement (MIT). - 40POINTS Points System (indicating how many answers they got wrong/correct). 20POINT 1-2 questions per President (45+ questions)
For this IP, you will create a very simple drawing app using Android Studio. The purpose...
For this IP, you will create a very simple drawing app using Android Studio. The purpose of this assignment is to give you more building blocks to use when programming apps. For full credit for this assignment, you should complete the following: Create a menu and display menu items on the app bar Detect when the user touches the screen and moves a finger Be able to change the color and width of a line Be able to save an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT