an object oriented approach to programming logic and design l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
An Object-Oriented Approach to Programming Logic and Design PowerPoint Presentation
Download Presentation
An Object-Oriented Approach to Programming Logic and Design

Loading in 2 Seconds...

play fullscreen
1 / 53

An Object-Oriented Approach to Programming Logic and Design - PowerPoint PPT Presentation


  • 276 Views
  • Uploaded on

An Object-Oriented Approach to Programming Logic and Design Chapter 4 Understanding Structure Objectives Describe the role of structure in object-oriented methods Draw flowcharts Understand unstructured spaghetti code Understand the three basic structures of sequence, selection, and loop

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'An Object-Oriented Approach to Programming Logic and Design' - albert


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
objectives
Objectives
  • Describe the role of structure in object-oriented methods
  • Draw flowcharts
  • Understand unstructured spaghetti code
  • Understand the three basic structures of sequence, selection, and loop
  • Build structured methods

An Object-Oriented Approach to Programming Logic and Design

objectives continued
Objectives (continued)
  • Use a priming read
  • Understand the need for structure
  • Recognize structure
  • Describe two special structures:
    • case
    • do until

An Object-Oriented Approach to Programming Logic and Design

the role of structure in object oriented methods
The Role of Structure in Object-Oriented Methods
  • Recall that classes can contain non-static instance methods such as
    • public get: to retrieve the object’s data
    • public set: to set values for the object’s data
    • Otherpublicmethods: to accomplish tasks using the object’s data

An Object-Oriented Approach to Programming Logic and Design

the role of structure in object oriented methods continued
The Role of Structure in Object-Oriented Methods (continued)
  • An application can also contain static methods
  • No object exists for these methods, but they may manipulate objects instantiated from prewritten classes
  • All methods should be structured methods

An Object-Oriented Approach to Programming Logic and Design

the role of structure in object oriented methods continued6
The Role of Structure in Object-Oriented Methods (continued)
  • A structured method:
    • Follows a specific set of rules for logical design
    • Has a single entry and exit point

An Object-Oriented Approach to Programming Logic and Design

drawing flowcharts
Drawing Flowcharts
  • Symbols on a flowchart:
    • Lozenge: racetrack-shaped; marks the beginning or end of a method
    • Annotation symbol: three-sided box attached to flowchart by a dashed line; provides details for a step
    • Processing statement: rectangle; contains an action taken
    • Input/output: parallelogram; denotes data coming in from or going out on a device

An Object-Oriented Approach to Programming Logic and Design

drawing flowcharts continued
Drawing Flowcharts (continued)

An Object-Oriented Approach to Programming Logic and Design

understanding unstructured spaghetti code
Understanding Unstructured Spaghetti Code
  • Spaghetti code: unstructured code that is confusing and complex due to poor structure
  • Flowchart of spaghetti code shows crossed flow paths with no clear path from beginning to end
  • Unstructured code is difficult to read, understand, and modify

An Object-Oriented Approach to Programming Logic and Design

understanding unstructured spaghetti code continued
Understanding Unstructured Spaghetti Code (continued)

An Object-Oriented Approach to Programming Logic and Design

three basic structures of sequence selection and loop
Three Basic Structures of Sequence, Selection, and Loop
  • Structure –a basic unit of programming logic
  • All programs are composed of 3 structures:
    • Sequence
    • Selection
    • Loop

An Object-Oriented Approach to Programming Logic and Design

three basic structures of sequence selection and loop continued
Three Basic Structures of Sequence, Selection, and Loop (continued)
  • Sequence structure: performs one or more actions in sequence, with no branching, skipping, or looping; continues step by step

An Object-Oriented Approach to Programming Logic and Design

three basic structures of sequence selection and loop continued13
Three Basic Structures of Sequence, Selection, and Loop (continued)
  • Selection structure: allows one of two or more alternative paths to be taken
    • if-then-else when only 2 alternatives

if hours-worked is more than 40 then

calculate overtimePay

else

calculate regularPay

