0% found this document useful (0 votes)
52 views8 pages

Experiment No. 4 Aim: Theory:: Write A Mobile Application To Calculator

The document describes the steps to create a simple calculator mobile application in Android studio. It discusses the required elements like EditText, number buttons 0-9, operational buttons, decimal and clear buttons. It provides the code to fetch button values, set onclick listeners, change EditText value on button click and calculate the result on equal button click by specifying the arithmetic operation. The key steps involve creating objects for elements, fetching their values, setting onclicklisteners to change EditText value and calculate the result.

Uploaded by

yo fire
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views8 pages

Experiment No. 4 Aim: Theory:: Write A Mobile Application To Calculator

The document describes the steps to create a simple calculator mobile application in Android studio. It discusses the required elements like EditText, number buttons 0-9, operational buttons, decimal and clear buttons. It provides the code to fetch button values, set onclick listeners, change EditText value on button click and calculate the result on equal button click by specifying the arithmetic operation. The key steps involve creating objects for elements, fetching their values, setting onclicklisteners to change EditText value and calculate the result.

Uploaded by

yo fire
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

ANUBHA GAJARGAONKAR BE-A

118CP1334B ROLL NO-11

Experiment No. 4

Aim: Write a mobile application to calculator.

Theory: As with all applications, we should always start with building the interface. For the
calculator app, we require the following elements.
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.
Here’s a screenshot of what the code at the bottom would look like, if you were to paste it in your
activity_main.xml file.

Coding the Functionality of our simple calculator

Step 1: Creating objects for our elements

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;
Step 2: Fetching values from the elements into the working of our app
Inside the onCreate function, we will fetch all values and assign it to our objects. We do this by
the following code
btn_0 = (Button) findViewById(R.id.btn_0);
Here, we are fetching the value of ‘btn_0’, which is the ID of our button zero, and storing it in the
object ‘btn_0’ that we created earlier. Though, they are named the same, Android Studio has the
ability to identify the correct one. But if it gets confusing for you, feel free to give either one of
them any other names.
27 | P a g e
Step 3: Changing the EditText value by pressing the buttons.
Here’s the code
btn_1.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v)
{ ed1.setText(ed1.getText()
+"1");
}
});
This is what happens when you push one. The editText, first already fetches the value that it is
already displaying, null in this case, and then adds 1 to it. If we don’t fetch the existing value
then it will become impossible to enter multiple digit numbers.

Step 4: The functional buttons.


Here’s the code we are using for multiplication.
btn_Mul.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v) {
Value1 = Float.parseFloat(ed1.getText() + "");
mMultiplication = true ;
ed1.setText(null);
}
});
We are using our first float value ‘Value1’ here. We use the simple function getText() to do that.
Here since we pressed the multiplication button, we set it’s boolean equivalent variable to true.
You will see how this is helpful in the ‘calculate’ button coding.

Step 5: Calculating, the actual functionality


Here’s the code of what happens when you or someone else presses the ‘=’ button after inputting
another integer.
btn_calc.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v) {
Value2 = Float.parseFloat(ed1.getText() + "");
if (mSubtract == true){
ed1.setText(Value1 - Value2 +"");
28 | P a g e
mSubtract=false;
}
}
});
A couple of things are happening here, firstly, we are fetching the value that you entered
after pressing one of the 4 functional buttons (+, -, *, /) and storing it in float variable
‘Value2’.
Next we execute an if-condition to check whether our four boolean values are true and then run
it’s subsequent code. In the above example, we are executing only the if condition for subtraction.
For the full code, refer to the code below.

Result:
Conclusion: In this experiment we learn make mobile application to calculator

29 | P a g e
Questionnaire

1) Which arithmetic operation use?

An arithmetic operation is specified by combining operands with one arithmetic operator. Arithmetic
operations can also be specified by the ADD, SUBTRACT, DIVIDE, and MULTIPLY built-in
functions.

2) Give the steps to create calculator.


Calculator Layout – Activity_main. xml. This file contains the main layout of the calculator APP. ..
Calculator Buttons. button.xml. This is the button shape used for the calculator buttons. ...
MainActivity. java. Now let's see about the calculator's working logic.

3) What are the requirements of creating calculator?


 Java. Java is the programming language that underpins all Android development. ...
 Understanding of XML. XML was created as a standard way to encode data for internet-based
mobile applications. ...
 Android SDK. ...
 Android Studio. ...
 APIs. ...
 Databases. ...
 Material Design.
4) What are the different buttons use in calculator?
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.

5) Write code for Changing the EditText value by pressing the buttons.
1. Start your EditText, the ID is come to your xml id. EditText myText = (EditText)findViewById(R.
id. my_text_id);
2. String text = "here put the text that you want"
3. myText.setText(text); //variable from point 2.

6) What is the use of setOnClickListener ?


setOnClickListener(this); means that you want to assign listener for your Button “on this instance” this
instance represents OnClickListener and for this reason your class have to implement that interface. If
you have more than one button click event, you can use switch case to identify which button is clicked.

7) What are the different elements use for creating calculator?

30 | P a g e
8) Which function is use to fetch all the values?

The fetch_all() / mysqli_fetch_all() function fetches all result rows and


returns the result-set as an associative array, a numeric array, or both.

9) Why use EditText?


Edittext is one of many such widgets which can be used to retrieve text
data from user. Edittext refers to the widget that displays an empty
textfield in which a user can enter the required text and this text is further
used inside our application.

10) Requirements for creating calculator in android studio.

1. You'll bring the data, we'll bring the app. Link your Google Sheets or
an Excel file via OneDrive, Dropbox, etc. ...
2. Now, you are behind the wheel. ...
3. Your prototype is ready… ...
4. Publish your app. ...
5. Share and manage your app. ...
6. Make your app even better.

You might also like