Before we start, did you try solving the workbook yourself?
If not, please click the link in the resources folder and follow the instructions for this workbook.
Welcome to Workbook 6.5.
Task one is to make a function that returns a random number between 0 to 49999.
All right, say less public static.
The return type is an integer and the function name is random number.
And now you'll remember the built in function math random.
What it does is it returns a random decimal that is greater than or equal to zero and less than one.
So the value that we get back from that random can be anything between the range of 0 to 0.9999999.
And whatever this ends up returning, if we multiply the result by 50,000, then the minimum times 50,000
would still be zero.
But if math thought random ends up returning the maximum possible value of 0.999999, that multiplied
by 50,000 will return 49,999.999999.
All right.
So we'll say here double decimal is equal to the following.
And now if we return that decimal, but typecast it to end.
Given that decimal can be any value within this range, zero typecast to int would still be zero.
But if decimal happens to be the maximum possible value that can be returned by this code, then this
number typecasting it to int would just be cutting off all the decimals.
And so the value that we return can be anything between zero and 49,999.
All right, simple enough.
We covered this very extensively in the function section.
Now we create an array that stores ten numbers.
So here we will create an array.
That can store elements of type int We'll say int scores is equal to an array that is going to store
ten values.
So I can just call a random number ten times copying and pasting this nine more times.
All right.
Each element equals whatever random number gets returned from this function.
Task number three is to use a four loop to print every single element on the same line.
This should be system print, not system dot dot, print line.
Here, we can put a colon.
Followed by a space.
And now we can create a for loop.
That starts with I equaling zero.
The four loop is going to keep running so long as I is smaller than the length of the scores array.
And every time the loop runs, we increase EI by one.
When the loop is running, we can use AI to index every single element in the array.
And during each run, we're going to print that element followed by some empty space.
So here I've got ten elements.
The length of my score's array is ten.
The counter I is going to start at zero.
The element at index zero for me equals 46,638.
So that ends up printing on the same line.
Stepping over again.
Now I equals one.
The element that index one for me equals roughly 36,000, and the elements will keep printing in succession
until we eventually reach an index of nine.
Once we go back, I a plus plus is going to increase the counter value by one.
Ten is not smaller than the length of the array.
Ten.
So our four loop is going to break and we are good.
We were able to achieve a similar result to what we have in task number three, and now task number
four tells us that our code contains a high score variable.
Indeed it does.
And what do we have to do with it?
We have to use a for loop to update the variable with the highest score in the array and then print
it.
Here, it gives us a hint.
Your loop needs a condition that updates the variable whenever an element is higher, so our high score
starts at zero.
What I'll do is for every element in the scores array that we run through.
We're going to check if that score.
Is greater than.
Our current high score.
If that happens to be the case, then we set a high score equal to that element.
So why is this going to work?
Let's find out.
All right.
So right now, high score equals zero.
The element of index is zero.
29,034 is higher than our current high score.
So this evaluates to true.
And now we update our high score with the highest score that we've encountered so far, 29,000.
Beautiful.
Now I equals one.
The score at index one 1137 is not greater than our current high score, so we don't update our high
score.
And I hope you can now see the logic behind this condition.
Basically, it acts to only update the high score if it encounters a new high score that ends up being
greater than it.
So when I equals to 44,000 is greater than our current high score.
This condition evaluates to true so, which means we have to update our high score with the highest
score that we've encountered so far, 44,000.
And our if statement is going to continue being false until we reach I equals eight.
So let's just skip through it.
Six, seven, eight.
So the element that index eight equals 48,000, that is greater than our current high score.
So this condition evaluates to true, which means we update our high score with the highest score that
we've encountered so far.
All right, let's check the last score in the array.
Where I equals nine 32,000 is not greater than the highest score we've encountered so far of 48,000.
So this evaluates the false I A-plus plus increases.
9 to 10.
Ten is not smaller than the length of the array.
Ten So our for loop runs to completion.
The highest score from the scores array is 48,000.
So we give that man a cookie.
And our output doesn't match what we have on learn the part.
We need to print all of the scores and the high score.
So I need to restore my previous logic, which was to print all of the scores no matter what.
We'll say Java Sea.
I scored Java.
Java High score.
Upon rerunning my code, we get a fresh new batch of high scores.
I can clearly see with my own eyes that this is the highest score and our code was able to come up with
the same result.
All right.
I hope you enjoyed this breakpoint session.