An Object-Oriented Approach to Programming Logic and Design

three basic structures of sequence selection and loop continued14
Three Basic Structures of Sequence, Selection, and Loop (continued)
  • Loop structure: allows repetition or iteration
    • Asks a question and performs an action if a certain answer is given, then asks the question again.
    • Also called a while loop or a while-do loop

while testCondition continues to be true,

do someProcess

An Object-Oriented Approach to Programming Logic and Design

building structured methods
Building Structured Methods
  • All logic problems can be solved with only the sequence, selection, and looping structures
  • Structures can be stacked, one after another
  • Structures can be nested, one within another

An Object-Oriented Approach to Programming Logic and Design

building structured methods16
Building Structured Methods

An Object-Oriented Approach to Programming Logic and Design

building structured methods continued
Building Structured Methods (continued)

An Object-Oriented Approach to Programming Logic and Design

building structured methods continued18
Building Structured Methods (continued)
  • Block: group of statements that execute as a single unit
  • All pseudocode statements inside a block should be indented
  • Indentation in pseudocode reflects the logic shown graphically in a flowchart

An Object-Oriented Approach to Programming Logic and Design

building structured methods continued19
Building Structured Methods (continued)

An Object-Oriented Approach to Programming Logic and Design

building structured methods continued20
Building Structured Methods (continued)
  • endif always matches the most recent if that is not already matched
  • endwhile always matches with the most recent while that is not already matched

An Object-Oriented Approach to Programming Logic and Design

building structured methods continued21
Building Structured Methods (continued)

An Object-Oriented Approach to Programming Logic and Design

building structured methods continued22
Building Structured Methods (continued)
  • Each structure has 1 entry and 1 exit point
  • Other structures may attach only at an entry or exit point

An Object-Oriented Approach to Programming Logic and Design

using a priming read
Using a Priming Read
  • Priming read (or priming input): the first read or data input statement that occurs before and outside the loop that handles the rest of the input
  • Priming read is required to keep a method structured

An Object-Oriented Approach to Programming Logic and Design

using a priming read continued
Using a Priming Read (continued)

An Object-Oriented Approach to Programming Logic and Design

using a priming read continued25
Using a Priming Read (continued)
  • Rules for a structured loop:
    • You ask a question
    • If the answer indicates a task is to be performed, do so
    • After you perform the task, go back to ask the question again

An Object-Oriented Approach to Programming Logic and Design

using a priming read continued26
Using a Priming Read (continued)

Example: It’s structured, but doesn’t work!

An Object-Oriented Approach to Programming Logic and Design

using a priming read continued27
Using a Priming Read (continued)

Example: It works, but isn’t structured!

An Object-Oriented Approach to Programming Logic and Design

using a priming read continued28
Using a Priming Read (continued)

Example: It works, and it’s structured!

An Object-Oriented Approach to Programming Logic and Design

using a priming read continued29
Using a Priming Read (continued)

public void numberDoubling()

numeric userNumber

numeric answer

while userNumber not = 0

answer = userNumber * 2

print answer

input userNumber

endwhile

return

An Object-Oriented Approach to Programming Logic and Design

understanding the reasons for structure
Understanding the Reasons for Structure
  • Why structured methods are better:
    • Clarity: structured methods are less confusing
    • Professionalism: it is the expected standard in industry today
    • Efficiency: today’s programming languages are built expecting structure
    • Maintenance: easier to modify programs later
    • Modularity: facilitates reuse of modules

An Object-Oriented Approach to Programming Logic and Design

understanding the reasons for structure continued
Understanding the Reasons for Structure (continued)

An Object-Oriented Approach to Programming Logic and Design

understanding the reasons for structure continued32
Understanding the Reasons for Structure (continued)

An Object-Oriented Approach to Programming Logic and Design

recognizing structure
Recognizing Structure
  • Example: Is this flowchart segment structured?
  • Yes, it has a loop and a selection within the loop

An Object-Oriented Approach to Programming Logic and Design

