Sunday, March 4, 2018

An Android App to check if a number is Prime : Advance Java programming



The Advance java program (source code) of the above Android App made by me is written below
----------------------------------------------------------------------------------------------------------------------

package com.example.primecheck.primecheck;
//Assigment week 2
//author: Ashutosh Kumar Singh, Mobile computing course NPTEL, July 2016. On 07-08-2016
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
import android.view.Menu;


public class FindPrime extends AppCompatActivity {

    private Button btrue, bfalse;
    private Button bnext;
    private TextView tvN;
    int k;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_prime);

       bnext = (Button) findViewById(R.id.btnN);
        btrue = (Button) findViewById(R.id.btnT);
        bfalse = (Button) findViewById(R.id.btnF);
        tvN = (TextView) findViewById(R.id.tvN);
        tvN.setText("19");



        addListenerOnButton();
    }
    public void addListenerOnButton()
    {


        bnext.setOnClickListener
                (
                        new View.OnClickListener()
                        {
                            @Override
                            public void onClick(View v) {

                               k = myPrime();
                                String s = Integer.toString(k);
                                tvN.setText(s);


                            }

                        }
                );

        btrue.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        int i,  flag = 0;


                        for (i = 2; i <= k/ 2; i++)
                        {
                            if (k % i == 0)
                            {
                                Toast.makeText(getApplicationContext(), "Incorrect", Toast.LENGTH_SHORT).show();
                                flag = 1;
                                break;
                            }
                        }
                        if (flag == 0) {
                            Toast.makeText(getApplicationContext(), "Correct", Toast.LENGTH_SHORT).show();
                        }


                    }

                });

        bfalse.setOnClickListener
                (
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                int i, flag = 0;

                                for (i = 2; i <= k / 2; i++) {
                                    if (k % i == 0) {
                                        Toast.makeText(getApplicationContext(), "Correct", Toast.LENGTH_SHORT).show();
                                        flag = 1;
                                        break;
                                    }
                                }
                                if (flag == 0)
                                {
                                    Toast.makeText(getApplicationContext(), "Incorrect", Toast.LENGTH_SHORT).show();
                                }


                            }
                        }


                );
    }

    int myPrime()
     {
        Random r = new Random();

         int j = r.nextInt(100);
         String s=Integer.toString(j);
        return j;
    }
}


Thanks
Happy apping !

No comments:

Post a Comment

How can I run a C++ program directly from Windows?

How-can-I-run-a-C-program-directly-from-Windows