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

Simple Calculator Design

Uploaded by

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

Simple Calculator Design

Uploaded by

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

Simple Calculator:

Java Code:

package com.example.my_application;

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

public class MainActivity extends AppCompatActivity {

// Declare variables for UI components


private EditText number1, number2;
private Button addButton, subtractButton, multiplyButton, divideButton;
private TextView result;

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

// Initialize UI components
number1 = findViewById(R.id.number1);
number2 = findViewById(R.id.number2);
addButton = findViewById(R.id.addButton);
subtractButton = findViewById(R.id.subtractButton);
multiplyButton = findViewById(R.id.multiplyButton);
divideButton = findViewById(R.id.divideButton);
result = findViewById(R.id.result);

// Set up onClick listeners for each button


addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculate('+');
}
});

subtractButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculate('-');
}
});

multiplyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculate('*');
}
});

divideButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculate('/');
}
});
}

// Method to perform the calculation


private void calculate(char operator) {
String num1String = number1.getText().toString();
String num2String = number2.getText().toString();

// Check if inputs are not empty


if (num1String.isEmpty() || num2String.isEmpty()) {
result.setText("Please enter both numbers");
return;
}

double num1 = Double.parseDouble(num1String);


double num2 = Double.parseDouble(num2String);
double calculationResult = 0;

switch (operator) {
case '+':
calculationResult = num1 + num2;
break;
case '-':
calculationResult = num1 - num2;
break;
case '*':
calculationResult = num1 * num2;
break;
case '/':
if (num2 == 0) {
result.setText("Cannot divide by zero");
return;
}
calculationResult = num1 / num2;
break;
}

result.setText("Result: " + calculationResult);


}
}

XML code:

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/number1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter first number"
android:inputType="numberDecimal" />

<EditText
android:id="@+id/number2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter second number"
android:inputType="numberDecimal" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="16dp"
android:gravity="center">

<Button
android:id="@+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add" />

<Button
android:id="@+id/subtractButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subtract"
android:layout_marginStart="8dp" />

<Button
android:id="@+id/multiplyButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Multiply"
android:layout_marginStart="8dp" />

<Button
android:id="@+id/divideButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Divide"
android:layout_marginStart="8dp" />
</LinearLayout>

<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Result will be displayed here"
android:layout_marginTop="24dp"
android:textSize="18sp" />

</LinearLayout>

You might also like