1. Trang chủ
  2. » Thể loại khác

Java - profthinh ď jhtp5_06

62 219 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Cấu trúc

  • Chapter 6 - Methods

  • 6.1 Introduction

  • 6.2 Program Modules in Java

  • PowerPoint Presentation

  • 6.3 Math-Class Methods

  • Slide 6

  • 6.4 Methods Declarations

  • 6.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 stores

  • SquareIntegers.java Line 38 y is the parameter of method square Line 40 Method square returns the square of y

  • 6.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 maximum

  • Maximum.java Line 46 Method maximum returns value from method max of class Math

  • 6.5 Argument Promotion

  • Slide 15

  • 6.6 Java API Packages

  • Slide 17

  • 6.7 Random-Number Generation

  • RandomIntegers.java Line 16 Produce integers in range 1-6 Line 16 Math.random returns doubles. We cast the double as an int

  • RandomIntegers.java

  • RollDie.java Line 14 Produce integers in range 1-6 Lines 17-43 Increment appropriate frequency counter, depending on randomly generated number

  • RollDie.java

  • Slide 23

  • 6.8 Example: A Game of Chance

  • Craps.java Line 24 Method init starts JApplet and initializes GUI

  • Craps.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 point

  • Craps.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 loses

  • Craps.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 loses

  • Craps.java Lines 121-122 Method rollDice uses Math.random to simulate rolling two dice Line 131 return dice sum

  • Craps.java

  • Craps.java

  • 6.9 Scope of Declarations

  • Scoping.java Line 11 field x Line 26 Local variable x Line 28 Method start uses local variable x

  • Scoping.java Line 42 Recreate variable x and initialize it to 25 Lines 40-50 Method useLocal uses local variable x

  • Scoping.java Lines 53-61 Method useField uses field x

  • 6.16 Methods of Class JApplet

  • Slide 37

  • 6.15 Method Overloading

  • MethodOverload.java Lines 22-29 Method square receives an int as an argument

  • MethodOverload.java Lines 32-39 Overloaded method square receives a double as an argument

  • MethodOverload.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 Recursion

  • Slide 43

  • FactorialTest.java Line 21 Invoke method factorial

  • FactorialTest.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 call

  • 6.13 Example Using Recursion: The Fibonacci Series

  • FibonacciTest.java

  • FibonacciTest.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 fibonacci

  • FibonacciTest.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 call

  • Slide 50

  • Slide 51

  • Slide 52

  • 6.14 Recursion vs. Iteration

  • 6.14 Recursion vs. Iteration (cont.)

  • Slide 55

  • 6.15 (Optional Case Study) Thinking About Objects: Identifying Class Operations

  • 6.15 Thinking About Objects (cont.)

  • Slide 58

  • Slide 59

  • 6.17 Thinking About Objects (cont.)

  • Slide 61

  • Slide 62

Nội dung

1 Chapter - Methods Outline 6.1 Introduction 6.2 Program Modules in Java 6.3 Math-Class Methods 6.4 Method Declarations 6.5 Argument Promotion 6.6 Java API Packages 6.7 Random-Number Generation 6.8 Example: A Game of Chance 6.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 6.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 6.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 boss worker1 worker4 worker2 worker3 worker5 Fig 6.1 Hierarchical boss-method/worker-method relationship 2003 Prentice Hall, Inc All rights reserved 6.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 Method abs( x ) ceil( x ) cos( x ) exp( x ) floor( x ) log( x ) max( x, y ) min( x, y ) pow( x, y ) sin( x ) sqrt( x ) tan( x ) Fig 6.2 Math-class methods Description Example 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 rounds x to the smallest integer not less than x ceil( 9.2 ) is 10.0 ceil( -9.8 ) is -9.0 trigonometric cosine of x (x is in radians) cos( 0.0 ) is 1.0 exponential method ex exp( 1.0 ) is 2.71828 exp( 2.0 ) is 7.38906 rounds x to the largest integer not greater than x floor( 9.2 ) is 9.0 floor( -9.8 ) is -10.0 natural logarithm of x (base e) log( Math.E ) is 1.0 log( Math.E * Math.E ) is 2.0 larger value of x and y (this method also has float, int and long max( 2.3, 12.7 ) is 12.7 versions) max( -2.3, -12.7 ) is -2.3 smaller value of x and y (this method also has float, int and long min( 2.3, 12.7 ) is 2.3 versions) min( -2.3, -12.7 ) is -12.7 x raised to the power y (xy) pow( 2.0, 7.0 ) is 128.0 pow( 9.0, 0.5 ) is 3.0 trigonometric sine of x (x is in radians) sin( 0.0 ) is 0.0 square root of x sqrt( 900.0 ) is 30.0 sqrt( 9.0 ) is 3.0 trigonometric tangent of x (x is in radians) tan( 0.0 ) is 0.0 2003 Prentice Hall, Inc All rights reserved 6.4 Methods Declarations • Methods – Allow programmers to modularize programs • Makes program development more manageable • Software reusability • Avoid repeating code – Local variables • Declared in method declaration – Parameters • Communicates information between methods via method calls 2003 Prentice Hall, Inc All rights reserved 6.4 Method Declarations (Cont.) • Programmers can write customized methods 2003 Prentice Hall, Inc All rights reserved 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Outline // Fig 6.3: SquareIntegers.java // Creating and using a programmer-defined method import java.awt.Container; import javax.swing.*; SquareIntegers java Declare result to store square of number public class SquareIntegers extends JApplet { // set up GUI and calculate squares of integers from to 10 Method init invokes public void init() method square (next slide) { // JTextArea to display results JTextArea outputArea = new JTextArea(); // get applet's content pane (GUI component display area) Container container = getContentPane(); Line 21 Declare result to store square of number Line 26 Method init invokes method square Method square returns int // attach outputArea to container container.add( outputArea ); int result; String output = ""; // store result of call to method square // String containing results that result stores Line 26 Method square returns int that result stores // loop 10 times for ( int counter = 1; counter

Ngày đăng: 11/12/2017, 19:43

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN

w