Intro to the AS/400 - Florida State College at Jacksonville
Download
Report
Transcript Intro to the AS/400 - Florida State College at Jacksonville
Intro to Programming
& Algorithm Design
Functions
Assg
This presentation can be viewed on line in a file named:
ch06.IntrotoProg.Functions.ppt
Copyright 2003 by Janson Industries
1
Objectives
▀
▀
Explain
■
What a function is
■
How to use and create
functions
■
IPO Charts
Show how to implement and
use functions in Java
2
Copyright 2014 by Janson Industries
Function
▀
▀
Per text: “Functions are methods/
modules that return a value”
Just as with methods, the function
header defines the function's
■
■
▀
The function header also defines
■
▀
Name
Expected values (arguments)
The returned value type (Real, String)
And the body must contain
■
A Return statement
3
Copyright 2014 by Janson Industries
Function
▀
In pseudocode, function starts
with the word Function and ends
with End Function
Function String captureInput()
Declare String data
Display “Enter input”
Input data
Return data
End Function
4
Copyright 2014 by Janson Industries
Function
▀
▀
A function's returned value can be
assigned to a variable
Call a function just like a module:
■
Specify the function name
followed by parenthesis
Module main()
Declare String subtotal
subtotal = captureInput()
End Module
▀
Copyright 2014 by Janson Industries
Does captureInput accept any
data?
5
All Pseudocode
Module main()
Declare String subtotal
subtotal = captureInput()
End Module
Function String captureInput()
Declare String data
Display “Enter input”
Input data
Return data
End Function
Copyright 2014 by Janson Industries
6
Function Calls Flowchart
captureInput()
main()
Declare String
subtotal
Declare String
data
Display “Enter input”
subtotal =
captureInput()
Input data
End
Return data
7
Copyright 2014 by Janson Industries
SFC
Specify Function
The return value type
The variable/value to return
Function name
Copyright 2014 by Janson Industries
SFC
Then add function
statements
Copyright 2014 by Janson Industries
SFC
Finished function
Copyright 2014 by Janson Industries
Raptor
▀
Doesn't support
functions
Finished function
Copyright 2014 by Janson Industries
Raptor
When run
Copyright 2014 by Janson Industries
Java
▀
Java very similar to pseudocode
The return value type
Function name
The value/variable to return
▀
What will be the result?
Copyright 2014 by Janson Industries
Prewritten Functions
▀
▀
Supplied with programming
language
Example: System.out.println() the
println function is invoked in a
class called System
■
▀
You can tell because () follows
the name
This is an example of a function
that will accept a parameter
■
Copyright 2014 by Janson Industries
System.out.println("Stuff");
14
Why Create Functions
▀
▀
▀
▀
Copyright 2014 by Janson Industries
Just as with methods, cuts down
on duplicate code
Make it easier for programmer
For instance, System.out.println()
is a lot to type
Will create a class called MyUtils
■
Create a function called p(String output)
■
Will be able to specify MyUtils.p()
instead of System.out.println()
15
Pre-Written functions
▀
MyUtils just acts as a repository of
useful functions
■
So, no need for a main()
public class MyUtils {
}
public static int p(String output){
System.out.println(output);
return 1;
}
16
Copyright 2014 by Janson Industries
Pre-Written Functions
▀
▀
Now any other class can use the
function p()
In any java class, just specify
■
ClassName.functionName();
public class FunctionCall2 {
}
public static void main(String[] args) {
MyUtils.p(“Howdy”);
}
17
Copyright 2014 by Janson Industries
When FunctionCall2 is run, it invokes p in MyUtils
Must compile MyUtils before FunctionCall2
18
Copyright 2014 by Janson Industries
Prewritten Functions
▀
Most languages come with many
to perform common functions
■
Mathematical functions
♦
■
Generate a random number
■
Convert between data types
■
Displaying (println)
■
Exiting
■
Security
♦
Copyright 2014 by Janson Industries
Sine, cosine, square root, etc.
Userid/PW, cryptography
19
Prewritten Functions
▀
Advantage of prewritten functions
■
Don't have to create the function
yourself
■
Don't have to understand how it
works
♦
■
Called implementation hiding
You're sure they work
20
Copyright 2014 by Janson Industries
Prewritten Functions
▀
Some examples
■
random(#,#) – returns a number
between the first (inclusive) and
second number (inclusive)
♦
■
pow(#,#) – raises the first number to
the power of the second number
♦
■
pow(2,4) returns 16
round(#.#) – returns an integer
value for a real value
♦
Copyright 2014 by Janson Industries
random(1,6) returns a number
between 1 and 6
♦
round(3.49) returns 3
round(3.5) returns 4
21
Prewritten Functions
▀
Some examples
■
isInteger(string) – returns a Boolean
value based on whether the string
can be converted to an integer
♦
■
isInteger("6") returns a value of true
isReal(string) – returns a Boolean
value based on whether the string
can be converted to a Real
♦
isReal(“abc”) returns a value of false
22
Copyright 2014 by Janson Industries
Prewritten Functions
▀
▀
Converting data types
All data entered is String must
convert to get numeric value
■
stringToInteger(string) – returns the
integer value of the string
♦
■
stringToInteger(“6”) returns the number 6
stringToReal(string)– returns the real
value of the string
♦
stringToReal(“2.14”) returns the number
2.14
23
Copyright 2014 by Janson Industries
Prewritten Functions
▀
String functions
■
length(string) – returns the number of
characters in a string
♦
■
substring(string, 3, 5)– returns the
characters from position 3 to 5
♦
■
substring(“goodbye”, 3, 5) returns the
string “db”
contains(string, string)– returns
Boolean value of true or false based
on if the first string contains the
characters in the second string
♦
Copyright 2014 by Janson Industries
length(“hello”) returns the number 5
contains(“goodbye”, “oo”) returns the
value true
24
Prewritten Functions Example
▀
Will enhance the captureInput
function to convert the data into a
numeric value
Module main()
Declare Real subtotal
Display “Enter number”
subtotal = captureInput()
End Module
25
Copyright 2014 by Janson Industries
Prewritten Functions Example
Function Real captureInput()
Declare String data
Declare Real numberInput
Display “Enter a number”
Input data
While(!(isReal(data)))
Display “Please enter a number”
Input data
EndWhile
numberInput = stringToReal(data)
Return numberInput
End Function
26
Copyright 2014 by Janson Industries
Prewritten
Functions
Example
27
Copyright 2014 by Janson Industries
Prewritten Functions Java
▀
Are in classes like Math, Integer,
Double, String
■
Math.pow(#,#) – raises the first
number to the power of the second
number
♦
■
Math.pow(2,4) returns 16
Math.round(#.#) – returns an integer
value for a real value
♦
♦
Math.round(3.49) returns 3
Math.round(3.5) returns 4
28
Copyright 2014 by Janson Industries
Prewritten Functions Java
▀
Some examples
■
Math.random() – returns a number
between 0 and 1(exclusive)
♦
To get an int in a range, must multiply
the result by the max value & add one,
then change to an int (truncate)
• (int) (Math.random()*6) +1
■
Random class has nextInt(#) method
that returns a number from a range
0(inclusive) to #(exclusive)
♦
Random.nextInt(7)
• Returns integer value of 0, 1, 2, 3, 4, 5, or 6
29
Copyright 2014 by Janson Industries
Prewritten Functions Java
▀
▀
▀
Converting data types
All data entered is String must
convert to get numeric value
■
Integer.valueOf("6")
■
Double.valueOf("2.14")
There are no isReal or isInteger
functions in java
30
Copyright 2014 by Janson Industries
Prewritten Functions Java
▀
String functions
■
Assuming:
♦
String testString = new String("Goodbye");
■
testString.length() - returns 7
■
testString.substring(3, 5) – returns the
string “db”
■
testString.contains("oo") – returns
Boolean value true
■
testString.contains("OO") – returns
Boolean value false
31
Copyright 2014 by Janson Industries
Prewritten Functions Example
▀
How about creating a dice game
■
Ask user if they want to play
■
If yes, generate 2 random numbers
between 1 and 6 to rep the user's
and computer's dice roll
■
Print out numbers and message
saying who won (who got the higher
number) or if it was a tie
■
Ask the user if they want to play
again
32
Copyright 2014 by Janson Industries
Prewritten Functions Example
▀
What's the algorithm?
Dice game algorithm
▀
Then the XD is created
33
Copyright 2014 by Janson Industries
Prewritten Functions Example
▀
▀
Then an additional requirement is
added to create a function called
getMessage
getMessage will
■
Accept the two dice rolls
■
Generate the correct message
■
Return the message
34
Copyright 2014 by Janson Industries
Prewritten Functions Example
▀
Dice game pseudocode
▀
Generate the SFC flowchart
SFC flowchart
35
Copyright 2014 by Janson Industries
Dice Game Example: Raptor
▀
Raptor returns random number
like java as
■
▀
Copyright 2014 by Janson Industries
A non-Integer between 0 and 1
(exclusive)
So must:
■
Multiply by 6 (max value) to get
number between 0 and 5.99999
■
Then add one to get 1 to 6.9999
■
Then truncate (with floor command)
36
to get 1 to 6
Dice Game
Example
37
Copyright 2014 by Janson Industries
Dice Game Example
Copyright 2014 by Janson Industries
38
Dice Game Example When Run
Copyright 2014 by Janson Industries
39
Dice Game Example Java
Copyright 2014 by Janson Industries
40
Dice Game Example Java
Copyright 2014 by Janson Industries
41
Alternative Documentation
▀
IPO (Input Processing Output) Chart
■
▀
Really a table with text (not a chart)
Three columns
■
First column identifies input
■
Second column describes processing
■
Third column identifies output
42
Copyright 2014 by Janson Industries
IPO Example
▀
getTotalWithTax(String zip, Real
total)
■
Calculates a Sales amount including
tax based on the zip code where the
transaction takes places
■
Reads file for the Sales tax
percentage
43
Copyright 2014 by Janson Industries
IPO Example Pseudocode
Function Real getTotalWithTax(String zip,
Real total)
Declare Real taxRate, useableTaxRate,
totalWithTax
taxRate = Read TaxFile for zip
useableTaxRate = taxRate + 1
totalWithTax = total * useableTaxRate
Return totalWithTax
End Function
44
Copyright 2014 by Janson Industries
IPO Example
▀
Input
zip: String
Processing
Real useableTaxRate, totalWithTax,
total: Real
taxRate: Real
getTotalWithTax(String zip, Real
total)
Output
totalWithTax
totalWithTax
Read taxRate from TaxFile for zip
useableTaxRate = taxRate + 1
totalWithTax = total * useableTaxRate
▀
Copyright 2014 by Janson Industries
Notice that taxRate considered
input even though not a function
argument
45
Points to Remember
▀
▀
▀
Functions are methods/modules
that return values
Lots of prewritten functions that
provide commonly used logic
IPO Charts are an alternative
design/documentation tool
46
Copyright 2014 by Janson Industries
Assignments
Non-Graded
Chap
6 labs 6.1 - 6.3
Graded
Chap
6 lab 6.4
47
Copyright 2014 by Janson Industries