Workbook 6.11
From the Java Bootcamp Resources
, launch Workbook 6.11
.
Task 1
Create a function called celciusToFahrenheit
.
/** * Function name: celciusToFahrenheit. * * * * * */
The function will receive an array of values in Celcius
.
/** * Function name: celciusToFahrenheit. * @param celsius ( double[] ) * * * * */
The function will return an array of values in Fahrenheit
.
/** * Function name: celciusToFahrenheit. * @param celsius ( double[] ) * @return fahrenheit ( double[] ) * * * */
- The function creates a
double[]
array calledfahrenheit
.
/** * Function name: celciusToFahrenheit. * @param celsius ( double[] ) * @return fahrenheit ( double[] ) * * Inside the function: * 1. Creates a double[] array called 'fahrenheit'. * * * */
- The function copies every value from the
celcius
parameter to thefahrenheit
array.
/** * Function name: celciusToFahrenheit. * @param celsius ( double[] ) * @return fahrenheit ( double[] ) * * Inside the function: * 1. Creates a double[] array called 'fahrenheit'. * 2. Copies all the values from celsius to fahrenheit. * * */
- The function uses the formula below to convert every value in the fahrenheit array to Fahrenheit.
/** * Function name: celciusToFahrenheit. * @param celsius ( double[] ) * @return fahrenheit ( double[] ) * * Inside the function: * 1. Creates a double[] array called 'fahrenheit'. * 2. Copies all the values from celsius to fahrenheit. * 3. Updates the fahrenheit values (F = (C/5 * 9) + 32). * */
- Finally, the function returns the result.
/** * Function name: celciusToFahrenheit. * @param celsius ( double[] ) * @return fahrenheit ( double[] ) * * Inside the function: * 1. Creates a double[] array called 'fahrenheit'. * 2. Copies all the values from celsius to fahrenheit. * 3. Updates the fahrenheit values (F = (C/5 * 9) + 32). * 4. return fahrenheit. */
Task 2
The starter code contains an array of Celsius values.
double[] celsius = {12.5, 14.5, 17.0, 21.0, 23.0, 18.5, 20.0};
Use your function to return an array of Fahrenheit values from the celsius
array.
double[] fahrenheit = celciusToFahrenheit(celsius);
Print the contents of fahrenheit
using Arrays.toString
.
Result
>>: [54.5, 58.099999999999994, 62.5999999999994, 69.800000000001, 73.4, 65.300000000001, 68.0]
Task 3
Remove your print statement. Instead, create a function that receives a double[]
array and a String
.
/** * Function name - printTemperatures * @param temp ( double[] ) * @param tempType ( String ) can be: Celsius or Fahrenheit * * * * * * * */
- The function prints the temperature type using
print
, notprintln
.
/** * Function name - printTemperatures * @param temp ( double[] ) * @param tempType ( String ) can be: Celsius or Fahrenheit * * Inside the function: * 1. System.out.print(tempType + ": "); * * * * */
- The function uses a
for
loop to print the temperatures on the same line.
/** * Function name - printTemperatures * @param temp ( double[] ) * @param tempType ( String ) can be: Celsius or Fahrenheit * * Inside the function: * 1. System.out.print(type + ": "); * 2. A loop prints the temperatures in ONE line System.out.print(temp[i] + " "); * * */
- The function prints a new line after the loop.
/** * Function name - printTemperatures * @param temp ( double[] ) * @param tempType ( String ) can be: Celsius or Fahrenheit * * Inside the function: * 1. System.out.print(type + ": "); * 2. A loop prints the temperatures in ONE line System.out.print(temp[i] + " "); * 3. Prints a new line after the loop System.out.print("\n"); */
Task 4
Call printTemperatures
for each array.
double[] celsius = {12.5, 14.5, 17.0, 21.0, 23.0, 18.5, 20.0}; double[] fahrenheit = celsiusToFahrenheit(celsius); printTemperatures(celsius, "Celsius"); printTemperatures(fahrenheit, "Fahrenheit");
Result
>>: Celsius: 12.5 14.5 17.0 21.0 23.0 18.5 20.0 >>: Fahrenheit: 54.5 58.099999999999994 62.599999999999994 69.80000000000001 73.4 65.30000000000001 68.0
Don't mind the decimal points. You will learn rounding in Module 2.
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.