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

MADEX01

Uploaded by

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

MADEX01

Uploaded by

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

61781922106041

EX NO: 01
CALCULATOR WITH SIMPLE OPERATION
DATE: 5/8/2024

AIM:
To develop an Android application that will perform simple calculator operation.

PROCEDURE:

step 1: Start
step 2: Open Android Studio and create a new project
step 3: Create a simple layout with buttons for digits (0-9), operators (+, -, *, /), and
an equals button
step 4: Add an `EditText` to display the input and result
step 5: Set `OnClickListener` for each button to append digits or operators to the input
step 6: Implement logic to handle simple operations (addition, subtraction,
multiplication, division)
step 7: Use `TextUtils` or simple string manipulation to parse the input
step 8: Perform the operation when the equals button is clicked and display the result
step 9: Handle edge cases like division by zero
step 10: Stop

ACTIVITY_MAIN.XML:

<?xml version="1.0" encoding="utf-8"?>


<androidx.constraintlayout.widget.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:id="@+id/main" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
61781922106041

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculator"
android:textSize="50sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.047" />

<EditText
android:id="@+id/input1"
android:layout_width="296dp"
android:layout_height="55dp"
android:inputType="number"
android:text=""
app:layout_constraintTop_toBottomOf="@id/textView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintVertical_bias="0.225" />

<EditText
android:id="@+id/input2"
android:layout_width="290dp"
android:layout_height="52dp"
android:inputType="number"
android:text=""
61781922106041
app:layout_constraintTop_toBottomOf="@id/input1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintVertical_bias="0.377" />

<Button
android:id="@+id/btnadd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
app:layout_constraintTop_toBottomOf="@id/input2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintVertical_bias="0.5" />

<TextView
android:id="@+id/res"
android:layout_width="296dp"
android:layout_height="55dp"
android:text=""
android:textSize="100sp"
app:layout_constraintTop_toBottomOf="@id/btnadd"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintVertical_bias="0.643" />

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
61781922106041
android:text="Num1"
app:layout_constraintBottom_toTopOf="@id/input1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintVertical_bias="0.252" />

<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Num2"
app:layout_constraintBottom_toTopOf="@id/input2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintVertical_bias="0.393" />

<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/border"
android:text="Reg No : 61781922106062"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintVertical_bias="0.899" />

</androidx.constraintlayout.widget.ConstraintLayout>

MAINACTIVITY.JAVA:
package com.example.calculator;
61781922106041

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


Button b1, b2, b3, b4;
TextView t1, t2, t3;

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

b1 = findViewById(R.id.button); // Addition Button


b2 = findViewById(R.id.button2); // Subtraction Button
b3 = findViewById(R.id.button3); // Multiplication Button
b4 = findViewById(R.id.button4); // Division Button

t1 = findViewById(R.id.editTextText); // First input


t2 = findViewById(R.id.editTextText2); // Second input
t3 = findViewById(R.id.editTextText4); // Result display

// Addition
b1.setOnClickListener(view -> {
if (validateInputs()) {
int a = Integer.parseInt(t1.getText().toString());
61781922106041
int b = Integer.parseInt(t2.getText().toString());
int result = a + b;
t3.setText(String.valueOf(result));
}
});

// Subtraction
b2.setOnClickListener(view -> {
if (validateInputs()) {
int a = Integer.parseInt(t1.getText().toString());
int b = Integer.parseInt(t2.getText().toString());
int result = a - b;
t3.setText(String.valueOf(result));
}
});

// Multiplication
b3.setOnClickListener(view -> {
if (validateInputs()) {
int a = Integer.parseInt(t1.getText().toString());
int b = Integer.parseInt(t2.getText().toString());
int result = a * b;
t3.setText(String.valueOf(result));
}
});

// Division
b4.setOnClickListener(view -> {
if (validateInputs()) {
float a = Integer.parseInt(t1.getText().toString());
61781922106041
float b = Integer.parseInt(t2.getText().toString());
if (b != 0) {
float result = a / b;
t3.setText(String.valueOf(result));
} else {
Toast.makeText(MainActivity.this, "Cannot divide by zero",
Toast.LENGTH_SHORT).show();
}
}
});
}

// Validate inputs before performing any operations


private boolean validateInputs() {
if (t1.getText().toString().isEmpty() || t2.getText().toString().isEmpty()) {
Toast.makeText(this, "Please enter valid numbers", Toast.LENGTH_SHORT).show();
return false;
}
return true;
}
}
61781922106041

OUTPUT:

RESULT:
Thus Android application to implement the simple calculator is performed and
excuted successfully.

You might also like