DOC PREVIEW
LETU COSC 2103 - Chapter 6 - Methods

This preview shows page 1-2-3-4-29-30-31-32-59-60-61-62 out of 62 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 62 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 62 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 62 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 62 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 62 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 62 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 62 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 62 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 62 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 62 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 62 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 62 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 62 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Chapter 6 - Methods6.1 Introduction6.2 Program Modules in JavaPowerPoint Presentation6.3 Math-Class MethodsSlide 66.4 Methods Declarations6.4 Method Declarations (Cont.)SquareIntegers.java Line 21 Declare result to store square of number Line 26 Method init invokes method square Line 26 Method square returns int that result storesSquareIntegers.java Line 38 y is the parameter of method square Line 40 Method square returns the square of y6.4 Method Declarations (cont.)Maximum.java Lines 13-18 User inputs three Strings Lines 21-23 Convert Strings to doubles Line 25 Method init passes doubles as arguments to method maximumMaximum.java Line 46 Method maximum returns value from method max of class Math6.5 Argument PromotionSlide 156.6 Java API PackagesSlide 176.7 Random-Number GenerationRandomIntegers.java Line 16 Produce integers in range 1-6 Line 16 Math.random returns doubles. We cast the double as an intRandomIntegers.javaRollDie.java Line 14 Produce integers in range 1-6 Lines 17-43 Increment appropriate frequency counter, depending on randomly generated numberRollDie.javaSlide 236.8 Example: A Game of ChanceCraps.java Line 24 Method init starts JApplet and initializes GUICraps.java Lines 33 JTextField that output dice results Line 40 JTextField that output dice results Line 47 JTextField that outputs sum of dice Line 54 JTextField that outputs player’s pointCraps.java Line 59 JButton for rolling dice Line 66 Method invoked when user presses JButton Line 68 Invoke method rollDice Lines 76-80 If sum is 7 or 11, user wins Lines 83-88 If user rolls 2, 3 or 12, user losesCraps.java Lines 91-96 If sum is 4, 5, 6, 8, 9 or 10, that sum is the point Lines 105-109 If sum equals point, user wins; If sum equals 7, user losesCraps.java Lines 121-122 Method rollDice uses Math.random to simulate rolling two dice Line 131 return dice sumCraps.javaCraps.java6.9 Scope of DeclarationsScoping.java Line 11 field x Line 26 Local variable x Line 28 Method start uses local variable xScoping.java Line 42 Recreate variable x and initialize it to 25 Lines 40-50 Method useLocal uses local variable xScoping.java Lines 53-61 Method useField uses field x6.16 Methods of Class JAppletSlide 376.15 Method OverloadingMethodOverload.java Lines 22-29 Method square receives an int as an argumentMethodOverload.java Lines 32-39 Overloaded method square receives a double as an argumentMethodOverload.java Lines 8 and 15 Compiler cannot distinguish between methods with identical names and parameter sets Fig. 6.17 Compiler error messages generated from overloaded methods with identical parameter lists and different return types.6.12 RecursionSlide 43FactorialTest.java Line 21 Invoke method factorialFactorialTest.java Lines 29-30 Test for base case (method factorial can solve base case) Line 34 Else return simpler problem that method factorial might solve in next recursive call6.13 Example Using Recursion: The Fibonacci SeriesFibonacciTest.javaFibonacciTest.java Line 43 Method actionPerformed is invoked when user presses Enter Line 45 We use long, because Fibonacci numbers become large quickly Lines 48-53 Pass user input to method fibonacciFibonacciTest.java Lines 65-66 Test for base case (method fibonacci can solve base case) Lines 69-70 Else return simpler problem that method fibonacci might solve in next recursive callSlide 50Slide 51Slide 526.14 Recursion vs. Iteration6.14 Recursion vs. Iteration (cont.)Slide 556.15 (Optional Case Study) Thinking About Objects: Identifying Class Operations6.15 Thinking About Objects (cont.)Slide 58Slide 596.17 Thinking About Objects (cont.)Slide 61Slide 62 2003 Prentice Hall, Inc. All rights reserved.1Chapter 6 - MethodsOutline6.1 Introduction6.2 Program Modules in Java6.3 Math-Class Methods6.4 Method Declarations 6.5 Argument Promotion6.6 Java API Packages6.7 Random-Number Generation6.8 Example: A Game of Chance6.9 Scope of Declarations 6.10 Methods of Class JApplet 6.11 Method Overloading 6.12 Recursion 6.13 Example Using Recursion: The Fibonacci Series 6.14 Recursion vs. Iteration 6.15 (Optional Case Study) Thinking About Objects: Identifying Class Operations 2003 Prentice Hall, Inc. All rights reserved.26.1 Introduction•Modules–Small pieces of a problem•e.g., divide and conquer –Facilitate design, implementation, operation and maintenance of large programs 2003 Prentice Hall, Inc. All rights reserved.36.2 Program Modules in Java•Modules in Java–Methods–Classes•Java API provides several modules•Programmers can also create modules–e.g., programmer-defined methods•Methods–Invoked by a method call–Returns a result to calling method (caller)–Similar to a boss (caller) asking a worker (called method) to complete a task 2003 Prentice Hall, Inc. All rights reserved.4Fig. 6.1 Hierarchical boss-method/worker-method relationship.bossworker1 worker2 worker3worker4 worker5 2003 Prentice Hall, Inc. All rights reserved.56.3 Math-Class Methods•Class java.lang.Math–Provides common mathematical calculations–Calculate the square root of 900.0:•Math.sqrt( 900.0 )–Method sqrt belongs to class Math•Dot (.) allows access to method sqrt–The argument 900.0 is located inside parentheses 2003 Prentice Hall, Inc. All rights reserved.6Method Description Example abs( x ) absolute value of x (this method also has float, int and long versions) abs( 23.7 ) is 23.7 abs( 0.0 ) is 0.0 abs( -23.7 ) is 23.7 ceil( x ) rounds x to the smallest integer not less than x ceil( 9.2 ) is 10.0 ceil( -9.8 ) is -9.0 cos( x ) trigonometric cosine of x (x is in radians) cos( 0.0 ) is 1.0 exp( x ) exponential method ex exp( 1.0 ) is 2.71828 exp( 2.0 ) is 7.38906 floor( x ) rounds x to the largest integer not greater than x floor( 9.2 ) is 9.0 floor( -9.8 ) is -10.0 log( x ) natural logarithm of x (base e) log( Math.E ) is 1.0 log( Math.E * Math.E ) is 2.0 max( x, y ) larger value of x and y (this method also has float, int and long versions) max( 2.3, 12.7 ) is 12.7 max( -2.3, -12.7 ) is -2.3 min( x, y ) smaller value of x and y (this method also has float, int and long versions) min( 2.3, 12.7 ) is 2.3 min( -2.3, -12.7 ) is -12.7 pow( x, y ) x raised to the power y (xy) pow( 2.0, 7.0 ) is 128.0 pow( 9.0, 0.5 ) is 3.0 sin( x ) trigonometric sine of x (x is in radians) sin( 0.0 ) is 0.0 sqrt( x ) square root of x sqrt( 900.0 ) is 30.0 sqrt( 9.0 ) is 3.0 tan( x ) trigonometric tangent of x (x is in radians) tan( 0.0 ) is 0.0


View Full Document

LETU COSC 2103 - Chapter 6 - Methods

Documents in this Course
Arrays

Arrays

16 pages

Templates

Templates

17 pages

Methods

Methods

22 pages

Methods

Methods

22 pages

Arrays

Arrays

11 pages

Load more
Download Chapter 6 - 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 Chapter 6 - 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 Chapter 6 - 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?