Date post: | 19-Dec-2015 |
Category: |
Documents |
View: | 220 times |
Download: | 0 times |
An Object-Oriented Approach to Programming Logic and Design
Second Edition
Chapter 1
An Overview of Computers and Logic
An Object-Oriented Approach to Programming Logic and Design 2
Objectives
• Learn about computer components and operations
• Learn about the evolution of programming techniques
• Learn about the steps involved in the programming process
• Learn about flowcharts and pseudocode statements
An Object-Oriented Approach to Programming Logic and Design 3
Objectives (cont’d.)
• Create an application class with a main()method
• Use and name variables
• Assign values to variables
• Describe data types
• Learn about various forms of input
An Object-Oriented Approach to Programming Logic and Design 4
Understanding Computer Components and Operations
• Two major computer system components– Hardware
• Equipment or devices associated with the computer
– Software• Programs (sets of instructions) written by programmers
• Application software: applied to a task (word processing, spreadsheets, payroll, etc.)
• System software: manages computer resources
An Object-Oriented Approach to Programming Logic and Design 5
Understanding Computer Components and Operations (cont’d.)
• Four major hardware and software operations– Input– Processing– Output– Storage
• Input operation– A means for data to enter computer through an input
device• Mouse
• Keyboard
An Object-Oriented Approach to Programming Logic and Design 6
Understanding Computer Components and Operations (cont’d.)
• Processing data operation– Organizing – Checking for accuracy– Performing mathematical operations– Tasks occur in central processing unit (CPU)
• Using machine instructions
– Software • Provides machine instructions to CPU
• Written using programming languages
An Object-Oriented Approach to Programming Logic and Design 7
Understanding Computer Components and Operations (cont’d.)
• Output operation– A means to view, print, interpret, store data using
output devices• Printer
• Monitor
– Hardcopy• Printed computer output
– Softcopy• Screen output
An Object-Oriented Approach to Programming Logic and Design 8
Understanding Computer Components and Operations (cont’d.)
• Storage operation– Storing output information on storage device
hardware• Magnetic disks, compact discs, USB drives
• Computer software– Instructions controlling data manipulation
• Data input
• Data processing
• Data output or storage form
– Gives hardware a purpose
An Object-Oriented Approach to Programming Logic and Design 9
Understanding Computer Components and Operations (cont’d.)
• Obtaining programmer’s instructions (software)– Buying prewritten software stored on – Downloading software from the web– Write your own software instructions
• Enter instructions through hardware devices
• Store on storage device (disk, CD)
An Object-Oriented Approach to Programming Logic and Design 10
Understanding Computer Components and Operations (cont’d.)
• Programming languages– Common languages
• Visual Basic, C#, C++, COBOL, RPG, Fortran, JAVA
– Programmers may specialize
• Syntax– Rules governing usage and punctuation– Must be perfect
An Object-Oriented Approach to Programming Logic and Design 11
Understanding Computer Components and Operations (cont’d.)
• Machine language– Circuitry consisting of millions of on-off switches– Computer’s on-off circuitry language
• Compilers and interpreters – Language translation software– Identify correct/incorrect language use
• Compilers – Create and store reusable machine instructions
• Interpreters – Interpret programming language at run-time
An Object-Oriented Approach to Programming Logic and Design 12
Understanding Computer Components and Operations (cont’d.)
• Two programming language errors– Syntax errors
• Incorrect spelling
• Incorrect structure of language elements
– Semantic (logic) errors• Incorrect instructions
• Incorrect order of instructions
• Logic– Providing computer instructions in a specific
sequence
An Object-Oriented Approach to Programming Logic and Design 13
Understanding Computer Components and Operations (cont’d.)
• Logic (cont’d.)– Example: a logic error in cake-making instructions
• Stir
• Add two eggs
• Add a gallon of gasoline
• Bake at 350 degrees for 45 minutes
• Add three cups of flour
• Run or executed – Occurs after program written and compiled
successfully
An Object-Oriented Approach to Programming Logic and Design 14
Understanding Computer Components and Operations (cont’d.)
• Program instructions example– Written in English-like language– Three operations (input, processing, output)
Get inputNumber.
Compute calculatedAnswer as inputNumber times 2.
Print calculatedAnswer.
An Object-Oriented Approach to Programming Logic and Design 15
Understanding the Evolutionof Programming Techniques
• Storage categories– Internal and external needed by computer system– Internal storage
• Memory, main memory, primary memory
• Volatile main memory: lost when power turned off
• Inside machine
• Most often discussed in book
An Object-Oriented Approach to Programming Logic and Design 16
Understanding the Evolutionof Programming Techniques (cont’d.)
• Storage categories (cont’d.)– External storage
• Persistent (relatively permanent) storage
• Outside main memory (not necessarily outside computer)
• Exists in devices– Disk drives: hard and floppy– Flash media– Magnetic tape
An Object-Oriented Approach to Programming Logic and Design 17
Understanding the Evolutionof Programming Techniques (cont’d.)
• Program execution– Program copy stored in main memory– Required data must be in memory location
• Program calls location to obtain data
• Processing occurs
• Output displayed
An Object-Oriented Approach to Programming Logic and Design 18
Understanding the Evolutionof Programming Techniques (cont’d.)
• Modern computer programs– Around since 1940s
• Oldest programming languages– Programmers worked with memory addresses– Memorized awkward codes associated with machine
languages– Written as one piece
An Object-Oriented Approach to Programming Logic and Design 19
Understanding the Evolution of Programming Techniques (cont’d.)
• Newer programming languages– Look like natural language– Easier to use– Reasons for ease of use
• Use meaningful names for memory locations
• No awkward memory addresses
• Allow creation of self-contained modules or program segments
• Can be pieced together
An Object-Oriented Approach to Programming Logic and Design 20
Understanding the Evolution of Programming Techniques (cont’d.)
• Program development techniques– Procedural programming
• Procedures created to manipulate data
• Actions carried out (getting input data for an employee and writing the calculations)
• Breaks process into manageable subtasks
An Object-Oriented Approach to Programming Logic and Design 21
Understanding the Evolution of Programming Techniques (cont’d.)
• Program development techniques (cont’d.)– Object-Oriented programming (OOP)
• Objects, attributes, behaviors, states
• Attributes: object feature
• Behaviors: what object does
• States: set of all values of attributes
• Describes objects’ features or attributes
An Object-Oriented Approach to Programming Logic and Design 22
Understanding the Evolution of Programming Techniques (cont’d.)
• Program development techniques (cont’d.)– Differences
• Programmer focus area in early stage
• Object-oriented approach application types– Computer simulations
• Create a model
– Graphical user interfaces• Users interact with program in graphical environment
• Natural use for object orientation
An Object-Oriented Approach to Programming Logic and Design 23
Understanding the Programming Process
• Computer software system development
• Three object-oriented approach tasks– Object-oriented analysis (OOA)
• Analyzing the system
– Object-oriented design (OOD)• Designing the system
– Object-oriented programming (OOP)• Writing the programs
An Object-Oriented Approach to Programming Logic and Design 24
1. Understanding the Problem
• Programmer– Write programs to satisfy others needs– Provide services to users
• First understand what users want
• Well designed program– Steps might involve a short meeting
• Poorly designed program– Steps might take many meeting hours
• Thoroughly understanding problem– A most difficult aspect of programming
An Object-Oriented Approach to Programming Logic and Design 25
2. Envisioning the Objects
• Programmer – Envisions objects needed– Writes programming language instructions to create
objects• Distinct steps
– Data modeling• Identify all manipulated objects and relations
– Create class• General object category
– Establish communication means• Between objects, user
An Object-Oriented Approach to Programming Logic and Design 26
3. Planning the Logic
• Heart of programming process
• Programmer– Plans program steps – Decides steps to include and order
• Leads from available input to desired output
– Plans the solution: two tools• Flowcharts and pseudocode
• Both use English-like code
– Language syntax not a concern
An Object-Oriented Approach to Programming Logic and Design 27
3. Planning the Logic (cont’d.)
• Think about all possible data values program might encounter– How to handle each scenario
• Desk-checking– Walking through program’s logic on paper
• Before writing the program
• Focus of course
An Object-Oriented Approach to Programming Logic and Design 28
4. Coding the Program
• Write class definitions– Descriptions data in each class– Operations performed with data in each class
• Object-oriented languages– C++, C#, Java, Visual Basic, Smalltalk, OO COBOL,
Simula– Similarity
• All create objects and establishing communication
An Object-Oriented Approach to Programming Logic and Design 29
4. Coding the Program (cont’d.)
• Program object and logic needed to work with the objects – Executed using any number of languages
• Syntax (correct spelling and punctuation )– Concern once language chosen
• Experienced programmers– Combine planning and program coding in one step
An Object-Oriented Approach to Programming Logic and Design 30
5. Translating the Program Code
• Many programming languages• Computer knows one language
– Its machine language consisting of many 1s and 0s• Translator programs (compilers or interpreters)
– Change English-like high-level programming into low-level machine language
• Executable program
• Syntax error – Issues when translator cannot translate the code– Must be corrected to allow compilation to complete
An Object-Oriented Approach to Programming Logic and Design 31
5. Translating the Program Code (cont’d.)
Figure 1-1 Creating an executable program.
An Object-Oriented Approach to Programming Logic and Design 32
6. Testing the Program
• Syntax error free program – Not necessarily free of logical errors– Must test for logical errors
• Requires entering of sample data– Select test data carefully– Error discovery may require changes
• To program logic
• To actual object design
• Process resembles syntax error correction cycle
An Object-Oriented Approach to Programming Logic and Design 33
7. Putting the Program into Production
• Program adequately tested – Ready for organizational use
• Putting program into production– Time to implement
• Can be short or long
• Depends on program nature, size and user needs
– Factors• Training, data format changes, new platform causing
users to make choices in unanticipated combinations
An Object-Oriented Approach to Programming Logic and Design 34
7. Putting the Program into Production (cont’d.)
• Non working program may cause significant monetary loss
• Testing before deployment must be thorough
• Conversion– Entire set of actions organization takes to switch over
to new program (set of programs)• May take months or years to accomplish
An Object-Oriented Approach to Programming Logic and Design 35
8. Maintaining the Program
• Maintenance– Process of making required changes
• Reasons – New legislated tax rates– Input file format alterations– End user requires additional information
• Not included in original output specifications
An Object-Oriented Approach to Programming Logic and Design 36
9. Retiring the Program
• Final programming process step
• Program no longer needed by organization
• New program in process of going into production
An Object-Oriented Approach to Programming Logic and Design 37
Using Flowcharts andPseudocode Statements
• Flowchart– Pictorial representation of logical program steps – Program steps in boxes
• Connected with flowlines (arrows)• Shows processing order
• Pseudocode– English-like representation of logical program steps– Looks like programming language (is not)
• Important part of permanent documentation– Design and operation of software system
An Object-Oriented Approach to Programming Logic and Design 38
Using Flowcharts andPseudocode Statements (cont’d.)
Figure 1-2 Flowchart of number-doubling problem
An Object-Oriented Approach to Programming Logic and Design 39
Creating an Application Class with a main() Method
• Purely object-oriented programming languages – Every statement (application) part of class – “Pure View”
• Application– Program accomplishing some task
• Application pseudocode– Begins with word class – Ends with word endClass
• No object-oriented programming language uses endClass
• Used here for clarity
An Object-Oriented Approach to Programming Logic and Design 40
Creating an Application Class with a main() Method (cont’d.)
• Method– Set of statements performing task or group of tasks– Where program actions take place
• If class contains one executable method – Method named main()
• Identifier– Name of programming object
• Class, method, or variable
An Object-Oriented Approach to Programming Logic and Design 41
Creating an Application Class with a main() Method (cont’d.)
Figure 1-3 Flowchart and pseudocode for main() method that prints “Hello”
An Object-Oriented Approach to Programming Logic and Design 42
Creating an Application Class with a main() Method (cont’d.)
• Specific programming languages – Have rules for identifier structure
• Book pseudocode rules– Identifier names
• One word• Letters, digits, hyphens, underscores• No spaces
– Identifiers should have appropriate meaning– Compound words allowed
• Example: Helloclass
An Object-Oriented Approach to Programming Logic and Design 43
Creating an Application Class with a main() Method (cont’d.)
• main() statement– Method header
• Class containing main() method– An executable program
• Every method ends with return statement• main() method action statements
– Placed between main() method header and return statement
An Object-Oriented Approach to Programming Logic and Design 44
Creating an Application Class with a main() Method (cont’d.)
Figure 1-4 The Hello class written in the Java programming language
Figure 1-5 The Hello class written in the C# programming language
An Object-Oriented Approach to Programming Logic and Design 45
Using and Naming Variables
• Variables (fields)– Memory locations – Contents can change over time– Holds just one value at any given time
• Most objects in programs contain variables holding objects’ attributes
• Same rules for naming variables as naming classes
An Object-Oriented Approach to Programming Logic and Design 46
Using and Naming Variables (cont’d.)
Table 1-1 Valid and invalid variable names for an employee’s last name
An Object-Oriented Approach to Programming Logic and Design 47
Assigning Values to Variables
• Assignment statement– Statement assigning values to variables– Example: calculatedAnswer = inputNumber * 2
• Assignment operator– Example: equal sign– Requires memory location name on left side where
result stored
• Named constant– Named memory location (similar to variable)
• Value never changes during program execution
An Object-Oriented Approach to Programming Logic and Design 48
Assigning Values to Variables (cont’d.)
• High-level computer language– Programmers pick reasonable “English” name for
memory address• Computer keeps track of memory location
– Mathematical operations• Performed on memory address contents
– Operator precedence rules• Multiplication and division: precedence over addition
and subtraction
• Assignment operator has very low precedence
An Object-Oriented Approach to Programming Logic and Design 49
Understanding Data Types
• Basic data types: text and numeric
• Numeric constant (43)– Specific numeric value– Does not change– Digits with no quotation marks– Used for numeric calculations
• String constant (“chris”)– Specific character set enclosed in double quotes
• Character constant: single quotes (‘C’)
An Object-Oriented Approach to Programming Logic and Design 50
Understanding Data Types (cont’d.)
• Two distinct variable types– Numeric variable
• Holds a number
– Character variables• Hold single character
• String variables hold group of characters
– Distinguish between numeric and character• Computers handle two data types differently
– Language syntax rules define data type to expect• Done differently in every language
An Object-Oriented Approach to Programming Logic and Design 51
Understanding Data Types (cont’d.)
• Variable declaration statement– Contains data type and identifier
• Initializing a variable– Setting initial starting variable value– Popular languages (C++, C#, java, Visual Basic)
• Distinguish integer (whole number) numeric variables and floating-point (fractional) numeric variables containing decimal point
• Three broad types of variables– Numeric, character, string
An Object-Oriented Approach to Programming Logic and Design 52
Understanding Data Types (cont’d.)
• Garbage value– Uninitialized variable containing unknown value
• Default value– Automatic values for uninitialized variables
• Text or character data handled differently from numeric data (every computer)
• First step in writing programs– Identifying variables correctly
• Numeric, character, string
– Any programming language
An Object-Oriented Approach to Programming Logic and Design 53
Understanding Data Types (cont’d.)
Table 1-2 Some examples of legal and illegal assignments
An Object-Oriented Approach to Programming Logic and Design 54
Figure 1-6 Flowchart and pseudocode for the DoubleNumber class
An Object-Oriented Approach to Programming Logic and Design 55
Understanding VariousForms of Input
• Simple programs require simple data– Data values often entered from keyboard
• Users presented with a prompt
• Message on monitor asking for user response
– User interface • User’s means of interacting with the computer
– Command prompt• Input area on computer screen
• User can type text that communicates with computer’s operating system
An Object-Oriented Approach to Programming Logic and Design 56
Understanding VariousForms of Input (cont’d.)
Figure 1-7 A prompt followed by a response and output in a command-line environment
An Object-Oriented Approach to Programming Logic and Design 57
Understanding VariousForms of Input (cont’d.)
Figure 1-8 Flowchart and pseudocode for prompt and response in number-doubling program
An Object-Oriented Approach to Programming Logic and Design 58
Understanding VariousForms of Input (cont’d.)
• Text environment– Many programs do not run at command prompt – Run using GUI
• Typically contains graphical controls (buttons and text boxes)
• User can manipulate with a mouse or other pointing device
An Object-Oriented Approach to Programming Logic and Design 59
Understanding VariousForms of Input (cont’d.)
Figure 1-9 A prompt, a response, and output in a GUI environment
An Object-Oriented Approach to Programming Logic and Design 60
Understanding VariousForms of Input (cont’d.)
Figure 1-10 Flowchart and pseudocode for text box response in number-doubling program
An Object-Oriented Approach to Programming Logic and Design 61
Summary
• Hardware and software accomplish four major operations: input, processing, output, and storage
• Computer programming languages used to write machine instructions– Requires specific syntax
• Procedural programming focuses on procedures programmers creates to manipulate data
• OOP focuses on objects– Describes features or attributes and their behaviors
An Object-Oriented Approach to Programming Logic and Design 62
Summary (cont’d.)
• Programmer – Identifies objects and classes, codes program,
translates into machine language, tests, puts into production
• Flowcharts and/or pseudocode used to plan programming solution logic
• Program actions occur within a method
• Variables – Named memory locations, contents can vary– Choose meaningful variable names
An Object-Oriented Approach to Programming Logic and Design 63
Summary (cont’d.)
• Assignment always takes place from right to left
• Variable declaration tells computer type of data to expect
• Numeric, character, and string variables handled differently by computer
• User input data – Text or graphical environment– File stored alone or in database