Hello and welcome back.
In this tutorial we'll be talking about if statements in order talk about if statements will create
a new app that calculates movie prices.
All right.
So sit down and let's go because this one's going to be a bit of a long one.
So let's do it.
If you think you can handle it.
All right.
So first thing we're going to do is we're going to start a new project to go ahead and go to file new
file new new project.
ALL RIGHT LET'S LET'S SEE WHAT SHOULD WE CALL THIS IF YOU THINK YOU KNOW HOW ABOUT movie app.
There you go.
Movie Up next April 15.
Perfect.
Empty activity main activity everything is how we need it to be.
All right so the first thing we're going to do as always is we're going to change our activity main
to have the layout that we want.
So we're going to add a couple of spinners which is gonna be a new concept I'll talk about and edit
text or text view and then another TextView.
Another thing we'll be kind of going over today is a raise.
So arrays are basically a list of objects.
So a String array for example would be a shopping list.
Eggs milk butter.
Each one of those is a String object.
And then the Array holds it.
And I'll show you how to initialize them when we get to that array.
Let's go
it's loading ever so quickly
right.
We may have to wait for the bill to finish.
Yes of course we do.
But that's all right.
Just a little patience to get to hang out with me just a little bit longer.
So there's actually not going to be too many actual strings and the strings that Excel file today is
going to be the app name the chicken message and then the price of the ticket and then it's just going
to be a placeholder price and it's going to start at zero dollars.
But unfortunately you can't just get into the movies for free.
So there are going to be a bit of a price we're going to set on that.
Also I mentioned we're going to be looking at spinners spinners is basically what you would call a drop
down menu where you can select things.
I'm not quite sure where the term spinner comes from but it's pretty cool.
Frugal sounding and it's very handy.
All right.
So let's go ahead and first make all the strings we need so go into values which is under resources.
Strings close out of styles if you accidentally opened it like I did.
All right now String name equals ticket message and that's going to go ahead and be your ticket price
is what's going to be what's going to be.
Well we'll find out when we make the string for the ticket price price zero dollars.
All right.
So these are just what's going to pop up when the app is first open so let's go ahead and change hello
world to be at string slash ticket message and then let's make that what's a good one.
Why not 50 from the top.
Let's see let's go ahead and just drag that up
and then
I hit that little button that I just click here I'll undo.
So go ahead.
And hit this button to undo all the constraints so we can move it up and down all right.
Oh I spelled message wrong.
That's why I didn't change string message.
Perfect.
Your ticket price is dead.
So going to make that 50 from the top and then I'm going to center this horizontally.
Let's go ahead and add the text view for the price
so go ahead and change this stuff at string slash price.
Now I want to go and change the idea to price
and then let's make this also 50 from the top
and five away from your ticket prices
and then let's go ahead and add our spinners.
So if you go over the palette you can just go ahead and search up spinner and drag those on.
Here let me close out of these
so add one spinner and then go ahead and add another spinner.
So the first spinner is going to be selecting what movie you're going to see and then the second spinner
what that's going to be is liking the time we're gonna go see the movie.
So let's go ahead and center these horizontally
and let's add some ideas to those
I'm going to go ahead and call this
open up attributes.
Gonna call the top 1 movie spinner.
I'm gonna go ahead and call the bottom 1 time spinner
let's have this be
50 from your ticket price and then have this one be let's say 50 from the bottom of the movie spinner.
All right all right all right.
So I think that wraps it up for the main activity.
Now we're going to go over to or star activity Main.
Now we're going to go over to Main activity.
So this is where all the magic happening today.
We're going to go over some functions and listeners.
So I think we did a listener for the very first tutorial for the button for this.
We're going to talk about it a little bit more.
So let's go ahead and create the arrays so first we're going to do is create movie lists array.
And again this is a comment which you can see is denoted by the two forward slashes.
So this isn't part of the code to the final meaning it can't change.
String array movies and then we're going to go ahead initialize array right away.
So typically there's two ways you initialize an array.
You either just do that for example int array array equals new end and then you say how big this array
is.
So it's five and then you would go array at index 0 equals zero.
So then now you have zero and then empty values for the rest and I'm not sure if we talk about indexing.
So I'm to go over that really quick so arrays cannot change size.
Some languages you can change in array size but once you make an array it's going to be this size forever.
So if you make an integer array that's 5 it can hold maximum 5 numbers.
That's it.
You'd have to make a new array in order hold more or less numbers and then the way you indexing is you
start at zero and then you count up.
So it'd be 0 1 2 3 4
so if you wanted to access the number the first number you would do array index 0.
The fourth number would be array index 3 and the fifth number is array index 4.
And that's the last number of the array.
All right.
So let's go ahead and finish making our movies array.
This is the second way you can initialize arrays.
You can use curly braces and then you can go ahead and just type in the strings and separate them by
comments.
I'm gonna call it genetics is first movie then revenge was always battle
and then I'm going to do novel group and then finally the driver.
So these are movies that we can go see and then we need to create arrays for the movie times.
So the times you can see the movies.
So final String
movie won times.
So these are the times you can see the first movie I want to initialize it like how I did the other
ones with the curly braces.
You can see it at 11:00 am 3 p.m. and 7 5 p.m. and then let's make the movie to time.
So final String movie two times equals
0.
Make sure to capitalize string.
There you go string eleven am
128 2:00 p.m. and 5:00 p.m. Let's do 7 fifty five p.m. again.
So let's say the movie open movie theater opens at 11:00 and then happens to close around 10:00.
I think that's pretty reasonable
what these red squiggles for your semicolon.
Don't forget your semicolon very important final String movie three times let's do 11:00 AM classic
and then one fifteen p.m. five forty five p.m. And you can choose whatever times you want.
I'm just kind of throwing some random times out there.
Final String movie four times.
You know what I think we're pretty good so let's just do Three thirty p.m. boom got our chimes in.
So now we need prices to coordinate with correlate with these times.
So I'm just going to count up from eight.
I mean it's a pretty good movie ticket.
And then I'll just change the numbers up so then when we change our settings we can see that the numbers
change.
So final.
And so this is an integer array movie 1 Prices equals eight nine 10.
Final and Movie 2.
Prices
eleven twelve thirteen fourteen.
Can you guess what the next price is going to be.
All right let's do it.
You ever guess.
Yes you're right.
15 16 17.
And finally that's due on for eighteen dollars it's a really expensive movie.
It's really good though I promise.
Eighteen for the writer Oh I'm sorry.
I mean the driver was cool so now we're ready to get the movie spinner so we're going to make the movie
spinner so add things to our drop down menu.
So let's do a method to make a spinner.
So if we zoom out we can see that we're currently in the on create method which is what the app does
when it's made.
So now we're going to go out the side the braces and then add our new method.
Private spinner.
So it's a private method that only this class can see it's going to return a spinner object the name
of it's called makes thinner and then it's going to take the arguments integer I.D. key and then string
array array so the key is what we use for like fine view by I.D. so it can find which spinner to make
so then you're going to do spinner spinner equals spinner.
Round and round find view by the key.
So this is finding the spinner that we want to makes.
So if we do the I.D. key for movie it's going to change the movie spinner.
We do four times is gonna change the time spinner.
Now we're going to go ahead and have to import the spinner spinner class.
Go ahead and do import Android the widget it's better
then while we're up here go ahead and do TextView to import Android widget TextView perfect so let's
go ahead and finish up our method so we're going to have to use an array adapter so let's go ahead and
import those to import Android widget array adapter import Android widget
adapter view import Android view you.
All right.
Now we should be good on our imports.
Let's go back down and finish this up.
So what the array adapter is going to do it is going to adapt our array to be able to use or to be able
to be used in the spinner.
So let's make an array adapter and it's going to be a string spinner array adapter equals new new Array
adapter string
and then we're going to pass it this
and then we're also going to pass it.
Android are the layout.
Simple spinner item
then are.
So this is just creating a new array adapter and then setting our layout to simple spinner item because
that's what we chose for our.
That's the default for the spenders.
It's a simple spinner item and then we're passing it the array to be converted and put in there.
So now we're gonna do spinner array adapter set dropdown view resource Android
art layout symbols spinner dropdown item
0.
Simple banner dropdown item.
So what this is doing is it's setting our things to our spinner and then we're gonna do spinner set
adapter spinner array adapter.
So this is finally setting the adapter to the spinner and we're going to return that spinner so we can
use it.
All right.
Let's comment this method to make us thinner.
So now we're gonna go up.
We're going to find our movie spinner.
We're going to do this by
going under here.
Line 54 four and go find our spinner and do stuff with it.
Go ahead do spinner movie spinner.
So we're making a spinner object called Movie spinner make spinner our I.D. movie spinner movies.
So now we're calling our class we just are method.
We just made to make a movie spinner with our array movies and it's targeting movie spinner which is
this guy right here the top 1 and then we're going to go and figure out all right.
What movie did they select.
So string movie that's selected movie spinner gets selected item spinner if I can type get selected
item and then we're going to change that to a string so we can manipulate it with strings and now we're
gonna do a set on item selected listener.
So every time the person changes their mind and like I want to watch this movie I want to watch this
movie this updates our list of times and then we're going to do a nested set on item selected listener
to update our ticket price too.
So this is always listening to the spinner and saying did you change this change.
Did you change.
All right.
So let's do a movie spinner
set on item selected listener.
They're going to do a new adapter view
on selected item listener.
Go ahead and do some braces.
What's it say in.
We need to finish up the classes we need so then we're gonna do string movie equals parent get item
at position vision to string
so this is the movie selected
and then we're going to add we have to do public void on item selected adapter view.
Question mark.
Parent view.
View and
position long.
Heidi
those in the braces
so the reason this was unhappy and still is unhappy is we need to create the classes that are part of
the on items like the listener.
So one of them is on item selected so something is selected and another one is public void on nothing
selected.
So if they don't have anything selected adaptor view
adapter you
and then what this little question mark saying is saying is the type of adapter view is something.
Not sure what but it's something.
So see how down here we said string.
So we're declaring that the array doctor is going to be of type String here the type is ambiguous.
All right.
So now we are ready to do some if statements so if statement work by saying like if so for example if
red equals red
then you go to system dot out that print line true red equals red.
So this is just evaluating is this statement true or not.
If it's true it goes into here.
If it's not then it moves on.
So if you else then you would do system dot dot print line no
and then let's say you had a variable red and then you had or you had a variable color and then you
wanted to see if this string variable equals red so then if it does if you had string color equals
read then this would be true.
So the console would have true in it but if you did string color code blue then the console would print
out.
No.
So now that we've got everything setup I think there's a good point to actually stop and then we'll
cover the if statement in the next part.
All right.
So thank you very much for watching.
In the next hour we will continue talking about if statements and finishing our wonderful movie app.
Thank you for watching.
See you next time.
By
Welcome back.
As promised in this tutorial we'll be talking about our if statements finally and in order talk about
if statements we're going to have to continue our new app that calculates movie prices.
Well let's go.
So we have our app which I believe we called Movie app and then we're here.
We set up the spinners we set the listeners.
Now we gotta do our if statements.
So first thing I'm going to do if movie So is this like movie that equals movies a zero.
Do something.
So let me break this down.
What's this doing.
Well we have a string movies all right.
And then four strings instead of doing equals equals you got equals.
So there's different ways to compare things in Java.
There's equals equals.
And then dot equals so equals equals is good for things like integers so one equal equal to no one equals
equal var with var being
and var equals 1
Yes.
So this is just checking things.
So it does actually return true is going to turn a boolean false boolean is just another day type that
either is true or false.
So then that equals what that is for string specifically is a compares.
If the values of the strings are the same because if you do if you did movies equals equals movies at
index 0 of the array is going to say well does it equal it the object.
What I mean by that is.
Sure movie and movies could both be genetics but if you instantiate genetics as a different object to
instantiated differently or a different time then it's gonna have a different code or different identifier.
And the hash you will be a little bit different so then this will return false.
Even if movie with genetics and movies at index 0 with genetics because the objects are not the same.
So that's what you do dad equals because it compares the values so then we're going to go ahead and
then create the other statements so then else if so if check that this condition is true.
If it's not true it goes on.
If it is true it goes into this body.
So else if is saying if this isn't true then go into here.
So movie equals movies 1.
So this is checking if the movie that is selected in the spinner is equal to the second movie in the
array movies.
So again can pop over there if we look again this is index 0.
This is index 1 This is index 2 is index 3
here.
Same thing index 0.
Index 1.
Index 2
so then you're seeing Hey does this movie selected equal the thing at movies 1 in value.
Yes or no.
Else if movie equals movies at index to do something and then finally because we have exhausted all
of our options it's not going to be movie one is not going to be movie 2 and it's not going to be movie
3 you can just do.
Else if it's none of those things do something that's going to be for movie for
all right.
So now we're gonna have to make our movie time spinner if movie 1 is chosen we're gonna make a spinner
based on the movie Once selected which is going to be our movie one time spinner so do spinner movie
times equals make spinner the method that we made before our got I.D. that movie times
Oh I think we just called it times the idea time spinner movie one times semicolon
right and now we're going to go ahead and make the spinners for the other ones too so spinner.
Movie times make spinner ah dot I.D. the time spinner movie two times
do that for the third movie spinner movie times make spinner ah idea time spinner who movie to read
three times all right one more we can do it
let's get it done spinner movie times equals make spinner are the time spinner movie four times so could
you just copy and paste.
Yes.
Are we going to copy paste.
No because we got to build up our muscle memory.
All right.
So now cool we have the spinners made but what we need another spinner listener heck yeah because we
need to listen to whether or not the person chooses a different button or a different time to change
the ticket price.
So let's go ahead and make another let's go ahead and make another listener so we're going to a movie
time listener now.
Ms set on I keep having moved movie times set on item selected listener on item selected listener.
And then we're going to do a new adapter view on item selected listener.
And then boom it's gone.
If you hit enter at the right time it's going to automatically create classes we need.
So let's go ahead and do that for the other ones too.
Movie times on or set on items like the listener new adapter view on items like the listener two more
times movie times on set on items like the listener new adapter view on items like the listener.
And then last one movie times set on items like the listener new adapter view on items like the listener.
Perfect.
So now it's listening and saying All right what movie time are you going to pick.
Come on we don't have all day just kidding you have all day as long as you have battery on your phone
and then you're going to put more if statements in here.
So first we're going to say Hey what movie time do select slow string movie time equals adaptor view
get item at position i to string.
So this is saying all right.
Find the position.
So basically what item selected passes in is the adapter view.
So what we're looking at and then eye is the index of the item that's selected.
So for example if you chose novel group again the index is two.
So it's gonna give you two so you can find what the index selected is or what the object selected is
because you can get the item at the position of the index and then you're going to compare that to a
string all right and then you're going to text view price equals text you find view by I.D. our I.D..
Right.
So we're just linking up our our text view for price.
We're linking up the zero to our variable price.
So we can change it.
And now we're gonna put some more if statements in there is going to be if movie time equals what.
What do you want to compare it to.
That's right.
You Ray movie 1 times.
If it's the first one then do this price that set.
Text dollar sign plus it's movie movie 1.
Prices 0.
So what this is doing is it's contaminating the dollar sign with the movie one price at zero.
So that would be our eight dollars.
It would say hey you chose movie one what time you want.
Oh you want 11:00 a.m..
All right.
Well because your choice.
Equal to love am is gonna choose the first index of what's at the first index eight so your ticket price
is eight dollars.
All right.
So then you're going to do that for this guy else if movie time that movie time equals movie 1 times
at index 1 you're going to set the price to the second price price that's set.
Text dollar sign movie 1.
Prices 1.
And again.
Sure you could copy and paste but it's always good practice to just type things out because there's
less mistakes of making.
Not necessarily it's not a typo is just forgetting to change something so then you have duplicates everywhere.
It's no good.
And because there's only three different times you can just do else for the last one else.
Price that set text.
Dollar sign plus movie 1 prices and then let's say you didn't know how long it was.
Which would make sense because you would have to know how long it was to stop.
But let's say you wanted for always the last one to have the last movie price.
Then you could do a movie one prices that length.
So you see how long that array is and then subtract one.
And the reason you subtract one is because index is at zero.
So the last spot even though it's length five is not going to be an index.
I was going to be at index.
So you subtract one just a handy little thing all right.
If nothing selected to add you don't get a ticket price.
Nothing happens.
I mean you could do a set text there to like let's do that price that that text choose a time
all right.
You have to get the text view object so text view price equals text view fine view by Idi are Aidid.
Price
and now you're going to basically do the same thing for the other ones you're going to put if statements
in for whether or not they're time selected equals a correlated time on the list and see the price for
that let's go if movie time that equals
movie two times
price dot set text.
Dollar sign plus movie 2 prices 0
yet to find you have to find the price again so let's go ahead and look up their string.
Movie times equals make spinner
are these all right.
It's right there.
So we don't need to do that we just need to do the price
oh we do we have blue movie time
so we're going to do string movie time adapter view get item at position i just like we did before
and then we're gonna convert that to a string and we're gonna get the text view of the price equals
text view full fine view by Idi Arda Idi price also what this is doing is it's casting.
So it's casting whatever fine view by idea returns to a TextView
you could do this but then potentially it's not a tax you object.
So it's always good just to have that cast though and generals not go to castings because it can lead
to crashes like if you try casting a string to an integer.
Strings aren't numbers so that doesn't work.
So let's go ahead and finish this up so we have a few more to go.
Also if movie time equals movie two times at 1
price that's set.
Tax dollars sign plus movie 2 prices.
So there's a bit of discussion whether or not you should do else off on the next line or the same line.
I do it on the next line but it's really just what you choose to do and what style you prefer.
Else if movie time equals movie two times to price that set tax dollar sign plus movie two prices too
and then got to go back and add the plus sign.
So yes this is a little bit tedious but I think you should get a good idea of how if statements work
they're just conditionals.
If on this condition then do this.
If not then move on and do something else.
Prices three.
All right.
Just a few more
so find what movie time they selected string movie time adapter view get item at position i to string
text view price text view fine view by the R price.
And the reason you can't just put string here and you have to put two string is because all eight types
all classes for the most part have a two string method which convert things to a string for you because
if you just put string for this you can get kind of like jumble.
And that's what the interpreter sees as a string but it's not the specific method if that makes sense.
So then you're going to do if movie time equals movie three times come on.
There we go.
Movie three times we're almost there.
Price set.
Tax Dollar Sign plus movie 3 prices
all right.
If movie time that equals movie times 3 1 price set.
Text equals dollar sign plus movie 3.
Prices also things are case sensitive.
Make sure you don't have funny capitalization like I just had
even
take out that.
There you go.
Let's see.
Movie three times.
I'm going to.
Quick.
There you go
else changes to an elusive because two If Cinderella says OK it's not this then it does this or if it
is this then does this and then it checks if it is this and then does this so else if stops it from
like double counting and it's like if this do this and then it skips arrest it doesn't go into the elusive
price that's that tax dollar sign us movie three prices too.
So what I mean by Elsa is for example if you had a variable
let's say and var equals two.
So if you had if var equals equals to
var equals three and then you had if.
Var lesson.
If far equals equals three
far equals four.
If you add this then it would go into here.
It was like oh yes our variable is equal to two.
So now it's equal to three.
Then it goes down to here.
Continues on and then it goes oh our variable is equal to three so then it's equal to four now.
So now var equals four.
However if this is.
Else if it would say hey is a variable equal to two.
Yeah it is.
Set it to three up skips over that.
We already did that and then now it's three or not already do that.
We don't need to do that because it's.
Else if it's not equal to then look into here see people at 3.
All right.
One last one here.
We can just go ahead and put
text.
You don't even need to have this because there's only one option so you can just do price that set.
Text dollar sign plus movie for prices and then you have to find that price text view.
Go ahead and do text view price equals text view fine view by I.D. art I.D. price.
Go ahead and delete this line too because there's only one option so it doesn't need a listen to see
if they change times all right.
So now I think we're just about good.
So go ahead and run your app again.
I really recommend just connecting your USP device in it so you can see our beautiful app right on my
screen we go ahead and use the virtual machine.
It takes a while load up so be very patient and I will see you in a couple of minutes.
Though it only feel like an instant because I'm going to pause video and come back.
All right our app loaded up let's see if I can zoom in a little bit for you
so our ticket price eight dollars for genetics.
Thank you very much for watching
and the next tutorial we're gonna be talking about loops for loops while loops do while loops maybe
anything your little heart can imagine.
All right.
Thank you for watching.
See you next time.
Bye
Hello.
And welcome back everybody.
In this tutorial we will be covering while loops and in order to talk about while loop what we're gonna
do is we're going to go ahead and start new Andrea project and we're going to create kind of like a
slot machine random number generator app which uses a while loop.
So let's go ahead and create a new Android project.
I'm going to call this while creative.
Right.
Array API 15.
That's what we need the activity.
Of course and main activity so I'll tell you a little bit about while loops while this gets loaded.
So while loops are a little bit like if statement.
So it's basically saying while this statement is true do this.
So there's almost like an if statement within the while loop.
So it's going to be something along the lines of.
While this thing is true do these actions.
So for example for the slot machine it's going to be while all three numbers don't match.
Keep doing this until all numbers match and then the what's happening on the inside is is changing the
numbers with the random number generator.
And then once those numbers match we're going to use an if statement to see.
All right.
Are these matching.
And if they are.
Stop the while loop.
Okay so let's go ahead and go to activity Main and let's make our beautiful little layout
alright.
So let's go ahead and get to it.
I'm going to go ahead and close this.
I want to zoom in a little bit.
Go ahead and close it zoom in a little bit more.
So hello world.
Let's where to go down here let's go ahead and open up attributes again and change this to twenty five
from the top and then let's get rid of this I'm still not sure how to get rid of it other than just
going over here and clicking the unconstrained tool I'm sure there's a pretty easy way but I am actually
unsure at the moment what it is.
Go ahead and hit the unconstrained tool
and then let's add a top constraint.
Like I said let's make it twenty five twenty five and then let's go ahead and center this in the middle
and for the idea I wouldn't use trials.
How many times it took to run it and I don't want this symbol.
When we first run it because while no trials have been run so let's go ahead and just delete that string
and then actually let's go to our strings that X AML and add all the strings we need there's not too
many this time don't worry.
All right strings.
We're just going to add a button so String name equals but and then there's going to be go
go one go.
There you go and then it's just going to be the initial values of the numbers and they can all be the
same.
So Numb.
And then just put zero.
Go ahead back to activity main.
Let's minimizes minimizes let's zoom in a little bit.
Now let's go ahead and add three text views.
I'm just going to go ahead and drag them all out and then let's go ahead and add a button.
This one's going to be the left one it's going to be actually I want these all center.
So let's go ahead and
middle.
Let's go ahead and make this.
Let's see a hundred from the top and center this horizontally.
Go ahead and delete that.
All right.
So this one is going to be the left button.
And then go ahead make this on drag from the top.
Whoa too much.
Zero.
What's it referencing here.
Let's see.
So go ahead and unconstrained that we want to constrain to the parents or a hundred.
Let's see
that to this.
There you go.
So now it's parent to the top banner and then go ahead.
Change that to a hundred finally one hundred and then let's see which way this goes.
So this one's actually the right one
go ahead and I'm going to this unconstrained this one.
This one is going to be the left one
one hundred from the top.
Let me just click it and drag this up to the top.
Now King expected one hundred and let's make this 50 and have it parents did to the right side of this
guy right here.
It's the perfect array now for the button let's center that horizontally and let's make it 150 is a
good value from the bottom
let's go ahead and change the text view.
Text to be
at string slash NUM
and you can go ahead and make these numbers bigger.
But I think we are good
in case you don't remember.
I'll go ahead and show you how to make them bigger.
But then I want to keep them not so big you would just do.
Let's actually make the button bigger so then you could just see Android
or colon.
Text size lets go ahead and make that one hundred DP.
Whoa.
Way too big 50 DP.
Yeah.
There we go.
Let's go ahead and change this to the button text string button and then let's make it green.
So Android background zeroes the hex is 0 0 0 0 and what the f f means is 15.
So hex values it's based on binary.
So then this is 15 in binary array.
I'm going to delete this.
There you go.
Perfect.
Now we're ready to actually write our while loop
so let's go ahead and import the things we need.
So in view is already imported it looks like maybe.
No it's not so import Android view view
import Android widget button
import Android widget TextView import Android widget
we don't need anything else.
I was going to use motion of it but we don't actually need that because we're not going to be pressing
the button on and off and moving things around.
Right.
So let us initialize our variables so button but in case at a button.
Fine view by Idi.
And then this is just going to be far Idi button let's do that for our text use TextView left equals
TextView fine view by I.D..
Let me zoom in for you.
There we go.
Ah I see that
text view right.
Equals TextView.
Fine view by Ida.
Ah the idea that right now let's go the middle one TextView mid equals TextView fine view by Ida r a
d mid
OK now let's go ahead and set the text to a random value.
So in order to do that we're going to do.
Left that set.
Text
and then this is going to be math random.
So this generates a random double a random double between 0 and 1.
But we don't want a random drug in 0 1.
We want something between 1 and 10.
So we're going to multiply that by 10.
So then if it equals one it'll be 10 it if it equals zero it'll be zero if equals point five it'll equal
five.
And then let's go ahead and make this a string by concatenated an empty string with it.
And let's go ahead.
There's going to be a decimal point with this because if the doubles let's go ahead and replace that
decimal point with nothing in a B string.
Now we're going to do the same thing for the middle and the right so mid set text do do do math the
random times 10 plus an empty string and then we're going to go ahead and replace that decimal point
with an empty string.
All right.
Now we're going to go ahead do that for the right one set.
Text
random 10 plus empty string and go ahead or replace the decimal point
cool.
Now we need an on click listener.
We need to know hey the user Press the gold button.
So let's go.
So let's do.
Button set on click listener new view on click listener.
So just initializing an on click listener.
And then let's go ahead and do stuff when things are clicked.
What's it say in the ME
so then we need at override.
Public void on click.
That was the error.
We just need to have the on clicks method and then we want to do stuff
and want to spell override correctly.
So we're going to create a billion.
This is a true or false equal.
Are these numbers equal.
Well technically they are because we're starting off with zero.
Right now we're going to say no.
So we don't always just start off at zero and then that ends because I'd be really boring.
So first they're not equal.
And then and I equals zero.
This is going to be how many times do to go through this while loop it's going to start at zero and
then here's our while loop.
Are you ready.
While not equal to do stuff.
So what this is this exclamation points is not equal.
So this is going to change this to false.
So if you did something like the line along the lines of boolean
false equals true.
This will set the boolean to false whereas if you just said true.
And then you print it out with boolean is system out that print line false.
You would get true.
Whereas if you did this you'll get false array.
So right now it's false so false negative side is going to be true.
So let's go ahead.
Let's go ahead and do stuff in here.
So TextView All right
guys and gals and whatever gender you are let's copy and paste this.
I know I know I stress do not copy paste.
It's bad for you but for the sake of simplicity let's just go ahead to copy and paste it.
All right.
Don't tell anyone.
OK.
And then let's go ahead and add our numbers.
So this one we're going to assign the left num.
And the write downs and all that good stuff to a variable so that we can compare the variables later
on.
Whereas if you just did this you don't actually get to say what the random number was.
So we're going to go and math that random times 10.
So what this is doing is it's casting it to an end.
We'll want to do that up here too.
We're going to want to cast this to an end.
So that way we don't have a ton of numbers
like things after the decimal point.
We don't want that.
No no
and then we can go ahead and make this just an end and right now equals math that random times 10.
Let's go ahead and do the middle one and Millennium Goals and math.
The random times 10.
So now it's going to generate a random number one in ten.
That's a whole number.
Let's go ahead and set the text to this new number.
So left set.
Text
left num empty string and replace that decimal point
good do it for the right number.
Text left or right.
Num plus empty string replace.
There you go empty string.
And finally the right one or the middle one mid set.
Tax minute number plus
replace replace that decimal point if you want to keep the decimal point in.
That's totally your style and that's all right.
So now it's added the number.
So we want to see hey that's 1 trial.
So add plus plus to I.
Well this is basically the thing is I equals I plus 1.
This is equivalent so I'll go ahead and move that up here
and now we're going to do an if statement.
If these numbers are all equal to each other do something.
So if left equals equals mid and right equals equals mid equal equals not equal or you can do equals
equals true.
So this is checking is if this statement is true then do this and these two things these are.
And so you can't right.
And you have to write and this would be or
and then after a while loops done we want to tell the user Hey how many trials is this take.
So let's go ahead and find the textview trials
fine view by the Arda IBD trials.
Let's go ahead and set the text of that to how many trials it took took space plus I plus times to get
matching numbers.
All right welcome back everybody.
Our final loaded and as you can see it starts at 0 0 0.
And then we have our go button.
So to dot.
That is our while loops.
Thank you very much for watching.
And the next tutorial we'll be covering for 4 4 4 4 loops.
Another important thing.
And we'll also be going over arrays because arrays and for loops go hand in hand.