Workbook 4.3
From the Java Bootcamp Resources
, launch the Workbook 4.3
folder.
Weather
These are the temperatures for noon, evening, and midnight in Fahrenheit. We need to convert them to Celsius.
public static void main(String[] args) {
double noon = 77; //temperature in fahrenheit.
double evening = 61; //temperature in fahrenheit
double midnight = 55; //temperature in fahrenheit
}
Task 1 - Write a function
You will write a function that converts Fahrenheit to Celcius.
- Function name.
/** * Function name: fahrenheitToCelsius. <----- * @param fahrenheit (double) * @return celsius (double) * * Inside the function: * 1. return the celsius temperature. C = (F - 32) * 5/9 */
- Parameter.
/** * Function name: fahrenheitToCelsius. * @param fahrenheit (double) <------ * @return celsius (double) * * Inside the function: * 1. return the celsius temperature. C = (F - 32) * 5/9 */
- Return value.
/** * Function name: fahrenheitToCelsius. * @param fahrenheit (double) * @return celsius (double) <------ * * Inside the function: * 1. return the celsius temperature. C = (F - 32) * 5/9 */
Inside the function, use the equation to go from Fahrenheit to Celsius. Then, return the result.
/** * Function name: fahrenheitToCelsius. * @param fahrenheit (double) * @return celsius (double) * * Inside the function: * 1. return the celsius temperature. C = (F - 32) * 5/9 <---- */
Task 2 - Write another function
- Function name.
/** * Name: printTemperatures <----- * @param fahrenheit (double) * * Inside the function: * 1. prints the fahrenheit value: "F: <temp in fahrenheit>". * 2. calls fahrenheitToCelsius, and * prints the celcius value: "C: <temp in celsius> \n". */
- Parameter.
/** * Name: printTemperatures * @param fahrenheit (double) <----- * * Inside the function: * 1. prints the fahrenheit value: "F: <temp in fahrenheit>". * 2. calls fahrenheitToCelsius, and * prints the celcius value: "C: <temp in celsius> \n".. */
-
Return value: The function is
void
. -
Code the logic inside the function
/** * Name: printTemperatures * @param fahrenheit (double) * * Inside the function: <----- * 1. prints the fahrenheit value: "F: <temp in fahrenheit>". * 2. calls fahrenheitToCelsius, and prints the celcius value: "C: <temp in celsius> \n".. */
Task 3 - Call the function
From main()
, call the printTemperatures
function for each value.
Run your code
Visualizing the Runtime
After you solve this workbook, I still recommend watching the video solution on Udemy.
It will show you how to visualize the runtime using Visual Studio Code.
Associated Course: The Complete Java Development Bootcamp
Related Course: The Complete Spring Boot Development Bootcamp – Become a Java Web Developer
Feedback Summary
4.7
43 students
5
91%
4
2%
3
0%
2
0%
1
7%
Written Reviews
There are no written reviews yet.