MADEX01
MADEX01
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:
<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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 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();
}
}
});
}
OUTPUT:
RESULT:
Thus Android application to implement the simple calculator is performed and
excuted successfully.