Even or Odd and Data Validation

1,962 views
Skip to first unread message

Scott Ferguson

unread,
May 3, 2014, 10:57:36 PM5/3/14
to

> YouTube video <




Script:

(intro)
Welcome back App Inventors!

I am going to show a simple way to determine if an integer is even or odd using the Math remainder block.

We will also look at some ways to do data validation -- or in other words -- to make sure that what the user enters into a textbox is something that the app can use to do it's computations.

(end intro)

Let's start by entering some integers and see if they are even or odd:

(enter valid integer values)

OK that seems to work.

Let's see how this works:

First we will take a look at the remainder Math block:

the remainder of 8 / 2 is 0 since 8 is even-ly divisible by 2; so 8 is EVEN.

the remainder of 7 / 2 is 1 because 7 is not evenly divisible by 2; so 7 is ODD.

By definition, an integer is even if it is evenly divisible by 2 and this applies to both positive and negative integers.
If an integer is not divisible by 2 then it is odd. Another way to say that is if an integer is divided by two and it's remainder is zero, then it is even. If there is a remainder other than zero it is odd.

It is easier write this in App Inventor blocks the second way.

"If the remainder of the the number divided by 2 is zero, then the number is even otherwise it is odd."

(look at the blocks)
---

Data Validation:

OK let's look at why we need to do data validation:

This app should only allow the user to enter a positive or negative integer such as:

365, 7, 24, -32, etc. so we can determine if it is odd or even.

But what if the user enters letters or punctuation marks instead?

How will the app respond? Most likely with an error message or it may even crash completely.

So -  you will want to find ways to insure that the data entered is valid.

Without using any form of data validation yet, I will enter something into the textbox and view the result:

(enter apple)

We get an error stating: "The operation + cannot accept the arguments: apple 0.5"

(remove error message in blocks editor)

So we need to prevent the user from entering text when we need a number.

We can restrict what the user can type on the soft keyboard to a numeric keypad with a few other math-related keys by ticking the  NumbersOnly property checkbox for the IntegerTextBox component.

(tick the checkbox)

Now let's try that out.

This time the numeric keypad pops up.

If I enter 42 then we see "The number 42 is EVEN."

This is an improvement but it is not foolproof.

If I type in just a '-' symbol then click the submit button I still get the error message.

So what we need is a way to determine if the data entered is actually a number.

App Inventor has a Math block called 'is a number?' which does just that.

So we will incorporate that into our data validation now.

(add "is a number" in if block)

Alright. So now we check if the value entered is a number then if it is ok we do the calculation otherwise we ask the user to try again.

Let's try -62


-62 is even.

OK, how about -6.2?

It says that -6.2 is odd.

What's wrong? is the the blocks?

 --- No, the blocks work only if the data entered is an integer.
But -6.2 is a decimal value not an integer so the test fails.
It will always return ODD for any decimal value because of the way the blocks are constructed but the answer is really nonsense.

Another way of saying this is 'garbage in, garbage out'.

As said earlier, the test for EVEN or ODD requires that the number be an integer.
So we need to prevent the user from entering anyting but integer values.

Fortunately there is an easy way to do this by using the Math round block.

In App Inventor the Math round block works as you would expect for most decimal values:

(show examples)

round(6.9) = 7, and round(6.3) = 6.

It works for negative numbers as well: round(-6.9) = -7 and round(-6.3) = -6.

But round(4.5) = 4 not 5. Why?

(again?) App Inventor always rounds numbers that have an even whole part down when the decimal value is exactly point five (.5)and always rounds numbers that have an odd whole part up when the decimal value is exactly point five (.5)

This gives a better average result when rounding many numbers rather than always rounding numbers containing point five (.5) up.

Now, back to the problem at hand.

How is the round block used to test if a number is an integer?

The idea is to test the original number entered by the user against a rounded version of itself.

If the original number was an integer, then rounding it will have no effect and when comparing the rounded version to the original version:

round(25) = 25 is true; the number is an integer

If, however the original number contained decimal places, then when comparing the rounded decimal number to the original decimal number they will not be equal.

So the original number would not be an integer.

round(4.7) = 4.7 is false; the number is not an integer.

Note that if a number contains only zeros after the decimal point in App Inventor that number is equal to it's integer counterpart.

45 = 45.000 is true.

So let's add the final blocks to complete our data validation to insure that our user's input  is an integer:

(add round blocks)

(test)

OK it seems to be working correctly now.

---
(Conclusion)
Well, we have shown how to determine if an integer is even or odd and a few methods to validate input.
Now go out there and show us your skillz-
And as always-hAPPy INVENTORing!

---
END.
EvenOrOdd.aia

Shao Chen

unread,
Oct 24, 2016, 2:01:48 AM10/24/16
to App Inventor Developers Library
Very useful!! Thank you.
Reply all
Reply to author
Forward
0 new messages