Java Coding if/else while loop
IS 2031 LE 3.3 IF, IF/ELSE & WHILE LAB EXERCISES Student Name:
1. Based on the code below, answer these questions (identify line numbers for Java statements):
Which line(s) will be executed if the condition in line 3 is true?
Processing then will proceed to which line?
Which line(s) will be executed if the condition in line 3 is false?
Processing will then proceed to which line?
Line 10 will always be executed whether line _____ is true or false.
Lines 5-6 will only be executed when line ______ is ____________.
Line 9 will only be executed when line ______ is ____________.
|
1 |
double purchasesYTD = 200.0; |
|
2 |
double discount = 0.0; |
|
3 |
if(purchasesYTD >= 200.00) |
|
4 |
{ |
|
5 |
System.out.printf(“%nUse this promo code on your next purchase ME09X1.”); |
|
6 |
discount = .10; |
|
7 |
} |
|
8 |
else |
|
9 |
System.out.printf(“%nPlease check our daily specials.”); |
|
10 |
System.out.printf(“%nYou qualify for a %.0f%% discount.”, discount); |
|
|
|
2. Re-code the IF/ELSE structure in lines 3-9 above so the else statement is now the if statement. HINT: The condition tested should be reversed.
3. Use printf() with format specifiers as needed. Code on this page the following:
a. Code an if statement that prints “I got the promotion!”. The if condition tests the boolean variable promotion which is already declared.
b. Given a boolean variable called inactive, use the conditional operator (ternary operator) to assign ‘Y’ to the variable closeAcct; otherwise, assign ‘N’. Assume both variables are already declared.
c. Write an if-else statement that assigns 2 extra credit points for an 80% class attendance rate, 3 extra credit points for an 85% class attendance rate, and 5 extra credit points for anything 90% and above. Assume extraCredit and attendance are already declared. Make sure you scope the code properly.
d. Code a while loop that keeps printing “You are a DEDICATED student!” as long as the user enters an attendance rate of 90% or greater. Assume the loop-control variable, attendance , is already declared along with input for the Scanner class. The while loop requires a priming read for the loop-control variable prior to entering the loop.
e. There are 20 students. Code a while loop that processes extra credit points for student attendance. Use the if/else coded in 3c. Print “Sorry, you’ve not earned any extra credit for your attendance.” or “Congratulations! You’ve earned 9 extra credit points because of your good attendance.” where the 9 is the actual number of points earned based on the if/else from 3c. Assume the input variable for the Scanner class, the attendance and extraCredit variables are already declared. Declare the loop-control variable.
1