DOC PREVIEW
LETU COSC 2103 - Methods

This preview shows page 1-2-21-22 out of 22 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

MethodsProgram Modules in JavaPredefined MethodsMethod Declaration SyntaxArgument PromotionSlide 6Method DefinitionsSlide 8Java API PackagesRandom-Number GenerationGame of ChanceSlide 12Scope of DeclarationsMethods of Class JAppletSlide 15Method OverloadingRecursionRecursive Evaluation of 5!Recursive Factorial ProgramRecursive Fibonacci ProgramSlide 21Recursion vs. IterationMethodsChapter 62Program 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 method3Predefined 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);4Method 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 method5Argument 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()6Argument Promotion•Promotion rules–Specify how to ______________ types without data loss7Method 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.java8Method 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 needed9Java API Packages•Predefined classes grouped into categories of related classes–Called ________________•Called the Java Application Programming Interface (API)•Note the often used API packages10Random-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.811Game of ChanceRules 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 loses12Game of Chance•Note the adapted version of Figure 6.9–An application•Note–___________ class variables–Public ____________–The toUpperClass function requires Character. (a _________________ class)13Scope 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 class14Methods 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 page15Methods 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 applet16Method 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 methods17RecursionRecursive 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 step182! = 2 * 1 = 2 is returned(a) Sequence of recursive calls.(b) Values returned from each recursive call.Final value = 1205! = 5 * 24 = 120 is returned4! = 4 * 6 = 24 is returned3! = 3 * 2 = 6 is returned1 returned5!14 * 3!3 * 2!2 * 1!5!14 * 3!3 * 2!2 * 1!5 * 4!5 * 4!Recursive Evaluation of 5!19Recursive Factorial Program•Function factorial() in Figure 6.15•Note–Initial call to factorial()–Anchor or base case–Recursive call20Recursive 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 inefficient21Recursive Fibonacci Programreturnreturn++return 1return 1fibonacci( 2 ) fibonacci( 1 )fibonacci( 1 ) fibonacci( 0 )return 0fibonacci( 3 )22Recursion 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


View Full Document

LETU COSC 2103 - Methods

Documents in this Course
Arrays

Arrays

16 pages

Templates

Templates

17 pages

Methods

Methods

22 pages

Arrays

Arrays

11 pages

Load more
Download Methods
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Methods and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Methods 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?