Title: Programming
1Programming
2Programming Overview
- Problem Solving Perspective
- Pseudocode and Algorithms
- Programming Design Process
- Flowcharting
- Control Logic
- Languages (Object Oriented)
- Visual Basic
- Object Oriented Concepts
- Function and Procedures
- Conditional and Iterative processing
3Programming is Problem Solving
- Informal - have incomplete specification
- Formal - have complete specification
- initial conditions
- specified results
- specified allowable actions
Problem Solving
Informal
Formal
4Four Problem Solving Steps
Problem Solving
- Understand the Problem
- Devise a Plan
- Carry out the Plan
- Look Back
Informal
Formal
Understand
Plan
Execute
Review
5Programming is Problem Solving
- To create computer programs, programmers must
decompose the problem and then recompose a
solution.
Analysis Synthesis
6Problem Solution
- A well-defined set of specified actions that when
enacted, produce a desired result from a set of
initial conditions.
Actions
Initial Conditions
Result
7Problems Have Levels of Solution
- Graduate from UCCS
- Meet Degree Reqs.
- Total Units
- Cultural Diversity
- Approve Major
- Course of Study
- list of INFS courses
- select ethnic and non-western
- electives
General
- Plan of Action
- Strategic Plan
- Tactical Action Plan (Algorithm)
Specific
8Algorithm
- Defined finite set of unambiguous instructions,
that when given a set of input values, produces
the desired outputs, then stops. - Examples What are some examples of algorithms
from everyday life? Think History!? Science!?
9Pseudocode
- Algorithms contain Pseudocode that combines
characteristics of code and characteristics of
language (English) - Usually is written in declarative
- Should be unambiguous
- Example
- Open input file named employ.dat
- move data from a file into memory
10Algorithm Example
- Write an algorithm to instruct a person to add a
list of four numbers (3, 5, 1, 2). - Allowable actions are
- ADD - adds two numbers together to create a third
number or sum - STORE - allows a person to remember a number
- REMEMBER - allows a person to recall the last
number STORED - Desired outcome is a sum of the four numbers
given.
11Specific Result
- ADD 3 and 5, STORE
- ADD 1 and REMEMBER, STORE
- ADD 2 and REMEMBER, DISPLAY
12Variables Defined
- Variables are named memory locations which hold
data for programs - Refer to the name and your refer to the data
stored in that memory at the time of reference - Usually variable names are chosen to describe the
data to be stored in that memory location
13Variable Example
- The car in parking lot space A-16 refers to the
car currently parked in space A-16, if any. - Occupant or Resident mail
- Pick a number between 1 and 10
- Think of a playing card
14In ClassVariable Example
- Write down
- Two proper names
- a location
- two colors
15In ClassVariable Example
- Write down
- Two proper names
- a location
- two colors
- PROPER-NAME-1 and PROPER-NAME-2 went over to
LOCATION-1. After a few drinks of the special
tonic, a large COLOR-1 and COLOR-2 rabbit
introduced himself as Harvey. - How would you use variables in the algorithm to
add four numbers?
16More Generic (add 4 numbers)
- ADD first-item to second-item, STORE
- ADD third-item to REMEMBER, STORE
- ADD fourth-item to REMEMBER, DISPLAY
Memory
17More Generic (add 4 numbers)
- ADD first-item to second-item, STORE
- ADD third-item to REMEMBER, STORE
- ADD fourth-item to REMEMBER, DISPLAY
STORE
Memory
18More Generic (add 4 numbers)
- ADD first-item to second-item, STORE
- ADD third-item to REMEMBER, STORE
- ADD fourth-item to REMEMBER, DISPLAY
REMEMBER
Memory
19More Generic (add 4 numbers)
- ADD first-item to second-item, STORE
- ADD third-item to REMEMBER, STORE
- ADD fourth-item to REMEMBER, DISPLAY
STORE
Memory
20More Generic (add 4 numbers)
- ADD first-item to second-item, STORE
- ADD third-item to REMEMBER, STORE
- ADD fourth-item to REMEMBER, DISPLAY
REMEMBER
Memory
21More Generic (add 4 numbers)
- ADD first-item to second-item, STORE
- ADD third-item to REMEMBER, STORE
- ADD fourth-item to REMEMBER, DISPLAY
22Even More Generic
- Initial
- Make sure data list is there
- Make sure I can report the information
- Make space for variables SUM, DATA-ITEM
- Process
- set SUM to 0
- while there is data left
- READ the next item in data list STORE it in
DATA-ITEM - ADD DATA-ITEM to SUM
- Wrap-up
- WRITE The sum of numbers is , SUM
- Stop
23Practice Algorithm
- Suppose you have a handcounter (like those used
by movie ticket ushers) and a basic stopwatch.
You have been positioned on the corner of Austin
Bluffs and Nevada and have been instructed to
count the number of blue cars which pass you
within the next hour. - Write an algorithmic solution to this problem.
24Traffic Counter Thoughts
- Initial Conditions
- Selective Conditions
- Iteration
- Assumptions
- Reset counter
- Reset stopwatch
- Is it blue?
- Is it under one hour
- Each time blue car passes
- On 2nd and Hazel
25Programming is Part Art and Part Science
Science
Art
26Programming is a Design Process
- What are some questions with which to start
designing your program.
27Generic Design Questions
- Describe the Input
- What should the output look like?
- Is there a loop?
- What are the tasks in the loop?
- Major tasks subtasks
- What controls the loop?
- What must be done before the loop?
- What must be done after the loop?
28Program Design Shoulds
- Use a planning tool
- Structured
- Designed Top-Down
- Be Modular
29Programming is a Development Process
- Development Cycle
- Review Specifications
- Design
- Informal design
- Formal design
- Code and Compile
- Test
- Maintain
30Review Specifications
Input
Processing
Output
Describe the Input, Output Can you get the output
from the input?
31Informal Design
Tasks
1.0 1.1 1.2 1.3 2.0 2.1 2.1.1
2.1.2 3.0
- Decompose the Processing
- List main tasks
- List subtasks to main tasks
- The ultimate goal is to
- decompose the tasks into simple, obvious,
do-able actions. These actions will be turned
into code.
32Formal Design
- Create formal design
- convert generic tasks into pseudocode
- use flowcharts and system charts
- Desk check
- play computer
- confirm the logic
33Code
- Convert the pseudocode or Flowchart to Code
- Compile the program source code
- Simple step but the one on which most people
spend too much time.
34Test Maintain
- Usually have sets of test data to run against the
program to make sure all potential problems are
found. - Test your output against known answers.
- Once tested, code moves into production. There it
is used until problems are found which puts
requirements into the maintenance cycle
35Type of Errors
- Syntax errors occur when the Compiler cannot
understand the Source Code. - Runtime errors occur when the computer executes
an illegal instruction. - Logic errors occur when there are neither syntax
nor runtime errors but the executed statements
create invalid (incorrect) output.
36Fundamental Data Types
- Integer
- Real
- Character
- Boolean
- Scalar
- Composite
37Hierarchy of Languages
Example
Class
- Non-procedural
- High level
- Assembly
- Machine
- Visual Basic, SQL
- Qbasic, Pascal, COBOL, C
- IBM 360/370 Assembler
- Machine
38Language Equivalents
COBOL COMPUTE TOTAL W X Y
Pascal Z WXY
Assembler L 3,X
M 2,Y A 3,W
ST 3,Z
Which would you rather learn?
Machine 41 B0C1A4
3A 20C1A8 1A 30C1A0
50 30C1A4
39Flowcharting
- The purpose of flowcharting a program is to
diagram the logic contained in the program.
40Lifes Problem Solving Flowchart
Does it Work?
Did You Mess With it
Will you get in trouble
NO
NO
NO
Do Others Know
YES
YES
You Dummy
YES
NO
YES
Poor Guy/Gal
Do Not mess with it
Can You Blame Someone Else
NO
No problem
YES
41Flowcharts
- One answer to the Design questions
System Flowchart
Program Flowchart
42System Flowcharts
- Program
- Data File
- Report
- Data Flow
43High Level System FlowchartInput - Processing -
Output(IPO)
Report
Input Data
Computer Processing
Output data
44Flowchart Symbols
- Process or Action
- Decision
- Terminate
- Direction of Logic
T
?
F
45Flowchart Symbols
- Subroutine (Paragraph)
- Junction
- Input or Output
46Flowchart Rules
- Actions have one input and one output
- Terminator have one input OR one output
- Decisions have one input and TWO output
- Iterations should loop back to a junction
- Flowchart logic should run top to bottom and left
to right - Label Input-Output
- Show direction of logic with arrowheads
47Control Logic
- Sequential
- walk to Austin Bluffs
- turn right
- walk to Nevada
- Selection
- If the car is blue, then press the counter
- Iteration
- Until an hour is up, count the cars
- Case (special example of Selection)
- Based on the type of coin, add an amount to
subtotal
48Sequential Flowchart
- Sequential
- walk to Austin Bluffs
- turn right
- walk to Nevada
49Selection Flowchart
- Selection
- If the car is blue, then press the counter
Car is Blue?
True
False
Null
Press Counter
50Conditional PracticeAre these statements
logically the same?
- If AB then
- If CD then
- Add 1 to X
- Else
- Add 1 to Y.
-
- If (AB) And (CD) Then
- Add 1 to X
- Else
- Add 1 to Y.
Flowchart the logic in both.
Practice with following data 1.) A5 B1
C10 D15 2.) A1 B5 C10 D15
51Iteration Flowchart(COBOL UNTIL clause)
- Iteration
- Until an hour is up, count the cars
Hour Is Up?
False
Count Cars
True
52Case Flowchart
- Case (special example of Selection)
- Based on the type of coin, add an amount to
subtotal
Dime ?
Add .10
Penny ?
Add .01
Nickel ?
Add .05
Qtr ?
Add .25
53F
XY
T
T
F
RS
T
F
TV
ADD 1 TO G
ADD 1 TO J
ADD 1 TO H
ADD 1 TO K
True or False
For Class Discussion
A. IF XS then always add 1 to H B. If
X
RS and TV then always add 1 to H D. IF XY then
always add 1 to G E. IF XS and T
always add 1 to K
List ALL conditions necessary so that the program
will A. ADD 1 to G B. ADD 1 to K
Page 78 Grauer - A COBOL Book of Practice and
Reference
54Programming EnvironmentOverview
- Programming Languages
- procedural vs. event driven
- Object Oriented Concepts
- Basics
- examples
- Visual Basic Specifics and Demo
55Programming Languages
- Common Concepts
- Deal with a language
- specified actions - pre-determined verbs
- pre-determined and user defined variables
- language format - format for statements
- Source Code
- Match language syntax
- Errors in syntax are usually compile errors
- Object Code
- Match programs environment
- Errors in running object code are usually runtime
56Program Development Process
MINS114 VB Working Model
Edit
Source Code
Compile
Output
Link (Animate)
Object Code
Executable
57Source to Executable
Source Code
Compiler
Linker
Executable Code (Object)
58Compiler Discussion
- Source Code can be read by humans
- Compiler translate Source into Object Code
- Linkers add the environment specific information
to Object Code to make it Executable. - Computers understand Object Code and execute the
instructions verbatim
59Programming Languages (cont)
- Procedural
- logic is decomposed into smaller parts
- paragraphs, procedures, functions
- next step in program is predetermined
- heavily data dependent
- Event Driven
- logic is decomposed into smaller parts
- events, subprocedures
- next step in program is user driven
- heavily process dependent
60Programming Languages (cont)
Procedural
Event Driven
Time
BASIC, C, COBOL, Small Talk,
C, FORTRAN Java
61Programming Languages (cont)
Procedural
Event Driven
Visual Basic
Time
BASIC, C, COBOL, Small Talk,
C, FORTRAN Java
62Object Oriented Concepts
- Basic idea is that the real world consists of
concrete and conceptual entities, including
things, relationships and occurrences. - Wanted
- to be able to reuse code
- to respond to expanding user base
- to better model processes
- Take advantage of some evolving concepts
- GUI
- event programming
- relationships
- inheritance
63Four Main OO Concepts
- Classes - categories of things trees, TVs
- Objects (instance of a Class) - an instance or
example of a Class oak tree, SONY P85 - Inheritance - a property gained because of the
IS-A relationship oak tree inherits the property
of trees because OAK TREE is-a TREE - Polymorphism - an action is taken by the class
based upon data passed to it if a tree had an
action CHANGE_COLOR, then OAK_TREES would receive
a YES and EVERGREENS would receive a NO.
64Object Oriented Concepts
Three basic compartments of class diagram
- Class Name
- things, nouns
- is-a relationship
- Properties
- adjectives
- has-a relationship
- Methods
- verbs
- actions object can perform
Name Compartment Attribute Compartment Method
Compartment
65Object Oriented Concepts
Class DOG OBJECT
Instance of Class DOG
Name DOG has-a NAME has-a BREED has-a
COAT-COLOR BITES() BARKS()
Name MYDOG has-a NAME SKIPPY has-a BREED
LAB has-a COAT-COLOR
Black BITES() BARKS()
- What did the instance inherit? Why?
66Object Oriented Concepts
Class FORM Object
Instance of Class FRAME
Name Form has-a NAME has-a BACKCOLOR has-a
CAPTION Command1 () Command2 ()
Name frmMyform has-a NAME Main has-a BACKCOLOR
GRAY has-a CAPTION
Entry Form COMMAND(EXIT) COMMAND(DISPLAY)
- What did the instance inherit? What is
significant about COMMAND
67Object Oriented Concepts
- Objects can inherit other objects!!!
frmMyform
This starts to recognize the goal of reusable
code. Can you think of examples from the real
world of objects and inheritance.?
cmdExit
cmdDisplay
68Visual Basic Review
- Visual Basic Projects are Objects!!!
- Visual Basic is event driven
- Visual Basic programming environment
- three main states
- main elements
- Demo Program
69Visual Basic is event driven
- Processor waits for user to trigger an event (or
command), then executes the code associated with
that event and (if not stopped) waits for the
user to trigger the next event.
User
Event Processor
Event 1 Event 2 Event 3
70Visual Basic Programming Environment
- Three main states or modes
- Design build interface add code set
attributes - Run execute software test
- Break debug errors found
- Files
- VBP - OCX
- FRM - VBW
- BAS
71VB Demo Specifics
- IDE Interface
- (Integrated Development Environment)
- Some VB Objects
- Some VB Functions
- Conditions
- Simple
- Complex
- Loops
72VB 6.0 Environment
73Textbox
- Use when you want user input
- prefix txt
- Properties
- Text actual text
- Multiline true, false
- Methods
- .Change
- Example
- txt.Message.Text This is my story.
74Checkbox
- Presents list of options for user
- prefix chk
- more than one choice may be checked
- Properties
- .Caption actual text
- .Value
- 0 unchecked
- 1 checked
- 2 unavailable (grayed out)
75Option Button
- Presents list of options to user
- prefix opt
- choices are mutually exclusive - highlander
- Properties
- .Caption actual text
- .Value true/false
76Frame
- Associates sets of checkboxes and option buttons
- prefix fra
- Properties
- .Caption actual text
- HINT If you want to associate option buttons,
then draw and place frame before instantiating
the buttons. Options within a frame are local to
that frame.
77Functions and Procedures
- Program code module that perform activities on a
set of data. In general - the program code is given a meaningful name
- the set of data is referenced in arguments
- are classified as Procedures or Functions
- Functions take actions and return values for
assignment - Procedures take actions but dont return values
78Functions and Procedures
- Function (Returns a value)
- MAX(C1..C10)
- Procedure (Performs an operation)
- SWAP (X,Y)
- Procedure SWAP(A,B)
- DIM Temp
- MOVE A to TEMP
- MOVE B to A
- MOVE TEMP to B
79Functions and Procedures
- VB Functions
- Val Function - converts text to numeric for
assignment to numeric variable - Generally numeric type Val(text type)
- Example intQty Val(txtQty.Text)
- Rules for conversion start on left of text and
convert as long as it makes sense. - 14B30 14
- B14 0
- -123 -123
- 12.34.56 12.34
80Arithmetic Operations
- - /
- precedence
- parenthesis
- exponential
- Multiply or Divide
- Add or Subtract
- Assume x 2 y 4 z 3, what is
- x y z 2
- x (y z) 2
- (x y) z 2
- (x y) (z 2)
81IF Format
- If (condition-1) Then
- imperative statement(s)
- ElseIf (condition-2)
- imperative statement(s)
- Else
- imperative statement(s)
- End If
Pg. 125
82IF Format (Base)
- If (condition-1) Then
- imperative statement(s)
- End If
WARNING Acceptable to computer Not acceptable
in MINS114
Pg. 125
83IF Format (Minimum Acceptable)
- If (condition-1) Then
- imperative statement(s)
- Else
- imperative statement(s)
- End If
True Side False Side
More than ones statement called a Block
84Conditionals
- Some condition must be true before the specified
action may be taken. - Examples
- If the coast is clear, then gun it.
- If you see a blue car, press the counter.
- If we have a quiz on Thursday, then read the book
Wednesday night
85Conditionals
- Assume that if a customer has purchased more than
1000 dollars, then we will discount their bill
10. - What is the condition?
- What is in the true Block?
- What is in the false Block?
- How would one flowchart this business rule?
86Conditionals
Amt-in 1000
Include Do nothing branches in pseudocode and
code for clarity.
F
T
Do Nothing
Calculate Discount
87Condition(s)
- True or False conditions are created
- when comparing variables with relational
operators - by evaluating a boolean variable
- Relational Operators
- Greater than Less than
- Equal to
Not equal to - Greater than or equal to than or equal to
- Valuation
- numbers are numbers
- Letters are converted to numeric equivalents
(ASCII)
88Condition(s)
- Numbers
- 10 9.5 6 0 -5 -14
- Letters
- A
- Numbers are less than Letters
- 123
- Where does a space fit in?
- How do we convert from text to numeric?
- How do we convert from numeric to text?
89Condition(s)
- Comparing Numerics
- intAlpha 5 intBeta 4 intZeta -2
- T/F intAlpha intBeta
- T/F (intAlpha intBeta)
- T/F intAlpha 5
- T/F intAlpha intBeta
- Comparing Strings
- txtAlpha.text alpha strBeta
Beta lblCharlie.Caption 300 - T/F txtAlpha.text
strBeta - T/F strBeta
lblCharlie.Caption - T/F txtAlpha lblCharlie
90Condition(s)
- Comparing Strings is Case Sensitive
- txtAlpha.text alpha strBeta
ALPHA lblCharlie.Caption Alpha - T/F txtAlpha.text
strBeta - T/F strBeta
lblCharlie.Caption - T/F txtAlpha lblCharlie
- T/F
UCase(txtAlpha.text) UCase(strBeta) - Booleans
- Boolean variables use .Value property as default.
These are all the same (Only shortcut that is ok
to use in programs) - If (blnTF.Value True) Then
- If (blnTF True) Then
- If (blnTF) Then
- If blnTF Then
91Condition(s)
- Comparing Numerics and Strings (DONT)
- intAlpha 300 lblCharlie.Caption 300
- T/F intAlpha lblCharlie.Caption
- lblAlpha.Caption 300 lblCharlie.Caption
300 - T/F intAlpha lblCharlie.Caption
- lblAlpha.Caption 300 lblCharlie.Caption
-300 - T/F lblAlpha.Caption lblCharlie.Caption
- T/F Val(lblAlpha.Caption)
Val(lblCharlie.Caption)
Hint put apostrophes around strings to visually
reinforce the difference between numerics and
strings
92Compound Conditions
- You may join separate conditions with the AND
and the OR to create compound conditions - The AND is data reducing it reduces choices that
fit - The OR is data expanding it increases the
choices that fit
Blue Cars AND Four Doors is
????? Blue Cars OR Four Doors is
?????
Blue Cars
A B C
Four Doors
93Compound Conditions
Union Both
Alternation Either
And
F
T
T
F
Or
T
T
T
T
F
T
F
F
T
F
F
F
94Compound Conditions
- Evaluate Negations (NOTs) first
- Evaluate ANDs left to right
- Evaluate ORs after all ANDs left to right
An individual can be either Single, Married or
Divorced. The programmer wants to run a married
routine when the individual is married and an
unmarried routine otherwise. Does either set of
logic do it?
If NOT (Single OR Divorced) Call
MarriedRtn() Else Call UnmarriedRtn() End If
If NOT Single OR NOT Divorced Call
MarriedRtn() Else Call UnmarriedRtn() End If
95Compound Conditions
- Order to evaluate
- ANDs before ORs
- Given
- If curSale 1000 Or optDis.Value True And
UCase(txtSt.Txt) CA Then
CA OH CA NY CA
T/F T/F T/F T/F T/F
1500 1000 1000 1500 1000
False True True True False
Hint Use Parenthesis to clarify
96Compound Conditions
- Order to evaluate
- ANDs before ORs
- Given
- If ((curSale 1000)Or((optDis.Value
True)And(UCase(txtSt.Txt)CA))) Then
Hint Use Parenthesis to clarify
1st
CA OH CA NY CA
T/F T/F T/F T/F T/F
1500 1000 1000 1500 1000
False True True True False
And - what changes if an AND is used here?
97Nested If Statements
- More complex logic may need multiple conditions
and multiple branches of logic - Use the premise of Option Buttons and Check Boxes
to help - What are basic differences?
98Conditionals
chkHome .Value 1
T
Write pseudocode
Do Nothing
lblMessage.Caption Personal call
chkBus .Value 1
T
lblMessage.Caption Business call
Do Nothing
99Conditionals
optRed .Value 1
F
optBlu .Value 1
lbl.Message.Text Red
F
optBla .Value 1
lbl.Message.Text Blue
lbl.Message.Text Black
lbl.Message.Text No Color
Write pseudocode
100Loops
- ALWAYS make sure that for each loop you deal
with, you know - The initial conditions
- The ending condition
- How the condition changes within the loop
101Preprocessing / Postprocessing
- There are two basic places to check for the
stopping condition in a loop at the beginning
(before anything is done) and at the end (after
something is done at least once). Therein lies
the basic question Do you want the loop
performed at least once. If yes, then you write a
postprocessing loop if no, then you construct a
preprocessing loop.
Preprocessing Postprocessing
process
process
102Preprocessing / Postprocessing
- Preprocessing in VB
- Do While (condition) Do While (strA x)
- processing steps msgbox strA
- Loop strA
InputBox(New char) - Loop
Preprocessing Postprocessing
process
process
103Preprocessing / Postprocessing
- Postprocessing in VB
- Do Do
- processing steps msgbox strA
- Loop While (condition) strA InputBox(New
char) - Loop
While (strA x)
Preprocessing Postprocessing
104Preprocessing / Postprocessing
- Rules of thumb
- Know the stopping condition
- Know whether you want the statement block to
execute at least once - If yes, postprocess
- If no, preprocess
- Make sure that within the statement block the
stopping condition is being changed
Apply these rules of thumb to reading a file.
Describe the conceptual process to open, read and
display a file.
105Counted Loops
- Sometimes the programmer knows how many times a
loop needs to be executed (or the formula/logic
whereby to determine the number of times). In
that case, the programmer should use a COUNTED
LOOP. VB uses the construct called a FOR NEXT loop
FOR variable startvalue TO stopvalue
STATEMENT BLOCK NEXT variable
106FOR NEXT
intA 1
- Generic
- FOR variable startvalue to stopvalue
- statement block
- NEXT variable
- Specific
- FOR intA 1 TO 10
- msgbox intA
- NEXT intA
(intA 10)
intA intA 1
msgbox intA
107FOR NEXT (expanded)
- Generic
- FOR variable startval TO stopval STEP increment
- statement block
- NEXT variable
intA 1
How many times is the statement block executed in
the FOR NEXT loops 1.) FOR x 1 TO 10 STEP 3
msgbox (loop) NEXT x 2.) FOR x 1 TO
10 STEP 5 msgbox(loop) NEXT x 3.)
FOR x 1 TO 10 STEP 1 msxbox(loop)
NEXT x
(intA 10)
intA intA STEP
msgbox intA
108Nested Loops
intA 1
FOR intA 1 TO 10 FOR intB 1 TO 5
msgbox(loop) NEXT intB NEXT intA How many
times is the msgbox statement performed? How
many if FOR intA 1 TO 10 STEP3 FOR intB 1
TO 5 STEP 5 are used?
intA10
intA intA 1
intB 1
intB5
intB intB 1
msgbox intA
109Nested Loops (cont)
FOR intA 1 TO 3 FOR intB 1 TO 4
msgbox intA, intB NEXT intB NEXT intA
Given the above nested loops, what is displayed?
110Nested Loops (cont)
FOR intA 1 TO 3 FOR intB 1 TO 4 intAns
intA intB msgbox A x B intAns
NEXT intB NEXT intA
Given the above nested loops, what is displayed?
111Summary
- Programming is a development process based around
both Art and Science - Art
- Problem solving
- Analysis
- Science
- Flowcharting
- Coding