Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › To practise use of if-else, switch, while, break, continue statements and ?: operator.
New Posts  All Forums:Forum Nav:

# To practise use of if-else, switch, while, break, continue statements and ?: operator.

Steps:

(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.
(xi) A sample output session is shown below (your output needs to match with this):

C:\Java\Lesson4\assign41>javac Weather.java

C:\Java\Lesson4\assign41>java Weather

Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1 4 5 22 27 32 37 42 47 53 58 14 15
2 5 6 23 29 34 39 44 50 55 60 15 16
4 7 8 26 32 37 42 48 53 59 64 18 19
5 8 9 28 33 39 44 49 55 60 66 19 20
C:\Java\Lesson4\assign41>

(xii) Submit Weather.java.
package com.java.src;

public class Weather{

public static void main(String[] args) {
int i = 1;
double temp = 0.0;
final double ANNUAL_INCREASE = 1.0;
double uncertainty = 0.0;
String result = " ";

System.out.println(" Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");

while(true)
{
if (i > 5) break;
//System.out.println(" "+i);
i++;
{
if(i==3){i++;continue;}
}
}
switch(i)
{
case 1 :
uncertainty = 2.1;
break;
case 2 :
uncertainty = 4.3;
break;
case 4 :
uncertainty = 7.4;
break;
case 5 :
uncertainty = 8.6;
break;
default:
uncertainty = 0;
break;
}

}
}

I have done these much.. need help step number 10...
You have a few error in the code you posted, I fix that and commented the changes the best I could
I also gave you some pseudocode for the rest of the code

all you have to do is follow this pseudocode you should be good to go
it is marked with start stop tags
Code:
``````public class Weather {

public static void main(String[] args) {
int i = 1;
double temp = 0.0;
final double ANNUAL_INCREASE = 1.0;
double uncertainty = 0.0;
String result = "";

System.out.println(" Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");

while(true)
{

if (i > 5) { // open bracket for if statement
break;
} // close bracket for if statement

//System.out.println(" "+i); you can delete this as it is not needed

// i++; this is in the wrong place moving to end of while loop
// i is set to 1 before the loop
//{ not needed

if(i==3){
i++;
continue;
}

//} not needed
//} removed as it is closing the while loop to soon

// put the switch in the while loop
switch(i){
case 1 :
uncertainty = 2.1;
break;
case 2 :
uncertainty = 4.3;
break;
case 4 :
uncertainty = 7.4;
break;
case 5 :
uncertainty = 8.6;
break;
default:
uncertainty = 0;
break;
}

// write your for loop here still inside the while loop
// pseudocode starts here
/*
for loop over the months 1 through 12

use the ternary operator (? :) to to assign the formula
to the correct month
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
months 1 or 2 or 11 or 12 the formula is j + 3
all other months the formula is (5 * j) + 7
all of the above will be a single line of code (as required in the instruction) and will give you the temp for a given month

add the ANNUAL_INCREASE for years that apply
years greater then 1
us an if block or another ternary statement, the instructions do not require one or the other

calculate the uncertainty using the formula from the instruction
you posted

concatenate temperature values for all 12 months with spaces
in between, assign it to 'result'
*/

// display the results in one line along with the Year
// pseudocode ends here

i++;
/*
increment i at bottem of while loop
comming into the loop i is set to the value we will be using
only after everything is done do we want to change it
so that the next time through it is set
*/
} //added to close while loop
}
}```
```
 Core I7 (13 items)
CPUMotherboardGraphicsRAM
I7 920 rev. D0 @ 4.26Ghz EVGA X58 SLI EVGA GTX 285 OCZ XMP 3x2Gb (pc3 12800)
Hard DriveOptical DriveOSMonitor
Western Digital Caviar Black 640Gb x 2 LG GH22LS30 openSuse 12.1 x64 HP F2105
PowerCase
CORSAIR 850TX Cooler Master ATCS 840
 Core I7 (13 items)
CPUMotherboardGraphicsRAM
I7 920 rev. D0 @ 4.26Ghz EVGA X58 SLI EVGA GTX 285 OCZ XMP 3x2Gb (pc3 12800)
Hard DriveOptical DriveOSMonitor
Western Digital Caviar Black 640Gb x 2 LG GH22LS30 openSuse 12.1 x64 HP F2105
PowerCase
CORSAIR 850TX Cooler Master ATCS 840
for(int j =1; j<=12;j++)
{

temp = (j<=2 || j>=11)? j+3:5*j+7;
if (i>1)
ANNUAL_INCREASE = temp *(1.0 + uncertainty/100);
uncertainty = ANNUAL_INCREASE * (1.0 + uncertainty);

}

i++;
}
System.out.println(temp + result );
}
}

