1 00:00:00,700 --> 00:00:04,390 Before we start, did you try solving the workbook yourself? 2 00:00:04,420 --> 00:00:10,180 If not, please click the link in the resources folder and follow the instructions for this workbook. 3 00:00:10,840 --> 00:00:13,260 Welcome to Workbook 6.11. 4 00:00:13,270 --> 00:00:16,470 Task one is to create a function called Celsius the Fahrenheit. 5 00:00:16,480 --> 00:00:19,420 So we start with the usual public static. 6 00:00:19,450 --> 00:00:22,120 The function name is Celsius, the Fahrenheit. 7 00:00:23,030 --> 00:00:26,180 The function will receive an array of values in Celsius. 8 00:00:26,180 --> 00:00:29,720 So it's going to receive an array of type double. 9 00:00:29,720 --> 00:00:32,360 That store is Celsius temperatures. 10 00:00:34,340 --> 00:00:40,820 And upon receiving an array of Celsius temperatures, it's going to return an array of Fahrenheit temperatures. 11 00:00:40,820 --> 00:00:44,690 So it will also return an array of type double. 12 00:00:45,710 --> 00:00:46,850 All right. 13 00:00:47,300 --> 00:00:51,650 And inside the function, we need to create a double array called Fahrenheit. 14 00:00:51,650 --> 00:00:52,970 So double. 15 00:00:53,820 --> 00:00:59,970 Fahrenheit, because I'm assuming we have to convert all of the Celsius values in here to Fahrenheit. 16 00:00:59,970 --> 00:01:06,030 Then this array is going to have the same length as the array that gets passed in. 17 00:01:08,610 --> 00:01:09,650 Oh, right. 18 00:01:09,660 --> 00:01:14,080 The function copies every value from the Celsius parameter to the Fahrenheit array. 19 00:01:14,100 --> 00:01:18,450 So what we can do is run through the length of our Fahrenheit array. 20 00:01:20,150 --> 00:01:24,470 The loop will keep running so long is I is smaller than Fahrenheit length. 21 00:01:24,620 --> 00:01:25,910 I a plus plus. 22 00:01:27,650 --> 00:01:32,630 And for every single element that we index, we set it equal to a Celsius temperature. 23 00:01:34,340 --> 00:01:35,180 All right. 24 00:01:35,180 --> 00:01:37,990 But we don't want to just copy over the Celsius value. 25 00:01:38,000 --> 00:01:39,610 We want to convert it first. 26 00:01:39,620 --> 00:01:46,790 So in bullet point three, we need to use the formula below to convert every value to Fahrenheit. 27 00:01:46,880 --> 00:01:54,590 So using this formula, we can convert the Celsius value to Fahrenheit before putting it in our Fahrenheit 28 00:01:54,590 --> 00:01:55,260 array. 29 00:01:55,280 --> 00:01:59,060 So Celsius I divided by five. 30 00:02:01,040 --> 00:02:02,300 Times nine. 31 00:02:04,580 --> 00:02:05,840 Plus 32. 32 00:02:08,759 --> 00:02:14,070 So first we convert whatever Celsius value that we're currently looping through to Fahrenheit, and 33 00:02:14,070 --> 00:02:17,610 then we store the result as an element in the Fahrenheit array. 34 00:02:18,150 --> 00:02:23,790 And after we've converted every single Celsius temperature to Fahrenheit, we can return the result. 35 00:02:23,790 --> 00:02:30,630 So we can return the resulting Fahrenheit array, and we are finished with our function. 36 00:02:31,260 --> 00:02:34,780 Task two The starter code contains an array of Celsius values. 37 00:02:34,800 --> 00:02:41,220 It definitely does use your function to return an array of Fahrenheit values from the Celsius array, 38 00:02:41,220 --> 00:02:44,610 so we can call Celsius the Fahrenheit. 39 00:02:46,250 --> 00:02:51,740 Passing in this array, which will convert every single Celsius value to Fahrenheit. 40 00:02:51,740 --> 00:02:55,460 It will return an array of doubles, which we store here. 41 00:02:55,760 --> 00:02:56,870 All right. 42 00:02:57,150 --> 00:03:01,760 And now we can print the contents of Fahrenheit using arrays to string. 43 00:03:02,330 --> 00:03:03,680 So here, we'll say. 44 00:03:04,530 --> 00:03:06,900 System.out.println. 45 00:03:06,930 --> 00:03:08,010 Arrays. 46 00:03:08,700 --> 00:03:09,870 Two string. 47 00:03:10,350 --> 00:03:11,550 Fahrenheit. 48 00:03:12,840 --> 00:03:15,270 I'm not going to visualize the runtime just yet. 49 00:03:15,300 --> 00:03:18,960 Let us just say Java Sea Weather Dot. 50 00:03:18,960 --> 00:03:19,680 Java. 51 00:03:19,710 --> 00:03:21,030 Java Weather. 52 00:03:22,640 --> 00:03:24,010 Compare what we have to what's on. 53 00:03:24,020 --> 00:03:24,680 Learn the part. 54 00:03:24,680 --> 00:03:27,020 54.5 68. 55 00:03:27,770 --> 00:03:28,600 All right. 56 00:03:28,610 --> 00:03:30,170 Looks like we're doing good. 57 00:03:30,290 --> 00:03:33,290 And now task three tells us to remove our print statement. 58 00:03:33,290 --> 00:03:38,240 Instead, create a function that receives a double array and a string. 59 00:03:38,630 --> 00:03:39,710 All right. 60 00:03:40,260 --> 00:03:41,750 And we'll delete that. 61 00:03:43,180 --> 00:03:45,120 Public static. 62 00:03:45,130 --> 00:03:46,800 The function is void. 63 00:03:46,810 --> 00:03:50,110 It's going to be called, I believe, print temperatures. 64 00:03:50,230 --> 00:03:54,850 It will receive a double array called. 65 00:03:55,690 --> 00:03:56,470 Temp. 66 00:03:57,890 --> 00:04:03,170 So this could be an array of Celsius temperatures, an array of Fahrenheit temperatures, which means 67 00:04:03,170 --> 00:04:08,660 we need another parameter, temp type that tells us what is the array that is currently being passed 68 00:04:08,660 --> 00:04:09,050 in. 69 00:04:15,850 --> 00:04:19,269 The function prints the temperature type using print, not print line. 70 00:04:19,339 --> 00:04:21,220 Okay, we can just copy this over. 71 00:04:22,560 --> 00:04:23,480 Simple enough. 72 00:04:23,510 --> 00:04:25,280 The function uses a four loop to print. 73 00:04:25,280 --> 00:04:27,770 The temperature is on the same line. 74 00:04:29,840 --> 00:04:34,730 So you already know we need to create a for loop that runs through the length of whatever array gets 75 00:04:34,730 --> 00:04:35,360 passed in. 76 00:04:35,390 --> 00:04:42,890 This loop will keep running so long as AI is smaller than temp length I plus plus, and during each 77 00:04:42,890 --> 00:04:48,650 run we need to print the temperature that's currently being looped through and we can use this print 78 00:04:48,650 --> 00:04:49,490 statement. 79 00:04:50,380 --> 00:04:54,430 To print all of the temperatures that we iterate through on the same line. 80 00:04:54,820 --> 00:04:55,530 OC. 81 00:04:57,500 --> 00:05:00,690 And the function prints a new line after the loop. 82 00:05:00,710 --> 00:05:01,700 Perfect. 83 00:05:08,310 --> 00:05:08,910 All right. 84 00:05:08,910 --> 00:05:14,700 And now after we finish task four, then we will visualize the runtime so that we can do it all in one 85 00:05:14,700 --> 00:05:15,330 go. 86 00:05:16,180 --> 00:05:17,800 Print temperatures. 87 00:05:19,060 --> 00:05:19,870 And we're good. 88 00:05:20,860 --> 00:05:24,760 I'll put one breakpoint here, one here and one here. 89 00:05:27,230 --> 00:05:28,840 And I guess we don't need this anymore. 90 00:05:32,750 --> 00:05:35,090 Okay, Let us clean up a little. 91 00:05:35,630 --> 00:05:36,260 Close. 92 00:05:36,260 --> 00:05:41,000 This terminal that I'm not using will minimize the following. 93 00:05:42,260 --> 00:05:43,310 All right. 94 00:05:43,700 --> 00:05:45,580 I'm going to step inside of Celsius. 95 00:05:45,590 --> 00:05:46,490 The Fahrenheit. 96 00:05:47,460 --> 00:05:50,580 So here we pass in an array of seven temperatures. 97 00:05:51,750 --> 00:05:55,410 Our parameter receives that array and stores it. 98 00:05:55,440 --> 00:06:02,400 Here we're setting double Fahrenheit, equal to a new double array that shares the same length as our 99 00:06:02,400 --> 00:06:03,660 Celsius parameter. 100 00:06:04,290 --> 00:06:09,380 So this will also have seven elements, but by default they're all going to start off as zero. 101 00:06:09,390 --> 00:06:13,590 So this four loop is going to run through the length of our Fahrenheit array. 102 00:06:16,050 --> 00:06:17,490 I start at zero. 103 00:06:17,760 --> 00:06:29,490 The Celsius temperature index zero is 12.5, 12.5 divided by five is 2.5 times nine is 22.5 and plus 104 00:06:29,490 --> 00:06:32,460 32 is 54.5. 105 00:06:34,070 --> 00:06:41,360 Which means we're setting the Fahrenheit element at Index zero equal to 54.5, the converted Fahrenheit 106 00:06:41,390 --> 00:06:42,080 temperature. 107 00:06:42,080 --> 00:06:44,210 And we keep doing that throughout. 108 00:06:45,210 --> 00:06:47,170 So now the index equals one. 109 00:06:47,190 --> 00:06:51,750 The Celsius temperature at index one is 14.5, 14.5. 110 00:06:51,750 --> 00:06:54,630 Convert it to Fahrenheit is going to equal. 111 00:06:55,380 --> 00:06:57,000 58.1. 112 00:06:58,030 --> 00:07:05,290 Now I equals to the element that index two is 1717 convert it to Fahrenheit is going to equal. 113 00:07:06,550 --> 00:07:11,560 62.6 and this will keep on going until the loop runs to completion. 114 00:07:17,730 --> 00:07:23,280 And just like that, we've converted every single Celsius value to Fahrenheit and stored it in another 115 00:07:23,280 --> 00:07:23,940 array. 116 00:07:23,970 --> 00:07:26,940 That same array ends up being the return value. 117 00:07:29,250 --> 00:07:34,110 The function call Celsius Fahrenheit retains the array that was returned. 118 00:07:36,650 --> 00:07:37,150 Beautiful. 119 00:07:37,160 --> 00:07:39,260 So now we've got two arrays. 120 00:07:39,260 --> 00:07:44,840 One that store is at cell C temperatures, one that stores Fahrenheit temperatures. 121 00:07:44,840 --> 00:07:48,650 First, we're passing the Celsius array into print temperatures. 122 00:07:49,160 --> 00:07:54,950 All this is doing is printing the temperature type, which in this case we passed in Celsius. 123 00:07:56,050 --> 00:07:56,880 Beautiful. 124 00:07:56,890 --> 00:08:02,080 And this for loop is just going to run through the length of the array that was passed in and print 125 00:08:02,080 --> 00:08:04,150 each element on the same line. 126 00:08:08,840 --> 00:08:11,700 And after the loop ran to completion, it prints a new line. 127 00:08:11,720 --> 00:08:14,830 And the same thing is going to happen for the Fahrenheit array. 128 00:08:14,840 --> 00:08:18,800 So I'm just going to step over this and we are done.