For Tuesday • Read Gaddis, chapter 5, sections 6-9
Program 4 • Any questions?
• Write a method to determine the purchaser‟s discount based on a code. – If the code is 1, there is no discount. – If the code is 3, the discount is 15%. – If the code is 7, the discount is 10%. – If the code is 8, the discount is 30%. – If the code is 12, the discount is 4%. • The method should return the discount. Use a switch statement.
• A program is required to read a customer‟s name, a purchase amount and a tax code. The tax code has the following meaning: „A‟ or „a‟ tax exempt (0%) „B‟ or „b‟ state sales tax only (3%) „C‟ or „c‟ federal and state sales tax (5%) „H‟ or „h‟ special sales tax (7%) The program must then compute the sales tax and the total amount due and print the customer‟s name, purchase amount, sales tax and total amount due. If the tax code is invalid, the program should print an error message and set the purchase amount to zero.
Switches in Pseudocode • Typically, just write the nested if.
Formatting Numbers
Random class • A useful utility class. • We may use it at some point during the semester.
Increment and Decrement
Repetition
while loops • while (condition) statement
Problem 1 • Write a program that sums a sequence of integers. Assume that the first integer read specifies the number of values remaining to be entered. Your program should read only one value at a time. A typical input sequence might be 5 100 200 300 400 500
Loops in Pseudocode
Infinite Loops
Counting Loop • Write a method to add the numbers 1 to 10 and return the sum using a while loop.
Counting Loop 2 • Write a while loop to compute the sum of all integers between first and second (including first and second ), where first and second are integers and first <= second . You may not change the value of either first or second .
Sentinel Values
Using a sentinel • Design and write a program that will prompt for, receive, and total a collection of payroll amounts entered by the user until a sentinel amount of -99 is entered. After the sentinel has been entered, display the total payroll amount on the screen.
Loop and a Half
Designing a while Loop • Determine what steps need to be repeated • Figure out the stopping condition • Reverse that to get the condition for the loop • Determine whether a partial loop needs to be done before or after the loop itself
Sentinel Again • Write a program that finds the smallest of several integers. Assume that input will end when a sentinel value of – 999 is read. Do not count – 999 as one of the integers to consider.
Do While loops • Always execute the body at least once. • Occasionally useful. • Seldom the “right” answer.
Validating User Input
For loops • Short hand for counting loops • Any for loop can be rewritten as a while loop
Do Over • Write a method to compute and return the sum of 1 to 10 using a for loop.
Recommend
More recommend