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

Thông tin cơ bản

Định dạng
Số trang 62
Dung lượng 603,5 KB

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