Question

You recently started working in at a University in the Math department as a software programmer....

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.

  • Addition
  • Subtraction
  • Division
  • Power

Follow these steps to complete the lab.

  1. Create the android studio project
  2. Use Android studio layout editor
  3. Add textboxes and labels according to the UI
  4. Add Button in the UI
  5. Review the XML code of the design
  6. Declare the variable before the oncreate function
  7. Read the value from textboxes and assign it to the variable
  8. Add the click event for the button
  9. Display the output
  10. Declare the variable before the oncreate function

Homework Answers

Answer #1

XML and Java Code provided below

Project name "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));
           }
       });
   }
}


Please UPVOTE thank you...!!!

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Scenario: You recently started working as the mobile app developer for a University and have been...
Scenario: You recently started working as the mobile app developer for a University and have been assigned to build a mobile app that calculates the students' grade. Your app should contain following screens: Screen 1 Labels for subject1, subject2, and subject3 Textboxes for subject1, subject2, and subject3 Labels for MaxGrade, MinGrade, and Avg.Grade Submit button Once user clicks submit button, you need to display letter grade in a label Screen 2 Screen that allows students to register for the course...
I need the java code for a 4-function calculator app on android studio. Please make sure...
I need the java code for a 4-function calculator app on android studio. Please make sure all the requirements shown below are followed (such as the error portion and etc). The topic of this app is to simply create a 4 function calculator which calculates math expressions (add, subtract, multiply, and divide numbers). The requirements are the following : - The only buttons needed are 0-9, *, /, +, -, a clear, and enter button - Implement the onclicklistener on...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT