Mad Lab3 20bce7051
Mad Lab3 20bce7051
Mad Lab3 20bce7051
PRADESH
Lab Sheet 3 : Calculator
Academic year: 2021-2022 Branch/ Class: B.Tech
Semester: Fall Date:
Faculty Name: Dr. N. Anupama School: SCOPE
NAME: Salome N Rao Daliya REGNO:20BCE7051
*******************************************************************
<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">
<EditText
android:id="@+id/digit"
android:layout_width="394dp"
android:layout_height="91dp"
android:ems="10"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.47"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.056" />
<LinearLayout
android:layout_width="414dp"
android:layout_height="106dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.666"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.219">
<Button
android:id="@+id/button1"
android:layout_width="100dp"
android:layout_height="90dp"
android:layout_weight="1"
android:text="1" />
<Button
android:id="@+id/button2"
android:layout_width="100dp"
android:layout_height="90dp"
android:text="2" />
<Button
android:id="@+id/button3"
android:layout_width="100dp"
android:layout_height="90dp"
android:text="3" />
<Button
android:id="@+id/buttonaddition"
android:layout_width="100dp"
android:layout_height="90dp"
android:text="+" />
</LinearLayout>
<LinearLayout
android:layout_width="414dp"
android:layout_height="106dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.333"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.421">
<Button
android:id="@+id/button4"
android:layout_width="100dp"
android:layout_height="90dp"
android:layout_weight="1"
android:text="4" />
<Button
android:id="@+id/button5"
android:layout_width="100dp"
android:layout_height="90dp"
android:text="5" />
<Button
android:id="@+id/button6"
android:layout_width="100dp"
android:layout_height="90dp"
android:text="6" />
<Button
android:id="@+id/buttonsubtraction"
android:layout_width="100dp"
android:layout_height="90dp"
android:text="-" />
</LinearLayout>
<LinearLayout
android:layout_width="414dp"
android:layout_height="106dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.627">
<Button
android:id="@+id/button7"
android:layout_width="100dp"
android:layout_height="90dp"
android:layout_weight="1"
android:text="7" />
<Button
android:id="@+id/button8"
android:layout_width="100dp"
android:layout_height="90dp"
android:text="8" />
<Button
android:id="@+id/button9"
android:layout_width="100dp"
android:layout_height="90dp"
android:text="9" />
<Button
android:id="@+id/buttonmultiplication"
android:layout_width="100dp"
android:layout_height="90dp"
android:text="*" />
</LinearLayout>
<LinearLayout
android:layout_width="414dp"
android:layout_height="106dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.845">
<Button
android:id="@+id/buttoncalculate"
android:layout_width="100dp"
android:layout_height="90dp"
android:layout_weight="1"
android:text="C" />
<Button
android:id="@+id/buttonzero"
android:layout_width="100dp"
android:layout_height="90dp"
android:text="0" />
<Button
android:id="@+id/buttonequals"
android:layout_width="100dp"
android:layout_height="90dp"
android:text="=" />
<Button
android:id="@+id/buttondivision"
android:layout_width="100dp"
android:layout_height="90dp"
android:text="/" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Java code:-
package com.example.calculatorapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b3=(Button)findViewById(R.id.button3);
Button b4=(Button)findViewById(R.id.button4);
Button b5=(Button)findViewById(R.id.button5);
Button b6=(Button)findViewById(R.id.button6);
Button b7=(Button)findViewById(R.id.button7);
Button b8=(Button)findViewById(R.id.button8);
Button b9=(Button)findViewById(R.id.button9);
Button b0=(Button)findViewById(R.id.buttonz);
Button be=(Button)findViewById(R.id.buttone);
Button bp=(Button)findViewById(R.id.buttonp);
Button bs=(Button)findViewById(R.id.buttons);
Button bm=(Button)findViewById(R.id.buttonm);
Button bd=(Button)findViewById(R.id.buttond);
Button bc=(Button)findViewById(R.id.buttonc);
b0.setOnClickListener(new View.OnClickListener()
@Override
if (!(e.getText().toString().equals("")))
e.setText(String.valueOf(e.getText().toString()+0));
});
b1.setOnClickListener(new View.OnClickListener()
e.setText(String.valueOf(e.getText().toString() + 1));
});
b2.setOnClickListener(new View.OnClickListener()
e.setText(String.valueOf(e.getText().toString()+2));
});
b3.setOnClickListener(new View.OnClickListener()
e.setText(String.valueOf(e.getText().toString()+3));
});
b4.setOnClickListener(new View.OnClickListener()
e.setText(String.valueOf(e.getText().toString()+4));
});
b5.setOnClickListener(new View.OnClickListener()
{
e.setText(String.valueOf(e.getText().toString()+5));
});
b6.setOnClickListener(new View.OnClickListener()
e.setText(String.valueOf(e.getText().toString()+6));
});
b7.setOnClickListener(new View.OnClickListener()
e.setText(String.valueOf(e.getText().toString()+7));
});
b8.setOnClickListener(new View.OnClickListener()
e.setText(String.valueOf(e.getText().toString()+8));
});
b9.setOnClickListener(new View.OnClickListener()
e.setText(String.valueOf(e.getText().toString()+9));
});
bc.setOnClickListener(new View.OnClickListener()
e.setText(String.valueOf(""));
}
});
bp.setOnClickListener(new View.OnClickListener()
op='+';
result=Double.parseDouble(e.getText().toString());
e.setText(String.valueOf(""));
});
bs.setOnClickListener(new View.OnClickListener()
@Override
op='-';
result=Double.parseDouble(e.getText().toString());
e.setText(String.valueOf(""));
});
bm.setOnClickListener(new View.OnClickListener()
{
@Override
op='*';
result=Double.parseDouble(e.getText().toString());
e.setText(String.valueOf(""));
});
bd.setOnClickListener(new View.OnClickListener()
@Override
op='/';
result=Double.parseDouble(e.getText().toString());
e.setText(String.valueOf(""));
});
be.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
if(op=='+')
double n=Double.parseDouble(e.getText().toString());
e.setText(String.valueOf(result+n));
else if (op=='-')
double n=Double.parseDouble(e.getText().toString());
e.setText(String.valueOf(result-n));
else if (op=='*')
double n=Double.parseDouble(e.getText().toString());
e.setText(String.valueOf(result*n));
else
{
double n=Double.parseDouble(e.getText().toString());
e.setText(String.valueOf(result/n));
});
OUTPUT:-
ADDITION:-
SUBTRACTION:-
MULTIPLICATION:-
DIVISION:-