Java Bootcamp – Workbooks and Challenges
)}

Workbook 6.11

From the Java Bootcamp Resources, launch Workbook 6.11.

6.11.png

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[] ) * * * */
  1. The function creates a double[] array called fahrenheit.
/** * Function name: celciusToFahrenheit. * @param celsius ( double[] ) * @return fahrenheit ( double[] ) * * Inside the function: * 1. Creates a double[] array called 'fahrenheit'. * * * */
  1. The function copies every value from the celcius parameter to the fahrenheit 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. * * */
  1. 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). * */
  1. 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 * * * * * * * */
  1. The function prints the temperature type using print, not println.
/** * Function name - printTemperatures * @param temp ( double[] ) * @param tempType ( String ) can be: Celsius or Fahrenheit * * Inside the function: * 1. System.out.print(tempType + ": "); * * * * */
  1. 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] + " "); * * */
  1. 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.

11.png

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.