Question

In: Computer Science

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

import java.util.Scanner;

public class Calculator {

    public static void main(String[] args) {

        Scanner reader = new Scanner(System.in);
        System.out.print("Enter two numbers: ");

        // nextDouble() reads the next double from the keyboard
        double first = reader.nextDouble();
        double second = reader.nextDouble();

        System.out.print("Enter an operator (+, -, *, /): ");
        char operator = reader.next().charAt(0);

        double result;

        switch(operator)
        {
            case '+':
                result = first + second;
                break;

            case '-':
                result = first - second;
                break;

            case '*':
                result = first * second;
                break;

            case '/':
                result = first / second;
                break;

            // operator doesn't match any case constant (+, -, *, /)
            default:
                System.out.printf("Error! operator is not correct");
                return;
        }

        System.out.printf("%.1f %c %.1f = %.1f", first, operator, second, result);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:weightSum="100">

    <TextView
        android:id="@+id/display"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="15"
        android:text=""
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="20"
        android:orientation="horizontal"
        android:weightSum="100">

        <Button
            android:id="@+id/buttonadd"
            android:layout_width="0px"
            android:layout_height="match_parent"
            android:layout_weight="25"
            android:text="+"/>

        <Button
            android:id="@+id/button7"
            android:layout_width="0px"
            android:layout_height="match_parent"
            android:layout_weight="25"
            android:text="7"/>
        <Button
            android:id="@+id/button8"
            android:layout_width="0px"
            android:layout_height="match_parent"
            android:layout_weight="25"
            android:text="8"/>
        <Button
            android:id="@+id/button9"
            android:layout_width="0px"
            android:layout_height="match_parent"
            android:layout_weight="25"
            android:text="9"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="20"
        android:orientation="horizontal"
        android:weightSum="100">

        <Button
            android:id="@+id/buttonsubtract"
            android:layout_width="0px"
            android:layout_height="match_parent"
            android:layout_weight="25"
            android:text="-"/>

        <Button
            android:id="@+id/button4"
            android:layout_width="0px"
            android:layout_height="match_parent"
            android:layout_weight="25"
            android:text="4"/>
        <Button
            android:id="@+id/button5"
            android:layout_width="0px"
            android:layout_height="match_parent"
            android:layout_weight="25"
            android:text="5"/>
        <Button
            android:id="@+id/button6"
            android:layout_width="0px"
            android:layout_height="match_parent"
            android:layout_weight="25"
            android:text="6"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="20"
        android:orientation="horizontal"
        android:weightSum="100">

        <Button
            android:id="@+id/buttonmultiply"
            android:layout_width="0px"
            android:layout_height="match_parent"
            android:layout_weight="25"
            android:text="*"/>

        <Button
            android:id="@+id/button1"
            android:layout_width="0px"
            android:layout_height="match_parent"
            android:layout_weight="25"
            android:text="1"/>
        <Button
            android:id="@+id/button2"
            android:layout_width="0px"
            android:layout_height="match_parent"
            android:layout_weight="25"
            android:text="2"/>
        <Button
            android:id="@+id/button3"
            android:layout_width="0px"
            android:layout_height="match_parent"
            android:layout_weight="25"
            android:text="3"/>
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="20"
        android:orientation="horizontal"
        android:weightSum="100">

        <Button
            android:id="@+id/buttondivide"
            android:layout_width="0px"
            android:layout_height="match_parent"
            android:layout_weight="25"
            android:text="/"/>

        <Button
            android:id="@+id/buttonclear"
            android:layout_width="0px"
            android:layout_height="match_parent"
            android:layout_weight="25"
            android:text="Clear"/>
        <Button
            android:id="@+id/button0"
            android:layout_width="0px"
            android:layout_height="match_parent"
            android:layout_weight="25"
            android:text="0"/>
        <Button
            android:id="@+id/buttonequals"
            android:layout_width="0px"
            android:layout_height="match_parent"
            android:layout_weight="25"
            android:text="="/>
    </LinearLayout>
</LinearLayout>

for java

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, buttonmultiply,
buttondivide, buttonequals, buttonclear;
TextView display;
String displaytext="";
double result;
double x, y;
ArrayList<String> list;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

button0 = (Button) findViewById(R.id.button0);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button6 = (Button) findViewById(R.id.button6);
button7 = (Button) findViewById(R.id.button7);
button8 = (Button) findViewById(R.id.button8);
button9 = (Button) findViewById(R.id.button9);
buttonadd = (Button) findViewById(R.id.buttonadd);
buttonsubtract = (Button) findViewById(R.id.buttonsubtract);
buttonmultiply = (Button) findViewById(R.id.buttonmultiply);
buttondivide = (Button) findViewById(R.id.buttondivide);
buttonclear = (Button) findViewById(R.id.buttonclear);
buttonequals = (Button) findViewById(R.id.buttonequals);
display = (TextView) findViewById(R.id.display);
display.setOnClickListener(this);
list = new ArrayList<>();
}

public void onClick(View view)
{
if(!(view.equals(buttonclear)&&!(view.equals(buttonequals))))
{
display.setText(display.getText()+""+((Button)view).getText());

if(displaytext.equals("0"))
{
display.setText(" ");
}

if(view.equals(buttonclear))
{
display.setText(" ");
}

if(view.equals(buttonequals))
{
displaytext= displaytext.substring(0,displaytext.length()-1);
StringTokenizer operators= new StringTokenizer(displaytext, "+-*/",true);

while(operators.hasMoreTokens())
{
list.add(operators.nextToken());
}

for(int j=0; j<list.size()-1; j++)
{
if (list.get(j).equals("*") || list.get(j).equals("/"))
{
x = Double.parseDouble(list.get(j - 1));
y = Double.parseDouble(list.get(j + 1));

if (list.get(j).equals("*"))
{
result+=(x*y);
}

if (list.get(j).equals("/"))
{
result+=(x/y);
}
}
}
for(int k=0;k<list.size()-1;k++)
{
if (list.get(k).equals("+") || list.get(k).equals("-"))
{
x = Double.parseDouble(list.get(k - 1));
y = Double.parseDouble(list.get(k + 1));

if (list.get(k).equals("+"))
{
result+=(x+y);
}

if (list.get(k).equals("-"))
{
result+=(x-y);
}
}
}
}
display.setText(""+result);


}
}

this is the program for java code for calculator


Related Solutions

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.
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...
I need a full java code. And I need it in GUI With the mathematics you...
I need a full java code. And I need it in GUI With the mathematics you have studied so far in your education you have worked with polynomials. Polynomials are used to describe curves of various types; people use them in the real world to graph curves. For example, roller coaster designers may use polynomials to describe the curves in their rides. Polynomials appear in many areas of mathematics and science. Write a program which finds an approximate solution to...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is my first java homework so for you i don't think it will be hard for you. (basic stuff) the problem: Write a complete Java program The transport Company in which you are the engineer responsible of operations for the optimization of the autonomous transport of liquid bulk goods, got a design contract for an automated intelligent transport management system that are autonomous trucks which...
Lab 02 Final Grade Calculator **** I need it in JAVA **** **** write comments with...
Lab 02 Final Grade Calculator **** I need it in JAVA **** **** write comments with the code **** Objective: Write a program that calculates a final grade! The program should read in a file and then calculates the final grade based on those scores. Files read into this system are expected to follow this format: <Section0>\n <Grade0 for Section0>\n <Grade1 for Section0>\n … <GradeX for Section0>\n <Section1>\n <Grade0 for Section1>\n … <GradeY for Section1> <Section2> … <SectionZ> Sections denote...
I need this in java A6 – Shipping Calculator Assignment Introduction In this part, you will...
I need this in java A6 – Shipping Calculator Assignment Introduction In this part, you will solve a problem described in English. Although you may discuss ideas with your classmates, etc., everyone must write and submit their own version of the program. Do NOT use anyone else’s code, as this will result in a zero for you and the other person! Shipping Calculator Speedy Shipping Company will ship your package based on the weight and how far you are sending...
I need the code for following in C++ working for Visual studio please. Thanks Use a...
I need the code for following in C++ working for Visual studio please. Thanks Use a Struct to create a structure for a Player. The Player will have the following data that it needs maintain: Struct Player int health int level string playerName double gameComplete bool isGodMode Create the 2 functions that will do the following: 1) initialize(string aPlayerName) which takes in a playername string and creates a Player struct health= 100 level= 1 playerName = aPlayerName gameComplete = 0...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT