Question

I need the java code for a 4-function calculator app on android studio. Please make sure...

I need the java code for a 4-function calculator app 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 [public class MainActivity extends AppCompatActivity implements View.OnClickListener]

- The calcuator should use order of operations (PEMDAS)

- It should be able to continue from a previous answer (Ex: If you type 1+3 the calculator will display 4. 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 is to 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.

- 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. [ THIS IS TO BE DONE USING JAVA TRY AND CATCH STATEMENTS ]

Homework Answers

Answer #1
  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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
You recently started working in at a University in the Math department as a software programmer....
You recently started working in at a University in the Math department as a software programmer. You need to build an app that can calculate the mod of two numbers. Users need to enter num1 and num2. The app should perform the calculation and display the output as num1 mod nub2 operation. For example, 25 mod 5=0 24 mod 5 = 4 Once you implement the mod operations, then you need to add following buttons. Addition Subtraction Division Power Follow...
Implement a scientific calculator using MVC constructs in Swift 3 Programming language(basic calculator IOS app) Requirements:...
Implement a scientific calculator using MVC constructs in Swift 3 Programming language(basic calculator IOS app) Requirements: It needs to be able to handle multiple operations in a sequence. It should ONLY use the last operand if multiple operands are entered consecutively Example: “5++-1=”is equivalent to “5-1=” Proper error handling and self-correction. “+-5” is equivalent to “negative 5” “0004+32 ++ 0101 -- 04=” is equivalent to “4+32+101-4=” Implement 3 more buttons: ¼ ½ and ¾ at the bottom of the keyboard....
Implement a scientific calculator using MVC constructs in Swift 3 Programming language(basic calculator IOS app). It...
Implement a scientific calculator using MVC constructs in Swift 3 Programming language(basic calculator IOS app). It needs to have two classes. One class name is CalculatorBrain.swift for model and another is ViewController.swift for view. Also please show the screenshot of storyboard. Requirements: It needs to be able to handle multiple operations in a sequence. It should ONLY use the last operand if multiple operands are entered consecutively Example: “5++-1=”is equivalent to “5-1=” Proper error handling and self-correction. “+-5” is equivalent...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the codes below. Requirement: Goals for This Project:  Using class to model Abstract Data Type  OOP-Data Encapsulation You are asked to write an app to keep track of a relatively small music library. The app should load song information from a data file once the app is started. It should allow user to view, add, remove, and search for songs. The app should...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • A sample is obtained from a population with u=40 and o=6. Which of the following sample...
    asked 4 minutes ago
  • In a survey, 11 people were asked how much they spent on their child's last birthday...
    asked 22 minutes ago
  • Discussion 7.1: Practice Critical Thinking How well do you understand this material? Reading the material is...
    asked 23 minutes ago
  • A credit score measures a​ person's creditworthiness. Assume the average credit score for Americans is 709....
    asked 26 minutes ago
  • A Database Administrator (DBA) is play both managerial role and technical role in a modern organization;...
    asked 41 minutes ago
  • The Federal Election Commission collects information about campaign contributions and disbursements for candidates and political committees...
    asked 41 minutes ago
  • In the week before and the week after a​ holiday, there were 10,000 total​ deaths, and...
    asked 44 minutes ago
  • 1. Identify the vertex of the quadratic equation y=2x2+8x-10. using the formula 2. Write the equation...
    asked 59 minutes ago
  • How well do you understand this material? Reading the material is separate than understanding it; further,...
    asked 1 hour ago
  • Analyze the behavior of capacitors under steady state direct current and high frequency AC current (500...
    asked 1 hour ago
  • Traffic on the eastbound approach of a signalized intersection is traveling at 48 km/h, with a...
    asked 1 hour ago
  • Students in a chemistry class convince their teacher to use the following "group grading" scenario. Students...
    asked 1 hour ago