recognizing structure continued
Recognizing Structure (continued)
  • Example: Is this flowchart segment structured?
  • No, it is not constructed from the 3 basic structures

An Object-Oriented Approach to Programming Logic and Design

recognizing structure continued35
Recognizing Structure (continued)
  • To untangle spaghetti code, pull one “strand” at a time and follow it
  • Let’s “untangle” this

flowchart 

An Object-Oriented Approach to Programming Logic and Design

recognizing structure continued36
Recognizing Structure (continued)

Untangling Example: Steps 1 & 2

An Object-Oriented Approach to Programming Logic and Design

recognizing structure continued37
Recognizing Structure (continued)

Untangling Example: Step 3 – the “No” side of Question B

An Object-Oriented Approach to Programming Logic and Design

recognizing structure continued38
Recognizing Structure (continued)

Untangling Example: Step 4 – the “Yes” side of Question B

An Object-Oriented Approach to Programming Logic and Design

recognizing structure continued39
Recognizing Structure (continued)

Untangling Example: Step 5 – left side of Question D: repeat step C to untangle it

An Object-Oriented Approach to Programming Logic and Design

recognizing structure continued40
Recognizing Structure (continued)

Untangling Example: Step 6 – right side of Question D leads to the end

An Object-Oriented Approach to Programming Logic and Design

recognizing structure continued41
Recognizing Structure (continued)

An Object-Oriented Approach to Programming Logic and Design

describing two special structures case and do until
Describing Two Special Structures: case and do until
  • case structure: allows more than 2 alternative paths
  • dountil loop: an alternative to the while loop
  • case and do until are not needed, but are convenient, acceptable structures

An Object-Oriented Approach to Programming Logic and Design

the case structure
The case Structure
  • Use a case when there are several distinct values for a variable, each of which requires a different action
  • case eliminates the need for a set of nested if statements

An Object-Oriented Approach to Programming Logic and Design

the case structure continued
The case Structure (continued)

Example: using nested if statements

An Object-Oriented Approach to Programming Logic and Design

the case structure continued45
The case Structure (continued)

Example: using a case statement instead

An Object-Oriented Approach to Programming Logic and Design

the do until loop
The do until Loop
  • do until loop is similar to the while loop, but tests the condition at the end of the loop
  • while loop tests the condition at the beginning of the loop; If the answer is no, the action is not performed
  • dountil loop performs the action, then tests the condition; the action is always performed at least once

An Object-Oriented Approach to Programming Logic and Design

the do until loop continued
The do until Loop (continued)

An Object-Oriented Approach to Programming Logic and Design

the do until loop continued48
The do until Loop (continued)
  • while loop pseudocode:

pay bills

while there are more bills to pay

pay bills

endwhile

  • do until loop pseudocode:

do

pay bills

until all bills are paid

An Object-Oriented Approach to Programming Logic and Design

the do until loop continued49
The do until Loop (continued)

Comparison: using a do until loop

An Object-Oriented Approach to Programming Logic and Design

the do until loop continued50
The do until Loop (continued)

Comparison: using a while loop

An Object-Oriented Approach to Programming Logic and Design

summary
Summary
  • Flowchart symbols:
    • Lozenge = method header and return
    • 3-sided box = Annotation symbol
    • Rectangle = statement
    • Parallelogram = input/output action
  • Spaghetti code is unstructured, hard to read and modify
  • Structured code uses 3 basic structures: sequence, selection, and loop

An Object-Oriented Approach to Programming Logic and Design

summary continued
Summary (continued)
  • Structures can be stacked and nested
  • Priming read – the first read or data input prior to beginning a loop
  • Structured techniques result in clarity, professionalism, efficiency, and modularity
  • if-then-else handles 2 path choices
  • case structure handles 2 or more path choices

An Object-Oriented Approach to Programming Logic and Design

summary continued53
Summary (continued)
  • while loop tests the condition first, then does the action; the action may be performed 0 or more times
  • dountil loop does the action, then tests the condition; it always performs the action at least once

An Object-Oriented Approach to Programming Logic and Design