still confuse on these steps

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.
Your close to the answer, only a few little things to fix in your formulas and how you use them
Code:
``````for(int j =1; j<=12;j++)
{

temp = (j<=2 || j>=11)? j+3:5*j+7;

if (i>1)
/*
ANNUAL_INCREASE is defined as Final,  so you are using it incorrectly on this line
and the formula is incorrect (the formulas you used calculates the uncertainty)
you need to increase the temperature by the annual increase
on this line you add to the temperature 1 degree for each year except the first year
year 1 no ANNUAL_INCREASE
year 2 1 ANNUAL_INCREASE
year 3 2 ANNUAL_INCREASE
year 4 3 ANNUAL_INCREASE
year 5 4 ANNUAL_INCREASE
*/
ANNUAL_INCREASE = temp *(1.0 + uncertainty/100); // line to fix

/*
use the formula on the above line,  but assign it to the temperature
*/
uncertainty = ANNUAL_INCREASE * (1.0 + uncertainty); // line to fix

/*
here you need to build the string you are going to print

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
manipulating the blank space in between.
/*
result = result  + ;// finish this line, note displayed will be integer part only (decimal part truncated)
}

/*
Do not forget to add the number of the year and a space to the begging of the
result before (or when) you display it
maybe move i++; to after you System.out.println
and then print (i + " " + result)
/*
i++; // line to move down
System.out.println(temp + result ); // line to fix```
```
 Core I7 (13 items)
CPUMotherboardGraphicsRAM
I7 920 rev. D0 @ 4.26Ghz EVGA X58 SLI EVGA GTX 285 OCZ XMP 3x2Gb (pc3 12800)
Hard DriveOptical DriveOSMonitor
Western Digital Caviar Black 640Gb x 2 LG GH22LS30 openSuse 12.1 x64 HP F2105
PowerCase
CORSAIR 850TX Cooler Master ATCS 840
 Core I7 (13 items)
CPUMotherboardGraphicsRAM
I7 920 rev. D0 @ 4.26Ghz EVGA X58 SLI EVGA GTX 285 OCZ XMP 3x2Gb (pc3 12800)
Hard DriveOptical DriveOSMonitor
Western Digital Caviar Black 640Gb x 2 LG GH22LS30 openSuse 12.1 x64 HP F2105
PowerCase
CORSAIR 850TX Cooler Master ATCS 840
Also where is this assignment from?
 Core I7 (13 items)
CPUMotherboardGraphicsRAM
I7 920 rev. D0 @ 4.26Ghz EVGA X58 SLI EVGA GTX 285 OCZ XMP 3x2Gb (pc3 12800)
Hard DriveOptical DriveOSMonitor
Western Digital Caviar Black 640Gb x 2 LG GH22LS30 openSuse 12.1 x64 HP F2105
PowerCase
CORSAIR 850TX Cooler Master ATCS 840
 Core I7 (13 items)
CPUMotherboardGraphicsRAM
I7 920 rev. D0 @ 4.26Ghz EVGA X58 SLI EVGA GTX 285 OCZ XMP 3x2Gb (pc3 12800)
Hard DriveOptical DriveOSMonitor
Western Digital Caviar Black 640Gb x 2 LG GH22LS30 openSuse 12.1 x64 HP F2105
PowerCase
CORSAIR 850TX Cooler Master ATCS 840
on study course...

public class Weather{

public static void main(String[] args) {
int i = 1;
double temp = 0.0;
final double ANNUAL_INCREASE = 1.0;
double uncertainty = 0.0;
String result = " ";

System.out.println(" Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");

while(true)
{
if (i > 5) {
break;
}
else
if(i==3){
i++;
continue;
}

switch(i){
case 1 :
uncertainty = 2.1;
break;
case 2 :
uncertainty = 4.3;
break;
case 4 :
uncertainty = 7.4;
break;
case 5 :
uncertainty = 8.6;
break;
default:
uncertainty = 0.0;
break;
}
for(int j =1; j<=12;j++)
{

temp = (j<=2 || j>=11)? j+3:5*j+7;
temp += ANNUAL_INCREASE *(i-1);
temp *= (1.0 + uncertainty/100);
result += (int)temp + " ";
}
System.out.println(" "+i+" "+ result);
i++;
result=" ";
}

}
}
Thank you so much...tom.slick I got it... take time but, I clear my concept..
 Core I7 (13 items)
CPUMotherboardGraphicsRAM
I7 920 rev. D0 @ 4.26Ghz EVGA X58 SLI EVGA GTX 285 OCZ XMP 3x2Gb (pc3 12800)
Hard DriveOptical DriveOSMonitor
Western Digital Caviar Black 640Gb x 2 LG GH22LS30 openSuse 12.1 x64 HP F2105
PowerCase
CORSAIR 850TX Cooler Master ATCS 840
 Core I7 (13 items)
CPUMotherboardGraphicsRAM
I7 920 rev. D0 @ 4.26Ghz EVGA X58 SLI EVGA GTX 285 OCZ XMP 3x2Gb (pc3 12800)
Hard DriveOptical DriveOSMonitor
Western Digital Caviar Black 640Gb x 2 LG GH22LS30 openSuse 12.1 x64 HP F2105
PowerCase
CORSAIR 850TX Cooler Master ATCS 840
New Posts  All Forums:Forum Nav:
Return Home
Back to Forum: Application Programming
• To practise use of if-else, switch, while, break, continue statements and ?: operator.
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › To practise use of if-else, switch, while, break, continue statements and ?: operator.