Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.

Similar presentations


Presentation on theme: "Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition."— Presentation transcript:

1 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

2 2 Objectives You should be able to describe: Basic Loop Structures The while Statement Interactive while Loops The for Statement The do-while Statement

3 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition3 Objectives (continued) Recursion Program Design and Development: UML State Diagrams Applications: Random Numbers and Simulations Common Programming Errors

4 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition4 Basic Loop Structures Repetition statements –Provide capability to type set of instructions once –Have instructions repeated continuously –Until some preset condition is met

5 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition5 Basic Loop Structures (continued) Elements required for repetition: –Repetition statement –Condition that must be evaluated –Statement that initially sets condition –Statement within repeating section of code that allows condition to become false

6 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition6 Basic Loop Structures (continued) Java repetition structures: –while structure –for structure –do-while structure

7 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition7 Pretest and Posttest Loops Pretest loop –Condition being tested evaluated at beginning of repeating section of code –If condition is true executable statements within loop are executed –If initial value of condition is false executable statements within loop are never executed at all –Also called entrance controlled loops

8 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition8 Pretest and Posttest Loops (continued) Posttest loop –Loop that evaluates condition at end of repeating section of code –Always execute loop statements at least once before condition is tested –do-while construct is example of posttest loop

9 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition9 Pretest and Posttest Loops (continued) Figure 6.1: A pretest loop

10 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition10 Pretest and Posttest Loops (continued) Figure 6.2: A posttest loop

11 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition11 Fixed-Count Versus Variable- Condition Loops Fixed-count loop –Condition is used to keep track of how many repetitions have occurred –All of Java’s repetition statements can be used Variable-condition loop –Tested condition depends on variable that can change interactively with each pass through loop –All of Java’s repetition statements can be used

12 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition12 The while Statement General repetition statement –Can be used in variety of programming situations Syntax: while (condition) statement; Statement following condition is executed repeatedly as long as condition remains true

13 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition13 The while Statement (continued) 1.Test condition 2.If condition has nonzero (true) value a.Execute statement following parentheses b.Go back to step 1 Else Exit the while statement

14 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition14 The while Statement (continued) Figure 6.3: The anatomy of a while loop

15 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition15 The while Statement (continued) Must assign values to each variable in tested condition –Before while statement is encountered Infinite loop –Loop that never ends –To exit infinite loop: Pressing Ctrl and C keys at same time will break program execution

16 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition16 Interactive while Loops Combining interactive data entry with repetition capabilities of while statement –Produces adaptable and powerful programs User entered value used in conditional statement

17 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition17 Sentinels Sentinel –Data values that signal either start or end of data series –Must be selected so as not to conflict with legitimate data values

18 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition18 The break and continue Statements break statement –Forces immediate break, or exit, from: switch while for do-while

19 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition19 The break and continue Statements (continued) break statement (continued) –Violates pure structured programming principles: Provides second nonstandard exit from loop Extremely useful and valuable for breaking out of loops when unusual condition is detected

20 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition20 The break and continue Statements (continued) continue statement –Applies only to loops created with: while do-while for –When encountered in loop next iteration of loop begins immediately –Not often used in practice

21 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition21 The null Statement Semicolon with nothing preceding it –; Do-nothing statement –Used where statement is syntactically required but no action is called for

22 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition22 The for Statement Performs same functions as while statement –Uses different form Syntax: for (initializer; condition; increment) statement; Condition –Middle item in parentheses –Any Java expression that yields Boolean value

23 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition23 The for Statement (continued) Allows one or more initializing expressions to be grouped together –First set of items within for’s parentheses –Commas used to separate multiple initializations Last item within for’s parentheses –Condition-altering expressions –Typically increment or decrement Initializer, condition, increment and statement can be replaced with null statement

24 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition24 The for Statement (continued) Figure 6.11: The for statement’s flow of control

25 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition25 Interactive for Loops Using showInputDialog() method inside for loop –Produces same effect as when method is used within while loop

26 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition26 Do You Use a for or a while Loop? Each can construct both fixed-count and variable- condition loops Matter of style

27 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition27 Nested Loops Have loop within another loop Entire inner loop executed for each outer loop iteration

