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 Work Book 4.3.
Let us jump right into it.
Task one is to make a function called Fahrenheit two Celsius that expects to receive a double value.
And then from the value that it receives, it's going to return a Celsius result.
So I will start by defining my function over here.
Public static.
The functions return type needs to be double because we expect to return a double value and the function
name is Fahrenheit to Celsius.
The function must define one parameter called Fahrenheit because ultimately we're going to be receiving
a Fahrenheit temperature and then converting it to Celsius.
How will we do that?
We're going to do that using the formula that was provided.
So we'll say return.
Copying the following Replace F with the Fahrenheit temperature that gets passed in.
And that is all.
When this function gets called, it will receive a Fahrenheit value, and the result that we return
will be the conversion from Fahrenheit to Celsius using the following formula.
Let's call this function three separate times.
One four, the temperature in Fahrenheit at midnight.
One for the temperature in Fahrenheit and during the evening.
And one during noon.
All right.
Let us step inside the function every time we call it first, we're calling it passing in a value of
77 degrees Fahrenheit.
If you step inside the function, the Fahrenheit parameter stores the value that was passed in.
And here, using the value that was passed in, we can calculate what the corresponding Celsius temperature
will be and return the final result.
The function call retains the return value of 25 degrees.
But we're not really doing anything with it.
So moving on to the next breakpoint here, we're calling the function again, this time passing in a
temperature of 61 degrees Fahrenheit.
The parameter stores, the value that was passed in.
And here, using the Fahrenheit temperature, we can calculate the corresponding Celsius temperature
and return it.
And the function call holds on to the return value of 16.11 degrees Celsius.
We could store it in a variable, but right now let us just move on to task number two and stop the
runtime.
Okay, So we've successfully defined our Fahrenheit to Celsius function.
That's it for task one.
Task number two is to make a function that prints all of the temperatures.
It's going to need to receive a Fahrenheit temperature and it will not return anything.
So right there, we know that it's going to be void, public static void.
The function will not return anything because all it will do is print a bunch of temperatures.
It's going to receive the Fahrenheit temperature.
And upon receiving a Fahrenheit value, the first thing that we're going to do is print it.
So we'll say the value in Fahrenheit equals.
Whatever value that was passed in.
And the second print statement is going to calculate the corresponding Celsius temperature.
So here what we can do is call Fahrenheit to Celsius, and then at the end we will print a new line.
So what this line is doing is printing a double value.
And what this line is doing is printing whatever double value gets returned when we call Fahrenheit
to Celsius.
All right.
Now, what I'm going to do is call print temperatures.
And now we can visualize the runtime.
So here we're calling print temperatures, passing in a temperature of 77 degrees Fahrenheit.
Here we're just printing the Fahrenheit value that was passed in.
And here we're calling to functions.
We're calling the print line function, and we're calling Fahrenheit to Celsius.
So the first step into is going to take you to some random class.
We'll step out of it.
And then the next step into is going to step inside of our second function call Fahrenheit to Celsius.
And based on the value that was passed in, we're calculating the corresponding Celsius temperature
and we're returning the final result.
The function call basically equals the return value.
It equals the final result of 25 degrees Celsius, which we are then printing.
All right.
Continuing to the next breakpoint here.
We're calling the function a second time.
This time we're passing in a value of 61 degrees Fahrenheit.
Here we're just printing the value that was passed in.
But then here we're printing whatever, Fahrenheit to Celsius returns.
In this case, it's going to return 16.21 degrees Celsius.
Now, I'm just going to continue to the next breakpoint, keep pressing, continue this time calling
print temperatures for a Fahrenheit value of 55 degrees.
In this case, it's going to rent a Celsius value of 12.7 degrees.
That is all.
I hope you enjoyed this breakpoint session.