Lab 03 Looping
Lab 03 Looping
Lab 3 Looping
SPRING 2020
15 pts / 6 extra
Mini-lecture:
We will continue to refine our pseudo code. Now, we will look at looping
We looked at looping in class. Java has a while loop and a for loop. These are both pre-test
loops. The for loop is a definite loop and the while loop is an indefinite loop. (Java also has a
post-test indefinite loop: do..while.
Here is the flowchart and the pseudo code for an indefinite loop that uses a sentinel to terminate
the repetition of the loop. In this variation the sentinel is the value “N” which the user enters
when they are prompted if they want to continue or not.
Here is the general format for the while loop in pseudo code:
while BOOLEAN EXPRESSION
statements here that will be repeated
endWhile
As discussed, Boolean expressions are expressions that evaluate to true or false. These are
formed by relational operators (<. >,< =,>=, !=) and the logical operators AND && and OR ||.
For loops are definite pre-test loops. Use these when you want to repeat something some fixed
number of times.
do
these statements repeat
while BOOLEAN EXPRESSION
Which to use?
Use a while loop when you don’t know exactly how many times the loop will repeat (indefinite
loop)
Use a for loop when you know exactly how many times the loop will repeat (definite loop)
Both these loops are pre-test loops which means that if the first test fails (is false) then the
statements in the loop body are never executed.
The do while loop is also indefinite but is post test so that loop runs the code block at least once
before the first test. Every time you get input you do this. You collect data and plan to loop back
only to correct it!
Lab:
1. Just submit this document (See directions below.) and insert your work after each task
description.
2. For each of the following tasks, provide the complete pseudo code including all
elements that we have been using (i.e. class, end class, main return, output prompt).
Be sure to declare your variables and use symbolic constants where appropriate.
Determine if each task requires a definite or indefinite loop and use the correct one.
Use the do while loop for loops that you want to execute at least once, like getting
input.
a. Task 1 (3 pts): an application program where the user enters the price of
a series of items (assume at least one.) (Prompt user for additional items
class ShippingCalculator
main()
num itemCost = 0
num itemTotal = 0
num SHIPPING_RATE = .02
num shippingTotal = 0
num billTotal = 0
num itemNumber = 0
String response
boolean done = false
do
itemNumber = itemNumber + 1
output “Please enter the cost of item number” + itemNumber
input itemCost
itemTotal = itemTotal + itemCost
output “Do you have another item? Type Y for yes and N for no”
input response
if response == “N”
done = true
while done == false
If itemTotal <= 100 then
shippingTotal = itemTotal * SHIPPING_RATE
billTotal = shippingTotal + itemTotal
endIf
output “The total cost of your items is $” + itemTotal + “ The total cost of your shipping is
$” + shippingTotal + “ The total cost of your bill is $” + billTotal
return
end class
b. Task 2 (4 pts): A program that prompts the user for an initial balance
and a monthly interest rate and then displays each new monthly balance
class MonthlyInterestRateCalc
main()
num initialBalance = 0
num interestRate = 0
num interestTotal = 0
num month = 0
output “What is your initial balance?”
input initialBalance
output “What is your monthly interest rate?”
input interestRate
for month = 1 to 12 step 1
interestTotal = initialBalance * interestRate
initialBalance = initialBalance + interestTotal
interestTotal = 0
output “Your balance after “ + month + “ month(s) is $” + initialBalance
endFor
return
end class
d. Task 4 (5 pts): Rock Paper Scissors: The game repeats as long as the user
enters Y to a prompt to continue. Get a move from player A and then
from player B. Assume that the move is valid so it has to be one of the 3
choices (R P or S). Your program should compute the winner with an
appropriate output string: “Rock Breaks Scissors Player A wins, etc...
And then prompt to play again…
class RockPaperScissors
main()
string playerA
string playerB
boolean done = false
3. Submitting your work: carefully check your work. Rename your word file as
Lastname_Firstname_Lastname_Firstname Lab03.docx using your name.
Submit this file using the Canvas assignment mechanism. For The Extra Credit
resubmit the entire lab again using the second submission link provided.