1 00:00:00,180 --> 00:00:03,870 Before we start, did you try solving the workbook yourself? 2 00:00:03,900 --> 00:00:09,660 If not, please click the link in the resources folder and follow the instructions for this workbook. 3 00:00:10,900 --> 00:00:12,730 Welcome to Workbook 6.5. 4 00:00:12,760 --> 00:00:19,480 Task one is to make a function that returns a random number between 0 to 49999. 5 00:00:19,600 --> 00:00:22,390 All right, say less public static. 6 00:00:22,390 --> 00:00:27,790 The return type is an integer and the function name is random number. 7 00:00:28,670 --> 00:00:32,720 And now you'll remember the built in function math random. 8 00:00:33,870 --> 00:00:40,610 What it does is it returns a random decimal that is greater than or equal to zero and less than one. 9 00:00:40,620 --> 00:00:49,350 So the value that we get back from that random can be anything between the range of 0 to 0.9999999. 10 00:00:50,490 --> 00:00:58,200 And whatever this ends up returning, if we multiply the result by 50,000, then the minimum times 50,000 11 00:00:58,200 --> 00:00:59,560 would still be zero. 12 00:00:59,580 --> 00:01:07,470 But if math thought random ends up returning the maximum possible value of 0.999999, that multiplied 13 00:01:07,470 --> 00:01:13,620 by 50,000 will return 49,999.999999. 14 00:01:13,650 --> 00:01:14,880 All right. 15 00:01:15,480 --> 00:01:20,210 So we'll say here double decimal is equal to the following. 16 00:01:20,220 --> 00:01:25,590 And now if we return that decimal, but typecast it to end. 17 00:01:25,980 --> 00:01:31,860 Given that decimal can be any value within this range, zero typecast to int would still be zero. 18 00:01:31,860 --> 00:01:37,350 But if decimal happens to be the maximum possible value that can be returned by this code, then this 19 00:01:37,350 --> 00:01:41,580 number typecasting it to int would just be cutting off all the decimals. 20 00:01:41,580 --> 00:01:48,630 And so the value that we return can be anything between zero and 49,999. 21 00:01:48,810 --> 00:01:50,640 All right, simple enough. 22 00:01:50,730 --> 00:01:54,000 We covered this very extensively in the function section. 23 00:01:54,970 --> 00:01:57,520 Now we create an array that stores ten numbers. 24 00:01:57,520 --> 00:01:59,800 So here we will create an array. 25 00:02:00,900 --> 00:02:08,850 That can store elements of type int We'll say int scores is equal to an array that is going to store 26 00:02:08,880 --> 00:02:10,020 ten values. 27 00:02:11,300 --> 00:02:17,480 So I can just call a random number ten times copying and pasting this nine more times. 28 00:02:23,540 --> 00:02:24,290 All right. 29 00:02:25,530 --> 00:02:30,210 Each element equals whatever random number gets returned from this function. 30 00:02:32,560 --> 00:02:37,690 Task number three is to use a four loop to print every single element on the same line. 31 00:02:37,720 --> 00:02:43,510 This should be system print, not system dot dot, print line. 32 00:02:45,690 --> 00:02:47,580 Here, we can put a colon. 33 00:02:48,710 --> 00:02:50,270 Followed by a space. 34 00:02:50,600 --> 00:02:52,610 And now we can create a for loop. 35 00:02:53,290 --> 00:02:55,690 That starts with I equaling zero. 36 00:02:55,720 --> 00:03:01,390 The four loop is going to keep running so long as I is smaller than the length of the scores array. 37 00:03:01,750 --> 00:03:05,110 And every time the loop runs, we increase EI by one. 38 00:03:06,380 --> 00:03:11,660 When the loop is running, we can use AI to index every single element in the array. 39 00:03:12,930 --> 00:03:18,990 And during each run, we're going to print that element followed by some empty space. 40 00:03:31,040 --> 00:03:32,720 So here I've got ten elements. 41 00:03:32,720 --> 00:03:35,150 The length of my score's array is ten. 42 00:03:35,180 --> 00:03:37,850 The counter I is going to start at zero. 43 00:03:38,120 --> 00:03:43,160 The element at index zero for me equals 46,638. 44 00:03:43,160 --> 00:03:45,620 So that ends up printing on the same line. 45 00:03:48,730 --> 00:03:50,080 Stepping over again. 46 00:03:50,080 --> 00:03:52,150 Now I equals one. 47 00:03:52,180 --> 00:03:59,770 The element that index one for me equals roughly 36,000, and the elements will keep printing in succession 48 00:03:59,770 --> 00:04:02,650 until we eventually reach an index of nine. 49 00:04:03,220 --> 00:04:07,500 Once we go back, I a plus plus is going to increase the counter value by one. 50 00:04:07,510 --> 00:04:10,270 Ten is not smaller than the length of the array. 51 00:04:10,270 --> 00:04:10,780 Ten. 52 00:04:10,780 --> 00:04:14,680 So our four loop is going to break and we are good. 53 00:04:15,280 --> 00:04:20,680 We were able to achieve a similar result to what we have in task number three, and now task number 54 00:04:20,680 --> 00:04:24,040 four tells us that our code contains a high score variable. 55 00:04:24,070 --> 00:04:25,850 Indeed it does. 56 00:04:25,870 --> 00:04:27,490 And what do we have to do with it? 57 00:04:27,520 --> 00:04:33,010 We have to use a for loop to update the variable with the highest score in the array and then print 58 00:04:33,010 --> 00:04:33,400 it. 59 00:04:33,430 --> 00:04:34,780 Here, it gives us a hint. 60 00:04:34,810 --> 00:04:41,350 Your loop needs a condition that updates the variable whenever an element is higher, so our high score 61 00:04:41,380 --> 00:04:42,520 starts at zero. 62 00:04:42,550 --> 00:04:47,560 What I'll do is for every element in the scores array that we run through. 63 00:04:47,590 --> 00:04:50,470 We're going to check if that score. 64 00:04:52,120 --> 00:04:53,560 Is greater than. 65 00:04:54,410 --> 00:04:55,880 Our current high score. 66 00:04:59,480 --> 00:05:03,770 If that happens to be the case, then we set a high score equal to that element. 67 00:05:06,460 --> 00:05:08,390 So why is this going to work? 68 00:05:08,410 --> 00:05:09,670 Let's find out. 69 00:05:15,740 --> 00:05:16,010 All right. 70 00:05:16,010 --> 00:05:18,830 So right now, high score equals zero. 71 00:05:21,070 --> 00:05:23,230 The element of index is zero. 72 00:05:24,340 --> 00:05:28,180 29,034 is higher than our current high score. 73 00:05:28,180 --> 00:05:29,950 So this evaluates to true. 74 00:05:30,220 --> 00:05:36,310 And now we update our high score with the highest score that we've encountered so far, 29,000. 75 00:05:37,650 --> 00:05:38,370 Beautiful. 76 00:05:39,930 --> 00:05:41,730 Now I equals one. 77 00:05:41,760 --> 00:05:49,950 The score at index one 1137 is not greater than our current high score, so we don't update our high 78 00:05:49,950 --> 00:05:50,430 score. 79 00:05:50,430 --> 00:05:54,150 And I hope you can now see the logic behind this condition. 80 00:05:54,240 --> 00:06:00,270 Basically, it acts to only update the high score if it encounters a new high score that ends up being 81 00:06:00,270 --> 00:06:01,170 greater than it. 82 00:06:01,200 --> 00:06:07,170 So when I equals to 44,000 is greater than our current high score. 83 00:06:07,200 --> 00:06:12,840 This condition evaluates to true so, which means we have to update our high score with the highest 84 00:06:12,840 --> 00:06:15,990 score that we've encountered so far, 44,000. 85 00:06:16,380 --> 00:06:22,480 And our if statement is going to continue being false until we reach I equals eight. 86 00:06:22,500 --> 00:06:23,970 So let's just skip through it. 87 00:06:26,630 --> 00:06:29,150 Six, seven, eight. 88 00:06:31,140 --> 00:06:36,720 So the element that index eight equals 48,000, that is greater than our current high score. 89 00:06:36,720 --> 00:06:42,480 So this condition evaluates to true, which means we update our high score with the highest score that 90 00:06:42,480 --> 00:06:43,980 we've encountered so far. 91 00:06:47,700 --> 00:06:49,770 All right, let's check the last score in the array. 92 00:06:49,770 --> 00:06:56,310 Where I equals nine 32,000 is not greater than the highest score we've encountered so far of 48,000. 93 00:06:57,000 --> 00:07:01,170 So this evaluates the false I A-plus plus increases. 94 00:07:01,170 --> 00:07:02,220 9 to 10. 95 00:07:02,220 --> 00:07:04,680 Ten is not smaller than the length of the array. 96 00:07:04,680 --> 00:07:07,440 Ten So our for loop runs to completion. 97 00:07:07,770 --> 00:07:11,820 The highest score from the scores array is 48,000. 98 00:07:11,820 --> 00:07:14,670 So we give that man a cookie. 99 00:07:17,950 --> 00:07:21,100 And our output doesn't match what we have on learn the part. 100 00:07:21,100 --> 00:07:24,130 We need to print all of the scores and the high score. 101 00:07:24,580 --> 00:07:30,850 So I need to restore my previous logic, which was to print all of the scores no matter what. 102 00:07:32,050 --> 00:07:33,580 We'll say Java Sea. 103 00:07:34,320 --> 00:07:35,700 I scored Java. 104 00:07:36,390 --> 00:07:37,710 Java High score. 105 00:07:39,700 --> 00:07:43,360 Upon rerunning my code, we get a fresh new batch of high scores. 106 00:07:43,360 --> 00:07:49,000 I can clearly see with my own eyes that this is the highest score and our code was able to come up with 107 00:07:49,000 --> 00:07:50,290 the same result. 108 00:07:50,920 --> 00:07:51,370 All right. 109 00:07:51,370 --> 00:07:53,710 I hope you enjoyed this breakpoint session.