22
Methods Chapter 6

Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize

Embed Size (px)

DESCRIPTION

3 Predefined Methods The Math class These methods are called by –The __________ of the class –The dot. operator –The name of the _____________ Example: double x = Math.sqrt(900.0);

Citation preview

Page 1: Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize

Methods

Chapter 6

Page 2: Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize

2

Program Modules in Java

• What we call "functions" in C++ are called "___________________" in Java

• Purpose– Reuse code– Modularize the program

• This can be done by putting the code in a method– Various objects in a program can invoke the

same method

Page 3: Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize

3

Predefined Methods

• The Math class• These methods are called by

– The __________ of the class– The dot . operator– The name of the _____________

• Example:double x = Math.sqrt(900.0);

Page 4: Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize

4

Method Declaration Syntaxmodifiers returnType methodName

(parameterDeclaration){ statements}• modifiers : describers (____________________, etc.)• returnType : type of value returned by method, or

__________ if it does not return a value• methodName : identifier that names the method• parameterDeclaration : list of parameters, separated by

______________________• statements : define the behavior of the method

Page 5: Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize

5

Argument Promotion

• Coercion of arguments– Forcing arguments to appropriate type to pass

to method System.out.println( Math.sqrt( 4 ) );

– Evaluates Math.sqrt( 4 )

– Then evaluates System.out.println()

Page 6: Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize

6

Argument Promotion• Promotion rules

– Specify how to ______________ types without data loss

Page 7: Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize

7

Method Definitions

• Consider a method to square a number public int square( int y )

{ return y * y; // return square of y } // end method square

• We will place it in an ____________SquareIntegers.java

Page 8: Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize

8

Method Definitions

• Consider a method to find the maximum of three numbers in an adapted version of Figure 6.4

• Note– It is an _____________– The maximum function must be __________– ____________ not needed

Page 9: Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize

9

Java API Packages

• Predefined classes grouped into categories of related classes– Called ________________

• Called the Java Application Programming Interface (API)

• Note the often used API packages

Page 10: Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize

10

Random-Number Generation

• Consider the random method in the Math class– Generates a random double 0 <= n < 1.0– The number can be manipulated and

____________ as an int to get desired range

– For the roll of a die1 + (int) (Math.random() * 6)

• Note Figure 6.7 and Figure 6.8

Page 11: Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize

11

Game of Chance

Rules for "Craps"• Roll dice first time

– If sum equals 7 or 11, the player wins– If sum equals 2, 3 or 12, the player loses– Any other sum (4, 5, 6, 8, 9, 10) is that

player’s point• Keep rolling dice until…

– Sum matches player point• Player wins

– Sum equals 7 • Player loses

Page 12: Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize

12

Game of Chance

• Note the adapted version of Figure 6.9– An application

• Note– ___________ class variables– Public ____________– The toUpperClass function requires Character. (a _________________ class)

Page 13: Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize

13

Scope of Declarations• Scope of a parameter is body of __________

where declaration appears• Scope of local variable is from point of

declaration to ______________• Scope of label in labeled break/continue is

statement enclosed by labeled statement• Scope of local-variable in initialization of for( ) is

for ___________ of for and rest of header• Scope of method or field of class is body of class

Page 14: Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize

14

Methods of Class JApplet• public void init()

– Called once by ____________ container when applet loaded

– Initializes fields, creates GUI components, load sounds, images

• public void start()– Called ________________ init– Completes execution– Called again if browser _______________ to this

page

Page 15: Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize

15

Methods of Class JApplet• public void paint (Graphics g)

– Called after init finishes and start has started– Called each time applet needs to be ______________– Typical actions include drawing with Graphics object g

passed to the method• public void stop()

– Called when applet should stop (when browser leaves the HTML page)

– Performs tasks to suspend applet's execution• public void destroy()

– Called when applet is being removed from memory– Destroys resources allocated to the applet

Page 16: Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize

16

Method Overloading

• Multiple methods with same name can be declared in same class

• Methods must have different signatures– Different numbers, types, order of parameters

• Example– Figure 6.12– Multiple square methods

Page 17: Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize

17

RecursionRecursive method• Calls itself (directly or indirectly) through another

method• Method knows how to solve only a base case• Method divides problem

– Base case– Simpler problem – Method now divides simpler

problem until solvable• Recursive call• Recursive step

Page 18: Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize

18

2! = 2 * 1 = 2 is returned

(a) Sequence of recursive calls.

(b) Values returned from each

recursive call.

Final value = 120

5! = 5 * 24 = 120 is returned

4! = 4 * 6 = 24 is returned

3! = 3 * 2 = 6 is returned

1 returned

5!

1

4 * 3!

3 * 2!

2 * 1!

5!

1

4 * 3!

3 * 2!

2 * 1!

5 * 4! 5 * 4!

Recursive Evaluation of 5!

Page 19: Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize

19

Recursive Factorial Program

• Function factorial() in Figure 6.15• Note

– Initial call to factorial()– Anchor or base case– Recursive call

Page 20: Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize

20

Recursive Fibonacci Program

• Consider function from program   public long fibonacci( long n )   {      // base case      if ( n == 0 || n == 1 )           return n;      // recursive step      else         return fibonacci( n - 1 ) + fibonacci( n - 2 );   }  // end method fibonacci

• Note why this would be extremely inefficient

Page 21: Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize

21

Recursive Fibonacci Program

return

return

+

+ return 1

return 1

fibonacci( 2 ) fibonacci( 1 )

fibonacci( 1 ) fibonacci( 0 )

return 0

fibonacci( 3 )

Page 22: Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize

22

Recursion vs. Iteration

• Both the recursive functions shown can be done with either for or while loops

• In fact they are done more efficiently iteratively• Recursion has overhead

– Processor time– Memory space

• General rule: If it can be done either recursively or iteratively … choose iteratively