Chapter 5 Functional Methods Learning Java through Alice

  • Slides: 12
Download presentation
Chapter 5 Functional Methods

Chapter 5 Functional Methods

Learning Java through Alice © Daly and Wrigley Objectives • Properly construct and use

Learning Java through Alice © Daly and Wrigley 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

Learning Java through Alice JOption. Pane © Daly and Wrigley 3

Learning Java through Alice JOption. Pane © Daly and Wrigley 3

4 © Daly and Wrigley Learning Java through Alice Math Class Method Description Method

4 © Daly and Wrigley Learning Java through Alice Math Class Method Description Method Call Result Argument Returns the absolute value Math. abs(-5. 5); 5. 5 double Returns the value of the first argument raised to the Math. pow(5, 2); power of the second argument 25 double, double Returns a positive number that is greater than or equal Math. random( ); to 0. 0 and less than 1. 0. Number between none 0 and 1 double Returns the closest whole number to the argument Math. round(6. 45); 6 double Returns the rounded positive square root Math. sqrt(7); 2. 6457513 double

© Daly and Wrigley Learning Java through Alice String Methods String s 1 =

© Daly and Wrigley Learning Java through Alice String Methods String s 1 = "Now is the winter of our discontent"; String s 2 = "Java can be fun and hard "; Method Result s 1. length( ) 35 s 2. length( ) 25 s 1. to. Upper. Case() NOW IS THE WINTER OF OUR DISCONTENT s 2. to. Upper. Case() JAVA CAN BE FUN AND HARD s 1. to. Lower. Case() now is the winter of our discontent s 2. to. Lower. Case() java can be fun and hard s 1. starts. With("st") False s 2. starts. With("Java") true 5

© Daly and Wrigley Learning Java through Alice String Methods String s 1 =

© Daly and Wrigley Learning Java through Alice String Methods String s 1 = "Now is the winter of our discontent"; String s 2 = "Java can be fun and hard "; Method Result s 1. ends. With(“TENT") false s 2. ends. With("so") false s 1. replace( 'e' , 'L' ) Now is th. L wint. Lr of our discont. Lnt s 2. replace( 'a' , '*' ) J*v* c*n be fun *nd h*rd s 1. equals(s 2) false s 1. equals. Ignore. Case(s 2) false s 1. contains("winter") true s 2. contains("@") false 6

Learning Java through Alice © Daly and Wrigley Comparing Objects vs Primitives 7

Learning Java through Alice © Daly and Wrigley Comparing Objects vs Primitives 7

Learning Java through Alice © Daly and Wrigley String Comparisons 8

Learning Java through Alice © Daly and Wrigley String Comparisons 8

Learning Java through Alice © Daly and Wrigley String Comparisons 9

Learning Java through Alice © Daly and Wrigley String Comparisons 9

Learning Java through Alice © Daly and Wrigley String Tokenizer String s 1 =

Learning Java through Alice © Daly and Wrigley String Tokenizer String s 1 = "Now is the winter of our discontent"; String. Tokenizer tokens = new String. Tokenizer(s 1); int x = tokens. count. Tokens(); • Need following import: ▫ import java. util. String. Tokenizer; • Breaks up strings into pieces called tokens • Tokens are separated by whitespace characters such as blanks, tabs, newlines, and carriage returns. 10

Learning Java through Alice © Daly and Wrigley Alice Functional Methods 11

Learning Java through Alice © Daly and Wrigley Alice Functional Methods 11

Learning Java through Alice © Daly and Wrigley Method with Arguments & Return Value

Learning Java through Alice © Daly and Wrigley Method with Arguments & Return Value 12