1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This...

47
1 Programming & Algorithm Design Functions right 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt Assg

Transcript of 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This...

Page 1: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

1

Intro to Programming & Algorithm Design

Functions

Copyright 2003 by Janson Industries

This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt

Assg

Page 2: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries2

Objectives▀ Explain

■ What a function is

■ How to use and create functions

■ IPO Charts

▀ Show how to implement and use functions in Java

Page 3: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries3

Function▀ Per text: “Functions are methods/

modules that return a value”

▀ Just as with methods, the function header defines the function's■ Name■ Expected values (arguments)

▀ The function header also defines ■ The returned value type (Real, String)

▀ And the body must contain■ A Return statement

Page 4: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries4

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

Page 5: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries5

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

▀ Does captureInput accept any data?

Module main()Declare String subtotalsubtotal = captureInput()

End Module

Page 6: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries6

All PseudocodeModule main()

Declare String subtotal

subtotal = captureInput()

End Module

Function String captureInput()

Declare String data

Display “Enter input”

Input data

Return data

End Function

Page 7: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries

Function Calls Flowchart

7

Display “Enter input”subtotal =

captureInput()

main()

End

Declare String subtotal

Input data

captureInput()

Return data

Declare String data

Page 8: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries

SFCThe return value

type

Function name

Specify Function

The variable/value to return

Page 9: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries

SFC

Then add function

statements

Page 10: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries

SFC

Finished function

Page 11: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries

Raptor ▀ Doesn't support functions

Finished function

Page 12: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries

Raptor

When run

Page 13: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries

Java▀ Java very similar to pseudocode

▀ What will be the result?

The return value type

The value/variable to return

Function name

Page 14: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries14

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■ System.out.println("Stuff");

Page 15: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries15

Why Create Functions ▀ 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()

Page 16: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries16

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;}

}

Page 17: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries17

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”);

}}

Page 18: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries18

When FunctionCall2 is run, it invokes p in MyUtils

Must compile MyUtils before FunctionCall2

Page 19: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries19

Prewritten Functions▀ Most languages come with many to

perform common functions

■ Mathematical functions♦ Sine, cosine, square root, etc.

■ Generate a random number

■ Convert between data types

■ Displaying (println)

■ Exiting

■ Security♦ Userid/PW, cryptography

Page 20: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries20

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

Page 21: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries21

Prewritten Functions▀ Some examples

■ random(#,#) – returns a number between the first (inclusive) and second number (inclusive)

♦ random(1,6) returns a number between 1 and 6

■ 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

♦ round(3.49) returns 3♦ round(3.5) returns 4

Page 22: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries22

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

Page 23: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries23

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

Page 24: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries24

Prewritten Functions▀ String functions

■ length(string) – returns the number of characters in a string

♦ length(“hello”) returns the number 5

■ 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

♦ contains(“goodbye”, “oo”) returns the value true

Page 25: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries25

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

Page 26: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries26

Prewritten Functions ExampleFunction 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 numberInputEnd Function

Page 27: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries27

PrewrittenFunctionsExample

Page 28: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries28

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

Page 29: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries29

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

Page 30: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries30

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

Page 31: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries31

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

Page 32: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries32

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

Page 33: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries33

Prewritten Functions Example▀ What's the algorithm?

Dice game algorithm

▀ Then the XD is created

Page 34: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries34

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

Page 35: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries35

Prewritten Functions Example

▀ Dice game pseudocode

▀ Generate the SFC flowchart

SFC flowchart

Page 36: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries36

Dice Game Example: Raptor▀ Raptor returns random number like

java as ■ 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) to get 1 to 6

Page 37: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries37

Dice GameExample

Page 38: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries 38

Dice Game Example

Page 39: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries 39

Dice Game Example When Run

Page 40: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries 40

Dice Game Example Java

Page 41: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries 41

Dice Game Example Java

Page 42: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries42

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

Page 43: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries43

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

Page 44: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries44

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

Page 45: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries45

IPO Example▀ getTotalWithTax(String zip, Real total)

▀ Notice that taxRate considered input even though not a function argument

Input Processing Output

zip: String

total: Real

taxRate: Real

Real useableTaxRate, totalWithTax,

totalWithTax

Read taxRate from TaxFile for zip

useableTaxRate = taxRate + 1

totalWithTax = total * useableTaxRate

totalWithTax

Page 46: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries46

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

Page 47: 1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.

Copyright 2014 by Janson Industries

Assignments Non-Graded

Chap 6 labs 6.1 - 6.3

Graded

Chap 6 lab 6.4

47