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

for tuesday
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

For Tuesday

  • Read Gaddis, chapter 5, sections 6-9
slide-2
SLIDE 2

Program 4

  • Any questions?
slide-3
SLIDE 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.

slide-4
SLIDE 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.

slide-5
SLIDE 5

Switches in Pseudocode

  • Typically, just write the nested if.
slide-6
SLIDE 6

Formatting Numbers

slide-7
SLIDE 7

Random class

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

semester.

slide-8
SLIDE 8

Increment and Decrement

slide-9
SLIDE 9

Repetition

slide-10
SLIDE 10

while loops

  • while (condition)

statement

slide-11
SLIDE 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

slide-12
SLIDE 12

Loops in Pseudocode

slide-13
SLIDE 13

Infinite Loops

slide-14
SLIDE 14

Counting Loop

  • Write a method to add the numbers 1 to 10 and

return the sum using a while loop.

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

slide-16
SLIDE 16

Sentinel Values

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

slide-18
SLIDE 18

Loop and a Half

slide-19
SLIDE 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

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

slide-21
SLIDE 21

Do While loops

  • Always execute the body at least once.
  • Occasionally useful.
  • Seldom the “right” answer.
slide-22
SLIDE 22

Validating User Input

slide-23
SLIDE 23

For loops

  • Short hand for counting loops
  • Any for loop can be rewritten as a while loop
slide-24
SLIDE 24

Do Over

  • Write a method to compute and return the sum
  • f 1 to 10 using a for loop.