for tuesday
play

For Tuesday Read Gaddis, chapter 5, sections 6-9 Program 4 Any - PowerPoint PPT Presentation

For Tuesday Read Gaddis, chapter 5, sections 6-9 Program 4 Any questions? Write a method to determine the purchasers discount based on a code. If the code is 1, there is no discount. If the code is 3, the discount is 15%.


  1. For Tuesday • Read Gaddis, chapter 5, sections 6-9

  2. Program 4 • Any questions?

  3. • 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.

  4. • 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.

  5. Switches in Pseudocode • Typically, just write the nested if.

  6. Formatting Numbers

  7. Random class • A useful utility class. • We may use it at some point during the semester.

  8. Increment and Decrement

  9. Repetition

  10. while loops • while (condition) statement

  11. 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

  12. Loops in Pseudocode

  13. Infinite Loops

  14. Counting Loop • Write a method to add the numbers 1 to 10 and return the sum using a while loop.

  15. 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 .

  16. Sentinel Values

  17. 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.

  18. Loop and a Half

  19. 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

  20. 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.

  21. Do While loops • Always execute the body at least once. • Occasionally useful. • Seldom the “right” answer.

  22. Validating User Input

  23. For loops • Short hand for counting loops • Any for loop can be rewritten as a while loop

  24. Do Over • Write a method to compute and return the sum of 1 to 10 using a for loop.

Recommend


More recommend