Java Bootcamp – Workbooks and Challenges
)}

Workbook 6.11

From Java Bootcamp Resources -> Module 1 -> 6\. Arrays, open Workbook 6.11.

Weather

Once again, you're a weatherman!

Here is the forecast for the next 7 days. The temperatures are in Celsius.

12.5 -- 14.5 -- 17.0 -- 21.0 -- 23.0 -- 18.5 -- 20.0

As a weatherman, you need to present these temperatures in Celcius and Fahrenheit.

Task 1


Create a function that takes an array of Celcius values and returns them in Fahrenheit.

/** * Function name: celciusToFahrenheit. * @param celsius ( double[] ) * @return fahrenheit ( double[] ) * * Inside the function: * 1. Creates an array 'fahrenheit'. * 2. Copies all the values from celsius to fahrenheit. * 3. Updates the fahrenheit values (F = (C/5 * 9) + 32). * 4. returns the fahrenheit array. */

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};

Pass them into your function, and store the return value in the variable: double[] fahrenheit. Print the contents of fahrenheit using Arrays.toString.

Output:

[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 takes an array and prints it in a special format.

/** Task 3 * Function name - printTemperatures * @param temp ( double[] ) * @param type ( String ) can be: Celsius or Fahrenheit * * Inside the function: * * 1. A loop prints the temperatures in ONE line: * Celsius: [Celsius values separated by a space] * Fahrenheit: [Fahrenheit values separated by a space] * 2. Adds a new line of space after the for loop. */

Task 4


Call printTemperatures twice. Once for celsius, and once for fahrenheit.

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

Task 5


The fahrenheit values have too many decimal points. Research how the String.format function can round to two decimal places.

Good Luck!

Feedback Summary
4.8
39 students
5

92%
4

3%
3

0%
2

0%
1

5%
Written Reviews
There are no written reviews yet.