Just Maths - Android App (Main Logic)

This is the last post for this app - Just Maths. Best tutorial for beginners to start learning android. So let's just jump right in for the final code and you can download the code as well. In this post, i will just walk-through and integrate the code that we have done so far in our previous android posts. If you have not gone through them, please read those to understand the concepts. You can find the list here - Android

CheezyCode - Just Maths Android App Full Source Code

Code snippet for generating the random numbers and converting them to create random Maths Questions which are either correct or wrong.

    private void generateQuestion() {
        isResultCorrect = true; 
        Random random = new Random();
        int a = random.nextInt(100);
        int b = random.nextInt(100);
        int result = a + b;
        float f = random.nextFloat();
        if (f > 0.5f) {
            result = random.nextInt(100);
            isResultCorrect = false;
        txtViewQuestion.setText(a + "+" + b);
        txtViewResult.setText("=" + result);

In this code, we first assumed that we will show the correct answer - we generated 2 random numbers and result of their addition is stored in result variable. Then we again generated a floating point random number between 0 and 1. If this floating number is greater than 0.5f then we change the result and make it incorrect. Above logic is same as flipping a coin. If it is head then we won't change the answer and the question is actually correct and if it is tail then we change the answer to create a wrong question.

HEAD (f in line 7 is less than 0.5) - Question is Correct.
TAIL (f in line 7 is greater than 0.5) - Question is InCorrect.

To check the user input we used a simple function.

    public void verifyAnswer(boolean answer) {
        if (answer == isResultCorrect) {
            score += 5;
            txtScore.setText("SCORE: " + score);
        } else {
            Vibrator vibrator = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);

This function will be called from buttons on the screen. If user presses the correct, then verifyAnswer(true) otherwise verifyAnswer(false). We have used the Vibrator system service when the answer is incorrect. This is what we have for this app. Download the source code of the app provided at below link.

This is a quick starter series on android for beginners. In this app - you have learned about activities, intents - explicit and implicit intents, putExtra and getExtra, button clicks, event listeners, share messages across activities and much more.

Let us know if you have any questions or queries. Happy Reading.

Download Source


  1. When i type the wrong Answer the Score Activity Should be opened but unfortunately the main Activity is opened why ??


Post a Comment

Hey there, liked our post. Let us know.

Please don't put promotional links. It doesn't look nice :)

Popular posts from this blog

Polymorphism in Kotlin With Example

Create Android Apps - Getting Started