28 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition28 Nested Loops (continued) Figure 6.12: For each i, j loop

29 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition29 The do-while Statement Execute statements before expression is evaluated –Posttest Syntax: do statement; while(condition);

30 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition30 The do-while Statement (continued) Figure 6.13: The do statement’s flow of control

31 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition31 Validity Checks Data validity checks –Validate user input –Use do-while statement Read value inside loop Check entered value Exit loop when value correct

32 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition32 Recursion Method calls itself –Receives new copies of arguments and local variables Direct recursion –Method invokes itself Indirect or mutual recursion –Method can invoke second method, which in turn invokes first method

33 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition33 Mathematical Recursion Recursive concept –Solution to problem can be stated in terms of “simple” versions of itself Factorial of number n: –Denoted as n!, where n is a positive integer 1! = 1 n! = n * (n * 1)! for n > 1

34 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition34 Mathematical Recursion (continued) General considerations that must be specified: –What is first case? –How is nth case related to (n - 1) case?

35 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition35 Mathematical Recursion (continued) Pseudocode for factorial method: If n = 1 factorial = n Else factorial = n * factorial(n - 1)

36 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition36 How the Computation Is Performed Java method can call itself due to: –Java’s allocation of new memory locations for all method arguments and local variables As each method is called Made dynamically in memory area called stack Stack –Memory for rapidly storing and retrieving data

37 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition37 Recursion Versus Iteration Recursive method can be applied to any problem where solution represented in terms of solutions to simpler versions of same problem Recursive methods –Can always be written in non-recursive manner using iterative solution

38 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition38 Recursion Versus Iteration (continued) If problem solution can be expressed iteratively or recursively with equal ease iterative solution is preferable –Executes faster –Uses less memory Times when recursive solutions preferable: –Some problems are easier to visualize using recursive algorithm –Sometimes provides much simpler solution

39 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition39 Program Design and Development: UML State Diagrams Present transition of object’s state over time Considered dynamic models Show: –Different states that object can have –Events that cause states to appear

40 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition40 Program Design and Development: UML State Diagrams (continued) Event –Defined as individual signal (stimulus) from one object to another –Always one-way signals State –Defined by values of attributes –Activities are associated with states

41 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition41 Program Design and Development: UML State Diagrams (continued) Can represent either: –Continuously operating system –Or finite, one-time life cycle Almost always have initial state May have one or more final states or no final state

42 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition42 Program Design and Development: UML State Diagrams (continued) Figure 6.17: The state model identifies operations to be included in the class diagram

43 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition43 Program Design and Development: UML State Diagrams (continued) Figure 6.18: State diagram notation

44 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition44 Applications: Random Numbers and Simulations Random numbers –Series of numbers whose order cannot be predicted –Hard to find in practice Pseudorandom numbers –Sufficiently random for task at hand Java compilers provide general-purpose method for creating random numbers –Math.random()

45 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition45 Scaling Method for adjusting random numbers produced by random number generator to reside within ranges, such as: –1 to 100 –Accomplished using expression: (int) (Math.random() * N) Where number falls between 0 and N-1

46 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition46 Simulations Common use of random numbers: –Simulate events rather than going through time and expense of constructing real-life experiment Coin Toss Simulation –Use random number generator to simulate coin tosses Elevator Simulation –Simulate the operation of an elevator

47 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition47 Simulations (continued) Figure 6.27: Class diagram for a CoinToss class

48 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition48 Simulations (continued) Figure 6.28: A state diagram for an elevator

49 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition49 Common Programming Errors Creating loop that is “off by one” Repetition statements should not test for equality when testing floating-point (real-values) operands Placing semicolon at end of either while or for’s parentheses Using commas to separate items in for statement instead of required semicolons Omitting final semicolon from do-while statement

50 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition50 Summary Section of repeating code is referred to as loop –Types of loops: while for do-while while statement –Checks condition before any other statement in loop is executed

51 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition51 Summary (continued) for statement –Extremely useful in creating loops that must be executed fixed number of times do-while statement –Checks condition at end of loop Recursion –Method calls itself with new copy of arguments and local variables


Download ppt "Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition."

Similar presentations


Ads by Google