30 Sep Instructions:?? Objective: To practise use of if-else, switch, w
Objective: To practise use of if-else, switch, while, break, continue statements and ?: operator.
Steps:
(i) Create a new folder called 'assign41' under C:JavaLesson4. 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.
Our website has a team of professional writers who can help you write any of your homework. They will write your papers from scratch. We also have a team of editors just to make sure all papers are of HIGH QUALITY & PLAGIARISM FREE. To make an Order you only need to click Ask A Question and we will direct you to our Order Page at WriteDemy. Then fill Our Order Form with all your assignment instructions. Select your deadline and pay for your paper. You will get it few hours before your set deadline.
Fill in all the assignment paper details that are required in the order form with the standard information being the page count, deadline, academic level and type of paper. It is advisable to have this information at hand so that you can quickly fill in the necessary information needed in the form for the essay writer to be immediately assigned to your writing project. Make payment for the custom essay order to enable us to assign a suitable writer to your order. Payments are made through Paypal on a secured billing page. Finally, sit back and relax.
About Wridemy
We are a professional paper writing website. If you have searched a question and bumped into our website just know you are in the right place to get help in your coursework. We offer HIGH QUALITY & PLAGIARISM FREE Papers.
How It Works
To make an Order you only need to click on “Order Now” and we will direct you to our Order Page. Fill Our Order Form with all your assignment instructions. Select your deadline and pay for your paper. You will get it few hours before your set deadline.
Are there Discounts?
All new clients are eligible for 20% off in their first Order. Our payment method is safe and secure.
