12
Chapter 5 Functional Methods

Chapter 5 Functional Methods. © Daly and Wrigley Learning Java through Alice Objectives Properly construct and use methods when programming. Describe

Embed Size (px)

Citation preview

Page 1: Chapter 5 Functional Methods. © Daly and Wrigley Learning Java through Alice Objectives Properly construct and use methods when programming. Describe

Chapter 5

Functional Methods

Page 2: Chapter 5 Functional Methods. © Daly and Wrigley Learning Java through Alice Objectives Properly construct and use methods when programming. Describe

© Daly and Wrigley

Learning Java through Alice

Objectives•Properly construct and use methods when

programming.•Describe the difference between a procedural method

and a functional method.•Use the Java Application Interface to code programs.•Place methods into a separate file and call them from

the main program.

2

Page 3: Chapter 5 Functional Methods. © Daly and Wrigley Learning Java through Alice Objectives Properly construct and use methods when programming. Describe

© Daly and Wrigley

Learning Java through Alice

JOptionPane

Symbol int messageType

0

1

2

3

Blank -1

3

Page 4: Chapter 5 Functional Methods. © Daly and Wrigley Learning Java through Alice Objectives Properly construct and use methods when programming. Describe

© Daly and Wrigley

Learning Java through Alice

Math ClassMethod Description Method Call Result Argument Returns

Returns the absolute value Math.abs(-5.5); 5.5 double double

Returns the value of the first argument raised to the power of the second argument

Math.pow(5, 2); 25 double, double double

Returns a positive number that is greater than or equal to 0.0 and less than 1.0.

Math.random( ); Number between 0 and 1 none double

Returns the closest whole number to the argument Math.round(6.45); 6 double double

Returns the rounded positive square root Math.sqrt(7); 2.6457513 double double

4

Page 5: Chapter 5 Functional Methods. © Daly and Wrigley Learning Java through Alice Objectives Properly construct and use methods when programming. Describe

© Daly and Wrigley

Learning Java through Alice

String MethodsString s1 = "Now is the winter of our discontent"; String s2 = "Java can be fun and hard ";

Method Result

s1.length( ) 35

s2.length( ) 25

s1.toUpperCase() NOW IS THE WINTER OF OUR DISCONTENT

s2.toUpperCase() JAVA CAN BE FUN AND HARD

s1.toLowerCase() now is the winter of our discontent

s2.toLowerCase() java can be fun and hard

s1.startsWith("st") False

s2.startsWith("Java") true

5

Page 6: Chapter 5 Functional Methods. © Daly and Wrigley Learning Java through Alice Objectives Properly construct and use methods when programming. Describe

© Daly and Wrigley

Learning Java through Alice

String Methods                     String s1 = "Now is the winter of our discontent"; String s2 = "Java can be fun and hard ";

Method Result

s1.endsWith(“TENT") false

s2.endsWith("so") false

s1.replace( 'e' ,'L' ) Now is thL wintLr of our discontLnt

s2.replace( 'a' , '*' ) J*v* c*n be fun *nd h*rd

s1.equals(s2) false

s1.equalsIgnoreCase(s2) false

s1.contains("winter") true

s2.contains("@") false

6

Page 7: Chapter 5 Functional Methods. © Daly and Wrigley Learning Java through Alice Objectives Properly construct and use methods when programming. Describe

© Daly and Wrigley

Learning Java through Alice

Comparing Objects vs Primitives

7

Page 8: Chapter 5 Functional Methods. © Daly and Wrigley Learning Java through Alice Objectives Properly construct and use methods when programming. Describe

© Daly and Wrigley

Learning Java through Alice

String Comparisons

8

Page 9: Chapter 5 Functional Methods. © Daly and Wrigley Learning Java through Alice Objectives Properly construct and use methods when programming. Describe

© Daly and Wrigley

Learning Java through Alice

String Comparisons

9

Page 10: Chapter 5 Functional Methods. © Daly and Wrigley Learning Java through Alice Objectives Properly construct and use methods when programming. Describe

© Daly and Wrigley

Learning Java through Alice

String TokenizerString s1 = "Now is the winter of our discontent";StringTokenizer tokens = new StringTokenizer(s1); int x = tokens.countTokens();

•Need following import:▫ import java.util.StringTokenizer;

•Breaks up strings into pieces called tokens •Tokens are separated by whitespace characters such as blanks, tabs, newlines, and carriage returns. 

10

Page 11: Chapter 5 Functional Methods. © Daly and Wrigley Learning Java through Alice Objectives Properly construct and use methods when programming. Describe

© Daly and Wrigley

Learning Java through Alice

Alice Functional Methods

11

Page 12: Chapter 5 Functional Methods. © Daly and Wrigley Learning Java through Alice Objectives Properly construct and use methods when programming. Describe

© Daly and Wrigley

Learning Java through Alice

Method with Arguments & Return Value

12