0% found this document useful (0 votes)
112 views12 pages

Calculator

This document describes how to create a basic calculator app in Android Studio. It includes instructions on setting up the project, designing the user interface with buttons and text fields, and writing the code to handle number input and arithmetic operations like addition, subtraction, multiplication and division. The app will allow users to enter two numbers, select an operation, and see the result. Creating this simple calculator app requires knowledge of Java, XML and using Android Studio to lay out the UI and develop the code behind it.

Uploaded by

Mathanika. M
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)
112 views12 pages

Calculator

This document describes how to create a basic calculator app in Android Studio. It includes instructions on setting up the project, designing the user interface with buttons and text fields, and writing the code to handle number input and arithmetic operations like addition, subtraction, multiplication and division. The app will allow users to enter two numbers, select an operation, and see the result. Creating this simple calculator app requires knowledge of Java, XML and using Android Studio to lay out the UI and develop the code behind it.

Uploaded by

Mathanika. M
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/ 12

Calculator

Introduction:

Hey there Android enthusiasts, greetings from DataFlair. Today


we are going to see and learn to implement an Android Project that
is a simple calculator app in android studio. We’ll understand the
complete project development

If you don’t know how to built then you are for treat as we are going
to share how the App is created in Android. This is the one of
simplest App you can create to understand Android basics. In this
Calculator App tutorial we are going use of multiple Android UI
components to design and step by step developing a Basic Calculator
application in Android Studio.this article.

Do you know when was the first ever mechanical calculator created?
The Calculators that we use today were never the same earlier. The
first calculator was invented in the year 1642 by Blaise Pascal when
he was 19 years old. Okay, now let us begin with our project without
any further delay!!

1
Algorithm:
Step 1: Firstly get the android studio downloaded in your system,
then open it.

Step 2: Create a new project and name it Calculator.

Step 3: Open res -> layout -> activity_main.xml (or) main.xml. Here
we are going to create the application interface like add layouts,
Button , TextView and EditText.

i of Step 3 – Create a Linearlayout vertical, add a textview followed


by two textfields Number(decimal) for writing numbers in it.
Starting code of activity_main.xml

Functional Requirements:
This is a basic project for beginners, the android
calculator app will help us to do various arithmetic calculations. This
android application will have a user interface with numbers and
arithmetic operations. For the development of this application, we
will make use of Android Studio. Let us go through its description
about what things the user interface will have:

It will have the number keys that will be created using buttons

Another thing would be operators like +, *, /, -, % that are created


using buttons too

Then we’ll have to create two more buttons for the delete and
answer buttons

Then, there will be a screen that will show the number entered by
the user and result when they click on answer
2
This calculator app would be a one time install; once the user installs
it they are always ready to use it

Prerequisites for this android project:


To develop this simple calculator app the requirements
and prerequisites are as follows:

Java: First of all you need to have the knowledge of Java


Programming. Java programming plays a very important role as we
will develop the app code in Java

XML: XML is another important part of our android application.


It will be used for the development of the user interface for the
application.

Android Studio: Android Studio is the backbone of our


application, as we will develop our app using android studio. Android
virtual device is also shipped with android studio that will be helpful
in testing whether the applications are working or not.

3
Develop a Simple Calculator App in
Android Studio:
Now we’ll begin with the development of a simple
calculator app. Throughout this article, we’ll understand the usage
and working of the code before we implement it. So let us see the
required files & steps for the code.

First of all, you would need to download the code. Please


download the source code

Now, we’ll see what’s there in the code to understand it better. So to


create the interface we’ve added the number button and the
operators for the calculator. This will be for the users to interact with
our calculator. We’ve arranged them horizontally in four layers. This
will be present in the main_activity.xml file.

Also, there are the following things that we have added in the
drawable that are as follows.

The description of the buttons that we use. You can find that code
in Res > drawable > Buttons.xml.

We have added the description of the operator buttons. You can


find that code in Res > drawable > Operator.xml

We also described the selector in drawable, it will show the effect


as soon as we select the buttons. You can find that code in Res >
drawable > Selector, xml And then finally we had written the code for
the actual processing of the calculator. For this, the code is written in
the MainActivity.java file.So, once we are ready with code we will
run the calculator.

4
code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="abhiandroid.com.calculater.MainActivity"
android:orientation="vertical"
android:gravity="top"
android:textAlignment="center"
android:background="@android:color/holo_blue_bright"
android:weightSum="1">

