In: Computer Science
Write an Android phone program such that two instances
of EditText with texts “Input a positive integer (smaller)” and
“Input a positive integer (larger)”, one instances of Button with
text “Click here to get outputs”, and four instances of TextView
with respective texts “Decide if the larger one is a multiple of
the smaller one : ”, “The GCD of two integers : ”, “The LCM of the
two integers : ”, “The permutations based on two integers :
”, “The combinations based on the two integers : “, and "The
definite integral of 3*x*x of from smaller integer to larger
integer : " are displayed vertically on the AVD. The users of your
program will input two positive integers. After the users click the
button, the information or values based on the two input integers
will be displayed in the corresponding six instances of
TextView. (Hints: GCD denotes the greatest common divisor and
LCM denotes the least common multiple.)
OUTPUT
Code: MainActivity.class
package cheggans.app; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final EditText etnum1=findViewById(R.id.num1); final EditText etnum2=findViewById(R.id.num2); final TextView tvcheck=findViewById(R.id.checktv); final TextView tvgcd=findViewById(R.id.gcdtv); final TextView tvlcm=findViewById(R.id.lcmtv); final TextView tvperm=findViewById(R.id.permtv); final TextView tvcomb=findViewById(R.id.combtv); final TextView tvintg=findViewById(R.id.ingrltv); Button btn=findViewById(R.id.button); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int num1=Integer.parseInt(etnum1.getText().toString()); int num2=Integer.parseInt(etnum2.getText().toString()); tvcheck.setText("Decide if the larger one is a multiple of the smaller one :" + checklarger(num1,num2)); tvgcd.setText("The GCD of two integers :"+getGcd(num1,num2)); tvlcm.setText("The LCM of the two integers : :"+getLcm(num1,num2)); tvperm.setText("The permutations based on two integers : "+getPerm(num1,num2)); tvcomb.setText("The combinations based on the two integers : "+getComb(num1,num2)); tvintg.setText("The definite integral is :"+getIntegral(num1,num2)); } }); } public int checklarger(int a,int b){ int c=0; if(a>b){ c=a; } else{ c=b; } return c; } public int getGcd(int a,int b){ int n1 = a, n2 = b, gcd = 1; for(int i = 1; i <= n1 && i <= n2; ++i) { // Checks if i is factor of both integers if(n1 % i==0 && n2 % i==0) gcd = i; } return gcd; } public int getLcm(int a,int b){ int n1 = a, n2 = b, lcm; // maximum number between n1 and n2 is stored in lcm lcm = (n1 > n2) ? n1 : n2; // Always true while(true) { if( lcm % n1 == 0 && lcm % n2 == 0 ) { break; } ++lcm; } return lcm; } static int fact(int n) { int fact = 1; int i = 1; while(i <= n) { fact *= i; i++; } return fact; } public int getPerm(int a,int b){ return fact(a) / fact(a - b); } int getComb(int n, int r) { return fact(n) / (fact(r) *fact(n - r)); } int getIntegral(int a, int b) { //integral of 3*x*x is x^3 int lowerBound= a*a*a; int upperBound= b*b*b; return upperBound-lowerBound; } }
XML: activity_main
<?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/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="91dp" android:text="Click here to get outputs" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/num2" /> <EditText android:id="@+id/num2" android:layout_width="290dp" android:layout_height="wrap_content" android:layout_marginTop="29dp" android:ems="10" android:hint="Input a positive Integer(larger)" android:inputType="number" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/num1" /> <EditText android:id="@+id/num1" android:layout_width="290dp" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:ems="10" android:hint="Input a positive Integer(smaller)" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <LinearLayout android:id="@+id/textView6" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/button"> <TextView android:id="@+id/checktv" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:padding="5dp" android:text="" /> <TextView android:id="@+id/gcdtv" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:padding="5dp" android:text="" /> <TextView android:id="@+id/lcmtv" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:padding="5dp" android:text="" /> <TextView android:id="@+id/permtv" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:padding="5dp" android:text="" /> <TextView android:id="@+id/combtv" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:padding="5dp" android:text="" /> <TextView android:id="@+id/ingrltv" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:padding="5dp" android:text="" /> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>