Download Intro to the AS/400 - Florida State College at Jacksonville

Document related concepts
no text concepts found
Transcript
Intro to Programming
& Algorithm Design
Input, Processing
and Output
Assg1 Assg2 Assg3 Labs
1
Copyright 2014 by Janson Industries
Objectives
▀
Explain
♦
The process of designing and
coding a program
♦
The overall logic flow of programs
♦
Variables
♦
External and Internal design
2
Copyright 2014 by Janson Industries
Programming Process
▀
▀
More than just coding
1. Understand the problem and the
users requirements
♦
Unearth the real problem
► Don't
♦
♦
react to symptoms
Find a logical solution
Clarify non-logic requirements
► Format,
♦
♦
timeliness, responsiveness
Often considered an analysts job
Most difficult part of process
3
Copyright 2014 by Janson Industries
Programming Process
▀
▀
Who’s involved:
♦
Programmer/Analyst
♦
End user
Usually the focal point of discussion
is the output
♦
Specifically what data should appear
♦
The data organization and format
► Sort
order, summary data, paper
printout, screen display, storyboard
4
Copyright 2014 by Janson Industries
Programming Process
▀
2. Clearly define the program's logic
♦
♦
♦
Writing down the "steps" the program
should take (the algorithm)
Programming language neutral
Then further define, 2 primary tools
► Pseudo
code
► Flowcharts
♦
Like specifying directions
► Writing
out in words
vs.
► Draw
a map
5
Copyright 2014 by Janson Industries
Pseudo Code (OilCalc)
▀
Use subset of words consistently
▀
Indentation shows selection & looping
Module main()
Declare Integer order
Declare Real cost
Display “Amount of oil is? ”
Input order
If order < 150 Then
cost= order x 2.25
Else If order < 250 Then
cost= (150 x 2.25) + (order - 150) x 2.10
Else
cost= (150 x 2.25) + (100 x 2.10) + (order - 250) x 1.99
Endif
Endif
Display “The cost of the oil is ”, cost
End Module
6
Copyright 2014 by Janson Industries
Flow Charts
▀
▀
Copyright 2014 by Janson Industries
Use symbols and words to
further define the algorithm
Common flowchart symbols
♦
Input/Output:
♦
Process:
♦
Start/Stop:
♦
Decision:
♦
Sequence/Flow:
7
Flowchart
▀
Sequence shown
with arrows
Insert
key
Turn
key
Put
into
reverse
Copyright 2014 by Janson Industries
Step
on gas
pedal
8
Flowchart
▀
Selection shown with a diamond
True
Step
on brake
Copyright 2014 by Janson Industries
Light =
“red”
False
Maintain
speed
9
Flowchart
▀
Hair =
“dirty”
Iteration (aka looping, repeating)
True
Lather
Rinse
False
Get out of
shower
Copyright 2014 by Janson Industries
10
Flowchart
(OilCalc)
Display “The
amount of oil is ?”
Input order
Is order
< 150?
True
cost = order *
2.25
False
True
cost= (150 *
2.25) + (order150) * 2.10
Copyright 2014 by Janson Industries
Display “ The cost
of the oil is ”, cost
Is order
< 250?
False
cost=(150 * 2.25) +
(100*2.10)+(order250) * 1.99
11
Large Flowcharts
▀
Additional flowchart symbols
♦
On page connector:
1
♦
Off page connector:
Pg 3
C
12
Copyright 2014 by Janson Industries
Large Flowcharts
Copyright 2014 by Janson Industries
Start
1
Insert
key
Put
into
reverse
Turn
key
Step
on gas
pedal
1
Pg 3
C
13
Programming Process
▀
3. Code the program
♦
Pick a language based on
requirements
► Each
language has its own advantages
and disadvantages
♦
Enter HLL instructions
► Most
source code editors will highlight
obvious errors (like Word)
♦
Once a programmer has mastered a
language, this is relatively simple step
► The
Copyright 2014 by Janson Industries
hard part is the first two steps
14
Code the Program
// OilCalc.java
import java.io.*;
import java.util.Scanner;
public class OilCalc {
public static void main(String[] args) throws IOException {
Scanner keyboard = new Scanner(System.in);
int amount = 0;
double cost = 0;
System.out.print("Amount of oil is? ");
amount = = keyboard.nextInt();
if (amount <= 100) {cost = amount * 2.25;
else if (amount <= 250) { cost = 100 * 2.25 + (amount-100) * 2.10;}
else { cost = 100 * 2.25 + 150 * 2.10 + (amount-250) * 1.99;}
}
System.out.println("The cost of the oil is $" + cost)
}
Copyright 2014 by Janson Industries
15
Programming Process
▀
4. Translate source code to
executable machine language and
resolve syntax errors
♦
Iterative process:
► Translate
using compiler/interpreter
► Get error list from compiler/interpreter
► Change HLL instructions to resolve errors
► Translate using compiler/interpreter
♦
Compiler/interpreter not perfect
► Takes
a stab at what it thinks is the error
16
Copyright 2014 by Janson Industries
Errors
▀
If a program was like baking a
cake
♦
Get ingredients
♦
Process ingredients (mix, cook, ice)
♦
Serve cake
17
Copyright 2014 by Janson Industries
Errors
▀
Syntax errors
♦
▀
▀
▀
Get nigredients
A person could probably
understand that instruction
A computer does not
Translation s/w (compiler)
checks for syntax errors
♦
Will generate errors msgs
18
Copyright 2014 by Janson Industries
Resolve Syntax Errors
19
Copyright 2014 by Janson Industries
Resolve Syntax Errors
// OilCalc.java
import java.io.*;
import java.util.Scanner;
public class OilCalc {
public static void main(String[] args) throws IOException {
Scanner keyboard = new Scanner(System.in);
int amount = 0;
double cost = 0;
System.out.print("Amount of oil is? ");
amount = keyboard.nextInt();
if (amount <= 100) {cost = amount * 2.25;}
else if (amount <= 250) { cost = 100 * 2.25 + (amount-100) * 2.10;}
else { cost = 100 * 2.25 + 150 * 2.10 + (amount-250) * 1.99;}
}
System.out.println("The cost of the oil is $" + cost);
}
Copyright 2014 by Janson Industries
20
Resolve Syntax Errors
21
Copyright 2014 by Janson Industries
Programming Process
▀
5. Test for and resolve logic errors
♦
Execute with test data
► Should
have many tests with a wide
range of data
♦
Verify results are correct
♦
Often many types of testing:
► Programmer,
User, Performance
► Each type may require its own unique
test data
22
Copyright 2014 by Janson Industries
Errors
▀
Logic errors (aka run time errors)
♦
Instructions are syntactically
correct but do not perform the
desired function
♦
Often difficult to resolve
► In
baking cake example:
• Get baking soda (instead of powder)
♦
Or
► Put
letter in envelop
► Put envelop in mailbox
► Put stamp and address on envelop
Copyright 2014 by Janson Industries
23
Errors
▀
In other words:
♦
Instructions must be syntactically
correct
► Always
use the correct command to
indicate input, output, etc.
► Command must follow the
programming language syntax rules
♦
The order in which the instructions
are specified is important
► For
example, must read data before it
can be acted upon
24
Copyright 2014 by Janson Industries
Test/Resolve Logic Errors
▀
In OilCalc make sure
♦
Calculated amounts are correct
♦
Prompts and displayed results match
the design
25
Copyright 2014 by Janson Industries
General Program Logic
▀
Most program functions are
executed in this order
♦
♦
♦
▀
Get the input
Process the input into output
Show the output
Just like the baking a cake example
♦
♦
♦
Get ingredients
Process ingredients (mix, cook, ice)
Serve cake
26
Copyright 2014 by Janson Industries
Input
▀
▀
Users should be prompted for the
data to be entered
♦
In command line, prompts are
displayed text
♦
In GUI, text fields have labels
identifying the data to enter
When data entered into a
program, variables are needed to
store/hold that data
27
Copyright 2014 by Janson Industries
Command Prompt Input
When program is run, user prompted for info (Amount of oil is?),
user enters info (250) at the prompt, presses the Enter key, the
result (The cost of oil is $540.0) is displayed
28
Copyright 2014 by Janson Industries
GUI Input
Label text indicates what
data the user should enter
29
Copyright 2014 by Janson Industries
Good Design
▀
Write clear prompts for input
♦
♦
A prompt is a message describing the
information that should be input
In OilCalc should have prompted with
► Number
▀
of gallons of oil being purchased?
Echo back input
♦
In OilCalc, instead of
► The
♦
cost of the oil is $540
Should have displayed
► The
cost of 250 gallons of oil is $540
30
Copyright 2014 by Janson Industries
Good Design
▀
▀
Makes a program user-friendly
User-friendly means that the
program is easy for a user (aka an
end user) to work with
♦
I.e. It’s clear what the user should do
to get the desired results
31
Copyright 2014 by Janson Industries
Variables
▀
▀
Defined in programs to hold
♦
Data input to program
♦
Results of processing
Variable values can be changed
♦
This is what makes a program so
useful
► Same
function can be performed
against different data
▀
Can think of variables as ministorage areas within the program
32
Copyright 2014 by Janson Industries
Variables
▀
▀
In programming languages,
variables must have:
♦
A name (aka an identifier)
♦
A type
♦
A size
In designs, variables are declared
by specifying the name and type
of data
♦
Copyright 2014 by Janson Industries
Usually no size is required
33
Variable Names
▀
Names should be:
♦
♦
Meaningful
A continuous string of characters
► No
♦
♦
spaces between characters
Begin with a lower case letter
Be camel-cased
► Each
▀
new word starts with a capital letter
Examples
♦
♦
oilAmtGallons
custName
34
Copyright 2014 by Janson Industries
Variables
▀
Variable must be defined (aka
declared) before it can be used
Declare Integer oilAmtGallons
Input oilAmtGallons
▀
This is valid:
▀
This is not valid:
Input oilAmtGallons
Declare Integer oilAmtGallons
♦
Like saying
► Get
Copyright 2014 by Janson Industries
two eggs
► Crack into bowl
► Get bowl
35
Variable Types
▀
For designs, three variable types
♦
String variables hold text (values that are
not used in arithmetic operations) and
the assigned value is enclosed in ""
► “Joe
Smith”
► “123 Main St”
► “904-646-2270”
♦
Integer variables hold whole numbers
► 437,
♦
6, 27904
Real variables hold numbers with
fractional values
► 3.147562,
Copyright 2014 by Janson Industries
.99, 14367.52
36
Variables
▀
For example, OilCalc has variables
to hold the amount of oil entered
(order) and the calculated cost of
the oil (cost)
Declare Integer order
Declare Real cost
Display “Amount of oil is? ”
Input order
If order < 150 THEN
cost= order x 2.25
Else
If order < 250 THEN
cost= (150 x 2.25) + (order - 150) x 2.10
Else
cost= (150 x 2.25) + (100 x 2.10) +
(order - 250) x 1.99
Endif
Endif
37
Display “The cost of the oil is ”, cost
Copyright 2014 by Janson Industries
Setting Variable Values
▀
Can be initialized to a value
♦
Value assigned when variable is
declared
► Previously,
the variable examples were
all uninitialized variables
Declare Integer order
Declare Real cost
♦
Value is assigned with an equal sign
and then the value to the right of the
variable name
Declare String custName = “Joe Smith”
Declare Real salesTaxRate = .065
38
Copyright 2014 by Janson Industries
Setting Variable Values
▀
Uninitialized variables can be
assigned a value later, either with
input operations or calculations
Declare Integer order
Declare Real cost
:
:
:
Input order
:
:
:
cost = order * price
♦
Book uses the keyword Set, I will not
♦
You can use Set or not use Set
► But
you must me consistent!
39
Copyright 2014 by Janson Industries
Data Compatibility
▀
▀
The data assigned to a variable
must be the same type as the
variable type
The following are invalid
Declare Integer order
Declare Real cost
Declare String custName
cost = “John Smith”
custName = 44
order = 1.75
custName = John Smith
40
Copyright 2014 by Janson Industries
Data Compatibility
▀
▀
Real variables can hold values
with a fractional value of zero
So the following are valid
Declare Integer order
Declare Real cost
Declare String custName
cost = 1
custName = “John Smith”
order = 3416
♦
The double quotes are not part of
the String variable value
► But
are required when specifying a
string value
Copyright 2014 by Janson Industries
41
Variables
▀
Consider this calculation
netSalary = salary – (salary * .25)
▀
.25 is called an unnamed constant
▀
Problems with unnamed constants:
♦
Not clear what the value (.25) is
♦
If value is changed and is used many
times in the program, programmer
must find and change all occurrences
42
Copyright 2014 by Janson Industries
Named Constant Variables
▀
Used instead of unnamed
constants
♦
▀
Instead of using a value like .25 in
calculation, create a named constant,
assign .25 to it and use
Named constants
♦
♦
♦
Initialized when defined
Name is all caps
“Words” separated by underscores
Declare Real INCOME_TAX_RATE = .25
43
Copyright 2014 by Janson Industries
Constant Variables
▀
Advantages:
♦
Meaningful variable name rather
than static number makes
understanding program easier
► “Magic”
♦
values eliminated
If value changes, change once in
variable definition not throughout
the program
44
Copyright 2014 by Janson Industries
Assigning Variable Values
▀
▀
Variables can be assign a value
that is the result of a formula
Variable is on the left of the equal
sign, formula on right
Declare Real salary, netSalary, INCOME_TAX_RATE = .25
Input salary
netSalary = salary - (salary * INCOME_TAX_RATE)
▀
Standard arithmetic operators
♦
+, -, *, /
45
Copyright 2014 by Janson Industries
Order of Operations
▀
Follows math standard
♦
♦
♦
▀
▀
Formula within parenthesis
done before all above
So 2*3+(6-4)/2 equals 7
♦
♦
♦
Copyright 2014 by Janson Industries
Multiplication and division first
Then addition and subtraction
Left to right
♦
2*3+2/2
6+2/2
6+1
7
46
Order of Operations
▀
Without parenthesis what does
2*3+6-4/2 equal?
♦
6+6-4/2
♦
6+6-2
♦
12-2
♦
10
47
Copyright 2014 by Janson Industries
Integer Division
▀
Beware of a division operation
between 2 Integer variables
♦
▀
Any fractional value will be
truncated (i.e. deleted)
For example
Declare Integer firstNumber, secondNumber
Real result
firstNumber = 7
secondNumber = 2
result = firstNumber/secondNumber
▀
result is equal to 3 not 3.5
48
Copyright 2014 by Janson Industries
Documentation
▀
External: describes how to use
the program
♦
▀
Internal: describes how the
program works
♦
▀
Includes comments (nonexecutable text) added in code to
explain what the program is doing
For example, for a car
♦
♦
Copyright 2014 by Janson Industries
User manual
A manual on how to drive (external)
A mechanics manual (internal) 49
Comments
▀
Block vs. Line comments
♦
Block comments span many lines
► Contain
♦
Line comments are a single line
► Explain
▀
extensive info
a small area of the program
Orgs usually have standards re:
block and line comments
50
Copyright 2014 by Janson Industries
Comment Standards Example
▀
All programs must begin with a
block comment that includes
♦
Program and author’s name
♦
Date program completed
♦
Brief explanation of pgm function
(at least one paragraph)
♦
List of all files read/written to
♦
Entry for each change that includes
► Date
Copyright 2014 by Janson Industries
of change
► Programmers name
► Explanation of change (at least one
sentence)
51
Comment Standards Example
▀
▀
All programs must include a line
comment for at least every ten
lines of code
Line comment should explain the
function of the code
52
Copyright 2014 by Janson Industries
Comments
▀
▀
Each programming
language/internal design tool will
have it’s own syntax for defining
In pseudo code, will use two
forward slashes to indicate a
comment
// This is a non-executable line comment
53
Copyright 2014 by Janson Industries
Pseudocode Comments Example
//
//
//
//
OilCalc.java
Created by R. Janson on 1/1/2015
Program accepts the amount of oil being purchased by the
user then calculates and displays the total cost of that amount
// Variables defined to hold the input and output values
Declare Integer order
Declare Real cost
// Prompt the user for the amount and save it to order
Display “Amount of oil is? ”
Input order
// Calculate the cost based on the amount of oil being purchased
If order < 150 Then
cost= order x 2.25
Else
If order < 250 Then
cost= (150 x 2.25) + (order - 150) x 2.10
Else
cost= (150 x 2.25) + (100 x 2.10) +
(order - 250) x 1.99
Endif
Endif
// Display the cost of oil being purchased
Display “The cost of the oil is ”, cost
Copyright 2014 by Janson Industries
54
SFC
▀
Prompts for some of the block
comment info
♦
♦
♦
Author
Course
Program name
▀
Supports line comments
▀
Automatically puts in Start/Stop
▀
Variable declarations defined as
process statements
55
Copyright 2014 by Janson Industries
SFC
▀
Prompts for some of the block
comment info
▀
Supports line comments
▀
Automatically puts in Start/Stop
▀
Variable declarations defined as
process statements
56
Copyright 2014 by Janson Industries
SFC
To insert more line comments, click on location
within the FC then Edit and Insert
57
Copyright 2014 by Janson Industries
SFC
58
Copyright 2014 by Janson Industries
SFC
59
Copyright 2014 by Janson Industries
SFC
60
Copyright 2014 by Janson Industries
SFC
61
Copyright 2014 by Janson Industries
Simplified OilCalc
Start
Declare Integer order
Declare Real cost
Display “The
amount of oil is? ”
Input order
cost=order * 2.99
Display “The cost of ” +
order + “gallons of oil
is ” + cost
End
Copyright 2014 by Janson Industries
Non-graded Assg 1 - SFC
▀
▀
▀
Create the OilCalc flow chart in
SFC, save it as OilCalc.sfc and
send as email attachment with
topic of Chap2Assg1
Include the comments from the
pseudocode and the new
simplified calculation
Should look like this…
63
Copyright 2014 by Janson Industries
Non-graded Assg 1 - SFC
64
Copyright 2014 by Janson Industries
SFC
Non-graded Assg 1 - SFC
65
Copyright 2014 by Janson Industries
Raptor
▀
To create block comments at
beginning of FC, click
♦
♦
♦
Start oval
Edit
Comment
Enter comment text and click Done
66
Copyright 2014 by Janson Industries
Raptor
67
Copyright 2014 by Janson Industries
Non-graded Assg 2 - Raptor
▀
▀
Create the OilCalc flow chart in
Raptor, save it as OilCalc.rap
and send as email attachment
with topic of Chap2Assg2
Include the comments from the
pseudocode and the new
simplified calculation
♦
▀
Don’t forget that Raptor includes
the prompt with the input operation
Should look like this…
68
Copyright 2014 by Janson Industries
Non-graded Assg 2 - Raptor
69
Copyright 2014 by Janson Industries
Non-graded Assg 2 - Raptor
▀
When run should look like this
70
Copyright 2014 by Janson Industries
Java
▀
▀
//
//
//
//
Comment indicated with // (just
like in pseudo code)
Block comments placed at top of
file before all code
OilCalc.java
Created by R. Janson on 1/1/2015
Program accepts the amount of oil being purchased by the
user then calculates and displays the total cost of that amount
Start of java statements
71
Copyright 2014 by Janson Industries
Java
▀
To get input into program need to
use a Scanner object
♦
Need to import the object
♦
This means have to tell compiler
where to find it
► This
is done with a an import
statement
► import statements are at the beginning
of the source code
//
//
//
//
OilCalc.java
Created by R. Janson on 1/1/2015
Program accepts the amount of oil being purchased by the
user then calculates and displays the total cost of that amount
import java.io.*;
import java.util.Scanner;
Copyright 2014 by Janson Industries
72
Java
▀
The Scanner object has to be
created and assigned to a
Scanner variable
Scanner keyboard = new Scanner(System.in);
▀
Need the variables to hold the
input and output
♦
Java has int and double variable
types that correspond to Integer
and Real
int order = 0;
double cost = 0;
73
Copyright 2014 by Janson Industries
Java
▀
▀
To display text you can use either
System.out.println or
System.out.print
♦
println inserts a blank line after the
displayed text and puts the cursor
there
♦
print leaves the cursor on the same
line as the text
If we were prompting for the order
amount and then entering the
amount…
74
Copyright 2014 by Janson Industries
Java
▀
Using System.out.println, the
prompt and entered data would
look like this
The amount of oil is?
150
▀
Using System.out.print, the
prompt and entered data would
look like this
The amount of oil is? 150
75
Copyright 2014 by Janson Industries
Java
▀
▀
To get data into the program, we
use the appropriate next method
of the Scanner object
♦
nextInt()
♦
next()
♦
nextDouble()
Need to use the Scanner variable
(keyboard) to access the
Scanner object method
order = keyboard.nextInt();
76
Copyright 2014 by Janson Industries
//
//
//
//
OilCalc.java
Created by R. Janson on 1/1/2015
Program accepts the amount of oil being purchased by the
user then calculates and displays the total cost of that amount
Java
import java.io.*;
import java.util.Scanner;
public class OilCalc {
public static void main(String[] args) throws IOException {
// Variables defined to hold the input and output values
int order = 0;
double cost = 0;
// Create the Scanner object, prompt the user for the amount and save it to order
Scanner keyboard = new Scanner(System.in);
System.out.print("Amount of oil is? ");
order = keyboard.nextInt();
// Calculate the cost based on the amount of oil being purchased
cost = order * 2.99;
// Display the cost of oil being purchased
System.out.println("The cost of " + order + " gallons of oil is " + cost);
}
}
77
Copyright 2014 by Janson Industries
Non-graded Assg 3 - Java
▀
▀
When run, should look like this…
Create OilCalc.java, enter the
code, debug and run it, then
send OilCalc.java as an email
attachment with topic of
Chap2Assg3
78
Copyright 2014 by Janson Industries
Points to Remember
▀
Copyright 2014 by Janson Industries
Many phases to develop the
program:
♦
Understand the problem
♦
Create the External and Internal
design for the program
♦
Code the program
♦
Translate the program to machine
language
♦
Test the program and resolve
errors
79
Points to Remember
▀
Flowchart:
♦
▀
Diagram representation of program
logic
Pseudo code:
♦
Word representation of program logic
80
Copyright 2014 by Janson Industries
Labs
▀
Non-graded assgs:
♦
Chapter 2 Lab labs 1-1 through 1-4
► Paste
pseudocode into the document
and/or send as email attachment
• I recommended doing these and getting
feedback before doing the graded assg
► Send
flowchart files as email
attachments with lab name in subject
▀
Graded assg,
♦
Chapter 2 Lab 1-5
► Package
the work as specified above
81
Copyright 2014 by Janson Industries
All Assignments Due
▀
10 non-graded assgs:
♦
♦
Ch01 in class assgs 1,2,3 and Ch02 in
class assgs 1,2,3 sent as email
attachments
Chapter 2 Lab labs 1-1 through 1-4
► Paste
work into the document and/or send
as email attachment
• I recommended doing this and getting feedback
before doing the graded assg
▀
Graded assg, Chapter 2 Lab 1-5
♦
Paste all work into the document
and/or send files as email attachments
Copyright 2014 by Janson Industries
82
Assignment Problems
▀
If you have trouble emailing the labs
because the file is too big
♦
Delete the video links from the
document
♦
Break the document into separate docs
for each lab
► Instead
•
•
•
•
•
Copyright 2014 by Janson Industries
of one doc for all the labs, 5 docs
Lab 1_1.docx
Lab 1_2.docx
Lab 1_3.docx
Lab 1_4.docx
Lab 1_5.docx
83