Question

In: Computer Science

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.

Solutions

Expert Solution

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


}
}


Related Solutions

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...
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,...
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...
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...
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,...
the code base you will be working with involves an Android application. (Android Studio) application name...
the code base you will be working with involves an Android application. (Android Studio) application name " SharingApp" In the application’s current state: A user of the app is able to create and edit a profile with a unique username and an email address. A user of the app is able to login and logout. An owner is able to record the items they own and wish to share. A bidder is able to place bids on items they wish...
Android Studio. Java. Please create an application that -> An activity that allows user to enter...
Android Studio. Java. Please create an application that -> An activity that allows user to enter name, gender, date of birth, state of residence (selected from a pre-defined list: CA, AZ, NV, OR), email address and favorite website. This activity has a button "Show Data" that displays detail entered
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...
Create a mobile application using Android studio, for cinema tickets reservation. The application will be used...
Create a mobile application using Android studio, for cinema tickets reservation. The application will be used by the customers to book cinema tickets. Only users of 15 years old and above are allowed to book cinema tickets for a particular film. The user can book more than one ticket where ticket prices vary between 20 and 50 AED. Your app contains tree Activities. The first activity is a launching activity containing a logo and a button start. When the user...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT