Objective: to practise use of


Objective: To practise use of if-else, switch, while, break, continue statements and ?: operator.
(i) Create a new folder called ‘assign41’ under C:\Java\Lesson4. Copy HelloWorld.java from Lesson 1 into it, and rename the file to “Weather.java”. Replace the word “HelloWorld” with “Weather”. Replace the System.out line with instructions to declare variables, and display output as described below:
(ii) Declare in ‘int’ type variable called ‘i’ and initialize it with a value of 1. This variable represents the year, starting at Year 1.
(iii) Declare a ‘double’ type variable called ‘temp’ initialized to 0.0.
(iv) Declare a ‘final double’ type constant called ‘ANNUAL_INCREASE’, and assign it a value of 1.0.
(v) Declare an ‘double’ type variable called ‘uncertainty’ initialized to 0.0.
(vi) Declare an ‘String’ type variable called ‘result’ initialized to ” “.
(vii) Using a System.out.println line, display the header: ” Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec”.
(viii) Using a while(true) statment, loop over Year i from 1 thro 5, EXCEPT i = 3. Use a break statement to exit the loop as appropriate. When i = 3, it should skip the calculations and go the next iteration, so use a continue statement for that.
(ix) In each iteration of the while loop, assign a value to ‘uncertainty’ using a switch statement and the following table:
Year (i) Uncertainty (Percent)
1 2.1
2 4.3
4 7.4
5 8.6

(x) Next (still inside the while loop), use the following formula to compute the temperature of each of the 12 months of years 1, 2, 4 and 5. Use a for loop to loop over the months in a given year.
Year i = 1:
Month j = 1, 2, … 12
Note: (A) and (B) below, taken together will be called “Equation 1”.
(A) Winter Months: j = 1, 2, 11 and 12: Temperature = j + 3
(B) Other Months: j = 3, 4, 5, .., 9, 10: Temperature = 5j + 7

Note: Use a logical expression to differentiate between Winter and Other Months, in conjunction with the ?: operator, all in one single statement to compute the temperature for a month j using Equation 1.

1st year, so ANNUAL_INCREASE doesn’t apply, but uncertainty does. In the next line, increase Temperature by Uncertainty percent to display the final temperature, for each month.

Example: If Temperature calculated above is 45.0, and uncertainty is 2.1 percent, then final Temperature is 45.0 * (1.0 + 2.1/100) = 45.945. Only the integer part is to be displayed (decimal part truncated), so it should show 45 as the value in this case.
Years: i = 2, 4, 5
First calculate Temperature using Equation 1. Next, find the increases to apply to that value. Temperature increase per year: ANNUAL_INCREASE (degrees) – same increase applies to each month of a year. Use the Year i and ANNUAL_INCREASE in an expression to figure the exact increase that would apply for that year.

Uncertainty percent applies to the value obtained after the ANNUAL_INCREASE.

Example: If temperature for a month is 45.0 using Equation 1, and it is Year 4, so you are 3 years away from Year 1, which means 3 annual increases on top of each other. Each annual increase is 1.0 degree (= ANNUAL_INCREASE), so for 3 years beyond Year 1, you need 1.0 + 1.0 + 1.0 = 3.0 degrees from the base value (45.0), so temp for Year 4 would be 45.0 + 3.0 = 48.0.

Next, you apply the uncertainty percent on this modified value (48.0) as applicable for year 4 (7.4 percent). So the temperature for that month in Year 4 becomes: 48.0 X (1.0 + 7.4/100) = 51.552. Value displayed will be integer part only (decimal part truncated), so it should display 51 in this example.

Inside the ‘for loop’, concatenate temperature values for all 12 months with spaces in between, assign it to ‘result’ and display in one line along with the Year i. Repeat for years 1, 2, 4 and 5. Try to align the output with the Month headers by maniupulating the blank space in between. 

Place this order or similar order and get an amazing discount. USE Discount code “GET20” for 20% discount