<TextView
android:text="@string/enter_two_numbers"
android:layout_width="match_parent"
android:id="@+id/textView"
android:layout_height="30dp"
android:gravity="center_horizontal"
android:textColorLink="?android:attr/editTextColor"
tools:textStyle="bold|italic"
android:textStyle="bold|italic"
android:fontFamily="serif"
android:visibility="visible"
android:textSize="24sp"
android:layout_weight="0.07" />

<EditText
android:layout_width="match_parent"

5
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/editOp1"
android:textSize="18sp"
android:gravity="center_horizontal"
android:layout_marginBottom="5dp"
android:visibility="visible" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/editOp2"
android:textSize="18sp"
android:gravity="center_horizontal"
android:elevation="1dp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<Button
android:text="+"
android:layout_width="78dp"
android:layout_height="wrap_content"
android:id="@+id/btnadd"
android:layout_weight="0.03" />

<Button
android:text="-"
android:layout_width="78dp"
android:layout_height="wrap_content"

6
android:id="@+id/btnsub"
android:layout_weight="0.03" />

<Button
android:text="*"
android:layout_width="78dp"
android:layout_height="wrap_content"
android:id="@+id/btnmul"
android:layout_weight="0.03"/>

<Button
android:text="/"
android:layout_height="wrap_content"
android:id="@+id/btndiv"
android:layout_width="78dp"
android:layout_weight="0.03" />

<Button
android:text="Clear"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:id="@+id/btnclr"
android:layout_weight="0.03" />
</LinearLayout>
// Addition
btnadd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if((opr1.getText().length()>0) && (opr2.getText().length()>0))
{
double oper1 = Double.parseDouble(opr1.getText().toString());
double oper2 = Double.parseDouble(opr2.getText().toString());
double result = oper1 + oper2;
txtresult.setText(Double.toString(result));
}

7
else{
Toast toast= Toast.makeText(MainActivity.this,"Enter The
Required Numbers",Toast.LENGTH_LONG);
toast.show();
}
}
});
//Subtraction
btnsub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if((opr1.getText().length()>0) && (opr2.getText().length()>0))
{
double oper1 = Double.parseDouble(opr1.getText().toString());
double oper2 = Double.parseDouble(opr2.getText().toString());
double result = oper1 - oper2;
txtresult.setText(Double.toString(result));
}
else{
Toast toast= Toast.makeText(MainActivity.this,"Enter The
Required Numbers",Toast.LENGTH_LONG);
toast.show();
}

}
});
// Multiplication
btnmul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if((opr1.getText().length()>0) && (opr2.getText().length()>0))
{
double oper1 = Double.parseDouble(opr1.getText().toString());
double oper2 = Double.parseDouble(opr2.getText().toString());
double result = oper1 * oper2;
txtresult.setText(Double.toString(result));

8
}
else{
Toast toast= Toast.makeText(MainActivity.this,"Enter The
Required Numbers",Toast.LENGTH_LONG);
toast.show();
}
}
});
// Division
btndiv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if((opr1.getText().length()>0) && (opr2.getText().length()>0))
{
double oper1 = Double.parseDouble(opr1.getText().toString());
double oper2 = Double.parseDouble(opr2.getText().toString());
double result = oper1 / oper2;
txtresult.setText(Double.toString(result));
}
else{
Toast toast= Toast.makeText(MainActivity.this,"Enter The
Required Numbers",Toast.LENGTH_LONG);
toast.show();
}
}
})

9
.

Output:

10
Conclusion:
In this work we have developed a calculator for exact real
number computation and performed a theoretical analysis of the
algorithms and experimented on their implementation.

We began by defining two representations of using


streams of digits. These were then extended to represent real
numbers on the whole real line using a (mantissa, exponent) style
representation. We showed how to convert between these
representations, how to convert decimal numbers into a signed
binary representation, and how to convert a finite portion of the
signed binary representation back into decimal.

Algorithms for the basic arithmetic operations were


implemented for these representations. A number of different
techniques are used to obtain these algorithms, including exploiting
the relationship between the list operation `cons' and numerical
average, using certain identities, and analysing of the range of
possible values of a stream starting with a given digit or sequence of
digits.

We developed an algorithm for the direct multiplication of


two streams of signed binary digit. This is a more complex operation
than the multiplication of dyadic digit streams, but avoids the
problem of dyadic digit swell which can be observed if the dyadic
digit multiplication is used to compute iterations of the logistic map,
for example, and which can cripple performance.

11
12

You might also like