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.11.
Task one is to create a function called Celsius the Fahrenheit.
So we start with the usual public static.
The function name is Celsius, the Fahrenheit.
The function will receive an array of values in Celsius.
So it's going to receive an array of type double.
That store is Celsius temperatures.
And upon receiving an array of Celsius temperatures, it's going to return an array of Fahrenheit temperatures.
So it will also return an array of type double.
All right.
And inside the function, we need to create a double array called Fahrenheit.
So double.
Fahrenheit, because I'm assuming we have to convert all of the Celsius values in here to Fahrenheit.
Then this array is going to have the same length as the array that gets passed in.
Oh, right.
The function copies every value from the Celsius parameter to the Fahrenheit array.
So what we can do is run through the length of our Fahrenheit array.
The loop will keep running so long is I is smaller than Fahrenheit length.
I a plus plus.
And for every single element that we index, we set it equal to a Celsius temperature.
All right.
But we don't want to just copy over the Celsius value.
We want to convert it first.
So in bullet point three, we need to use the formula below to convert every value to Fahrenheit.
So using this formula, we can convert the Celsius value to Fahrenheit before putting it in our Fahrenheit
array.
So Celsius I divided by five.
Times nine.
Plus 32.
So first we convert whatever Celsius value that we're currently looping through to Fahrenheit, and
then we store the result as an element in the Fahrenheit array.
And after we've converted every single Celsius temperature to Fahrenheit, we can return the result.
So we can return the resulting Fahrenheit array, and we are finished with our function.
Task two The starter code contains an array of Celsius values.
It definitely does use your function to return an array of Fahrenheit values from the Celsius array,
so we can call Celsius the Fahrenheit.
Passing in this array, which will convert every single Celsius value to Fahrenheit.
It will return an array of doubles, which we store here.
All right.
And now we can print the contents of Fahrenheit using arrays to string.
So here, we'll say.
System.out.println.
Arrays.
Two string.
Fahrenheit.
I'm not going to visualize the runtime just yet.
Let us just say Java Sea Weather Dot.
Java.
Java Weather.
Compare what we have to what's on.
Learn the part.
54.5 68.
All right.
Looks like we're doing good.
And now task three tells us to remove our print statement.
Instead, create a function that receives a double array and a string.
All right.
And we'll delete that.
Public static.
The function is void.
It's going to be called, I believe, print temperatures.
It will receive a double array called.
Temp.
So this could be an array of Celsius temperatures, an array of Fahrenheit temperatures, which means
we need another parameter, temp type that tells us what is the array that is currently being passed
in.
The function prints the temperature type using print, not print line.
Okay, we can just copy this over.
Simple enough.
The function uses a four loop to print.
The temperature is on the same line.
So you already know we need to create a for loop that runs through the length of whatever array gets
passed in.
This loop will keep running so long as AI is smaller than temp length I plus plus, and during each
run we need to print the temperature that's currently being looped through and we can use this print
statement.
To print all of the temperatures that we iterate through on the same line.
OC.
And the function prints a new line after the loop.
Perfect.
All right.
And now after we finish task four, then we will visualize the runtime so that we can do it all in one
go.
Print temperatures.
And we're good.
I'll put one breakpoint here, one here and one here.
And I guess we don't need this anymore.
Okay, Let us clean up a little.
Close.
This terminal that I'm not using will minimize the following.
All right.
I'm going to step inside of Celsius.
The Fahrenheit.
So here we pass in an array of seven temperatures.
Our parameter receives that array and stores it.
Here we're setting double Fahrenheit, equal to a new double array that shares the same length as our
Celsius parameter.
So this will also have seven elements, but by default they're all going to start off as zero.
So this four loop is going to run through the length of our Fahrenheit array.
I start at zero.
The Celsius temperature index zero is 12.5, 12.5 divided by five is 2.5 times nine is 22.5 and plus
32 is 54.5.
Which means we're setting the Fahrenheit element at Index zero equal to 54.5, the converted Fahrenheit
temperature.
And we keep doing that throughout.
So now the index equals one.
The Celsius temperature at index one is 14.5, 14.5.
Convert it to Fahrenheit is going to equal.
58.1.
Now I equals to the element that index two is 1717 convert it to Fahrenheit is going to equal.
62.6 and this will keep on going until the loop runs to completion.
And just like that, we've converted every single Celsius value to Fahrenheit and stored it in another
array.
That same array ends up being the return value.
The function call Celsius Fahrenheit retains the array that was returned.
Beautiful.
So now we've got two arrays.
One that store is at cell C temperatures, one that stores Fahrenheit temperatures.
First, we're passing the Celsius array into print temperatures.
All this is doing is printing the temperature type, which in this case we passed in Celsius.
Beautiful.
And this for loop is just going to run through the length of the array that was passed in and print
each element on the same line.
And after the loop ran to completion, it prints a new line.
And the same thing is going to happen for the Fahrenheit array.
So I'm just going to step over this and we are done.