Write a While Loop Equivalent to the for Loop Code Given Below

Total Page:16

File Type:pdf, Size:1020Kb

Write a While Loop Equivalent to the for Loop Code Given Below

[ CSE113 – EXAM2, VERSION 1] Fall 2014 1. (While loop) Write a while loop equivalent to the for loop code given below: int abc = 0; for (int i = 0; i < 18; i= i+4) { abc = abc + i;

println( “ i = “ + i + “ abc = “ + abc); // simply prints the values of I and xyz }

2. (switch statement) Write the switch statement equivalent to the if..else statement given below: if (weekday ==1) day = “Monday”; else if (weekday == 2) day = “Tuesday”; else if (weekday == 3) day =”Wednesday”; else if (weekday == 4) day =”Thursday”; else day = “Friday”;

3. (Array definition) Write Processing statements that (i) defines an array dailyTemp that contains daily temperatures (float type data); (ii) create 365 entries in the array for recording temperatures for the year (iii) write a “for” loop that initializes the array with random values between 0 and 80.

4. (Array Access) Write a Processing statements to (i) initialize a variable sum to 0.0 (ii) create a “for” loop to accumulate the sum (in sum) of all the temperatures in the array dailyTemp above and (iii) after the “for” loop, and accumulating the sum, determine the average of the daily temperatures in another variable avgTemp. Hint: you can use arrayName.length attribute.

3. (While loop) Write a while loop equivalent to the for loop code given below: int abc = 0; for (int i = 0; i < 18; i= i+4) { abc = abc + i;

println( “ i = “ + i + “ abc = “ + abc); // simply prints the values of I and xyz }

4. (switch statement) Write the switch statement equivalent to the if..else statement given below:

1 [ CSE113 – EXAM2, VERSION 1] Fall 2014 if (weekday ==1) day = “Monday”; else if (weekday == 2) day = “Tuesday”; else if (weekday == 3) day =”Wednesday”; else if (weekday == 4) day =”Thursday”; else day = “Friday”;

5. (MousePressed: Lab3 based) Write a complete mousePressed function that draws a circle of diameter 4 of color green and circle of diameter 8 of color blue. (Yes, concentric circles). 6. (Working with canvas space: Lab 3 based) Write statements that draw a red rectangle that covers the upper left quadrant, green rectangle that covers the upper right quadrant, blue rectangle that covers the lower left quadrant and white rectangle that covers the lower right quadrant of the sketch window, irrespective of the size of the sketch window. 7. (Function definition) Write a function coinCollector that inputs two parameters (i) number of quarters (25 cents) and (ii) number of nickels (5 cents) and computes and returns the total value of the input parameters in cents. For example if I have 5 quarters and 3 nickels the total value of the coins I have is 140 cents: 5 and 3 will be input parameters, it will return the value 140.

8. (Function call) Assume I have 300 quarters and 10 nickels in my piggy bank. Invoke/call this function coinCollector to compute the value of the piggy bank and assign this value to myChange.

2

Recommended publications