Slip 5 - D) Create an Android Application to find the factorial of a number and Display the Result on Alert Box.

Solution:


Main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et_no"
        android:layout_gravity="center"
        android:hint="enter the number">

    </EditText>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et_ans"
        android:layout_gravity="center"
        android:hint="answer">

    </TextView>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bt_cal"
        android:text="calculate factorial">

    </Button>

</LinearLayout>

.java file:

package com.example.demofactorial;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    EditText number;
    TextView answer;
    Button calculate;
    AlertDialog.Builder builder;
    int factorial= 1;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize();
        calcFactorial();
        builder = new AlertDialog.Builder(this);
    }
    public void initialize() {
        number = (EditText) findViewById(R.id.et_no);
        answer = (TextView) findViewById(R.id.et_ans);
        calculate = (Button) findViewById(R.id.bt_cal);
        calculate.setOnClickListener(this);
    }
    public void calcFactorial() {
        if (number.getText().toString().equals("")) number.setText("0");
        int num = Integer.parseInt(number.getText().toString());
        for (int i = 1; i <= num; i++) {
            factorial = i * factorial;
        }
    }




    @Override
    public void onClick(View v){
        calcFactorial();
            //Setting message manually and performing action on button click
            builder.setMessage("Factorial of " + number.getText().toString() + " is : " + factorial)
                    .setCancelable(false)
                    .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            finish();
                        }
                    })
                    .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                        }
                   });
        //Creating dialog box
        AlertDialog alert = builder.create();
        //Setting the title manually
        alert.setTitle("FactorialAlertDialogExample");
        alert.show();


        answer.setText("Factorial of " + number.getText().toString() + " is : " +     factorial);


    }
}

Post a Comment

0 Comments