CALULATOR PROGRAM:
XML FILE:-
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="19dp"
android:text="add"
android:textSize="10dp"
app:layout_constraintStart_toStartOf="@+id/div"
app:layout_constraintTop_toBottomOf="@+id/div" />
<TextView
android:id="@+id/ans"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:text="ANSWER"
android:textSize="30dp"
app:layout_constraintStart_toEndOf="@+id/add"
app:layout_constraintTop_toBottomOf="@+id/add" />
<Button
android:id="@+id/sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="75dp"
android:layout_marginRight="75dp"
android:layout_marginBottom="25dp"
android:text="subtract"
android:textSize="10dp"
app:layout_constraintBottom_toTopOf="@+id/ans"
app:layout_constraintEnd_toEndOf="@+id/sn" />
<Button
android:id="@+id/multi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="19dp"
android:text="multiply"
android:textSize="10dp"
app:layout_constraintBottom_toTopOf="@+id/sub"
app:layout_constraintStart_toStartOf="@+id/sub" />
<Button
android:id="@+id/div"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="48dp"
android:layout_marginLeft="48dp"
android:layout_marginEnd="51dp"
android:layout_marginRight="51dp"
android:layout_marginBottom="316dp"
android:text="Divide"
android:textSize="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/multi"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/tfn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="36dp"
android:layout_marginLeft="36dp"
android:layout_marginTop="195dp"
android:layout_marginBottom="193dp"
android:text="FIRST NUMBER"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="@+id/div"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/fn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:hint="FIRST NUMBER"
android:inputType="number"
app:layout_constraintBaseline_toBaselineOf="@+id/tfn"
app:layout_constraintStart_toEndOf="@+id/tfn" />
<EditText
android:id="@+id/sn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="60dp"
android:layout_marginRight="60dp"
android:layout_marginBottom="24dp"
android:hint="SECOND NUMBER"
android:inputType="number"
app:layout_constraintBottom_toTopOf="@+id/multi"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/tsn" />
<TextView
android:id="@+id/tsn"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="27dp"
android:layout_marginLeft="27dp"
android:layout_marginEnd="2dp"
android:layout_marginRight="2dp"
android:text="SECOND NUMBER"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="@+id/sn"
app:layout_constraintEnd_toStartOf="@+id/sn"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/sn" />
</androidx.constraintlayout.widget.ConstraintLayout>
========================================================================================================================
JAVA FILE:-
package com.example.main_q18;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
EditText e1,e2;
TextView t1;
Button b1,b2,b3,b4;
double ans;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1=findViewById(R.id.fn);
e2=findViewById(R.id.sn);
t1=findViewById(R.id.ans);
b1=(Button) findViewById(R.id.add);
b2=(Button) findViewById(R.id.sub);
b3=(Button) findViewById(R.id.multi);
b4=(Button) findViewById(R.id.div);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Double e=Double.parseDouble(e1.getText().toString());
Double f=Double.parseDouble(e2.getText().toString());
ans=e+f;
String ans1=String.valueOf(ans);
t1.setText(ans1);
ans=0;
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Double e=Double.parseDouble(e1.getText().toString());
Double f=Double.parseDouble(e2.getText().toString());
ans=e-f;
String ans1=String.valueOf(ans);
t1.setText(ans1);
ans=0;
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Double e=Double.parseDouble(e1.getText().toString());
Double f=Double.parseDouble(e2.getText().toString());
ans=e*f;
String ans1=String.valueOf(ans);
t1.setText(ans1);
ans=0;
}
});
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Double e=Double.parseDouble(e1.getText().toString());
Double f=Double.parseDouble(e2.getText().toString());
ans=e/f;
String ans1=String.valueOf(ans);
t1.setText(ans1);
ans=0;
}
});
}
}
==================================================================================================