Java Bootcamp – Workbooks and Challenges
)}

Workbook 4.3

From Java Bootcamp Resources -> Module 1 -> 4. Functions, open Workbook 4.3.

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


Good Luck!


Feedback Summary
4.8
38 students
5

92%
4

3%
3

0%
2

0%
1

5%
Written Reviews
There are no written reviews yet.