In: Computer Science
You recently started working in at a University in the Math department as a software programmer. You need to build an app that can calculate the mod of two numbers. Users need to enter num1 and num2. The app should perform the calculation and display the output as num1 mod nub2 operation.
For example, 25 mod 5=0
24 mod 5 = 4
Once you implement the mod operations, then you need to add following buttons.
Follow these steps to complete the lab.
I am adding the XML and java code files below:
Say your project name in Android Studio that you've created be "MyApplication" and package name given be "com.pac":
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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/etNum1" android:layout_width="152dp"
android:layout_height="78dp" android:layout_marginStart="8dp"
android:layout_marginTop="8dp" android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp" android:ems="10"
android:hint="number 1" android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.106"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.062" /> <EditText
android:id="@+id/etResult" android:layout_width="152dp"
android:layout_height="78dp" android:layout_marginStart="8dp"
android:layout_marginTop="8dp" android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp" android:ems="10"
android:hint="Result" android:inputType="textPersonName"
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.875" /> <Button
android:id="@+id/btnMod" android:layout_width="154dp"
android:layout_height="58dp" android:layout_marginStart="8dp"
android:layout_marginTop="8dp" android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp" android:text="MOD"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.531"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.302" /> <Button
android:id="@+id/btnPow" android:layout_width="154dp"
android:layout_height="58dp" android:layout_marginStart="8dp"
android:layout_marginTop="8dp" android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp" android:text="POWER"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.887"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.654" /> <Button
android:id="@+id/btnDiv" android:layout_width="154dp"
android:layout_height="58dp" android:layout_marginStart="8dp"
android:layout_marginTop="8dp" android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp" android:text="DIVIDE"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.099"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.654" /> <Button
android:id="@+id/btnSub" android:layout_width="154dp"
android:layout_height="58dp" android:layout_marginStart="8dp"
android:layout_marginTop="8dp" android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp" android:text="SUB"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.887"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.499" /> <EditText
android:id="@+id/etNum2" android:layout_width="152dp"
android:layout_height="78dp" android:layout_marginStart="8dp"
android:layout_marginTop="8dp" android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp" android:ems="10"
android:hint="number 2" android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.88"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.062" /> <Button
android:id="@+id/btnAdd" android:layout_width="154dp"
android:layout_height="58dp" android:layout_marginStart="8dp"
android:layout_marginTop="8dp" android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp" android:text="ADD"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.107"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.499" /> <TextView
android:id="@+id/textView4" android:layout_width="217dp"
android:layout_height="24dp" android:layout_marginStart="8dp"
android:layout_marginTop="8dp" android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp" android:text="Calculation
options:" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.707"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.218" />
</android.support.constraint.ConstraintLayout>
MainActivity.java
package com.pac; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends AppCompatActivity { EditText etNum1, etNum2, etResult; Button btnMod, btnAdd, btnSub, btnDiv, btnPow; int num1, num2; double result; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_chatbot); etNum1=findViewById(R.id.etNum1); etNum2=findViewById(R.id.etNum2); etResult=findViewById(R.id.etResult); btnAdd=findViewById(R.id.btnAdd); btnMod=findViewById(R.id.btnMod); btnSub=findViewById(R.id.btnSub); btnDiv=findViewById(R.id.btnDiv); btnPow=findViewById(R.id.btnPow); btnMod.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { num1=Integer.parseInt(etNum1.getText().toString()); num2=Integer.parseInt(etNum2.getText().toString()); etResult.setText(String.valueOf(num1%num2)); } }); btnAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { num1=Integer.parseInt(etNum1.getText().toString()); num2=Integer.parseInt(etNum2.getText().toString()); etResult.setText(String.valueOf(num1+num2)); } }); btnPow.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { num1=Integer.parseInt(etNum1.getText().toString()); num2=Integer.parseInt(etNum2.getText().toString()); etResult.setText(String.valueOf(Math.pow(num1,num2))); } }); btnSub.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { num1=Integer.parseInt(etNum1.getText().toString()); num2=Integer.parseInt(etNum2.getText().toString()); etResult.setText(String.valueOf(num1-num2)); } }); btnDiv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { num1=Integer.parseInt(etNum1.getText().toString()); num2=Integer.parseInt(etNum2.getText().toString()); etResult.setText(String.valueOf(num1/num2)); } }); } }