当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 4 Methods

资源类别:文库,文档格式:PPT,文档页数:91,文件大小:1.12MB,团购合买
⚫ To create methods, invoke methods, and pass arguments to a method (§4.2-4.4). ⚫ To use method overloading and know ambiguous overloading (§4.5). ⚫ To determine the scope of local variables (§4.6). ⚫ To learn the concept of method abstraction (§4.7). ⚫ To know how to use the methods in the Math class (§4.8). ⚫ To design and implement methods using stepwise refinement (§4.10). ⚫ To write recursive methods (§4.11 Optional). ⚫ To group classes into packages (§4.12 Optional).
点击下载完整版文档(PPT)

Chapter 4 Methods Prerequisites for Part I Basic computer skills such as using Windows Internet Explorer, and Microsoft Word Chapter 1 Introduction to Computers, Programs, and java Chapter 2 Primitive Data Types and Operations hapter 3 Control Statements 条条大路通罗马 hapter 5 arrays Introduction to Java Programming, revised by Dai-kaiyu

Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 4 Methods Chapter 1 Introduction to Computers, Programs, and Java Chapter 2 Primitive Data Types and Operations Chapter 3 Control Statements Chapter 5 Arrays Chapter 4 Methods Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word Prerequisites for Part I 条条大路通罗马

Objectives o To create methods invoke methods and pass arguments to a method($4.2-4. 4) o To use method overloading and know ambiguous overloading(§45) o To determine the scope of local variables($4.6) To learn the concept of method abstraction($4.7) To know how to use the methods in the math class §48) To design and implement methods using stepwise refinement(§4.10) ● To write recursive methods(§4.110 ption) o To group classes into packages(8 4.12 Optional Introduction to Java Programming, revised by Dai-kaiyu

Liang,Introduction to Java Programming,revised by Dai-kaiyu 2 Objectives ⚫ To create methods, invoke methods, and pass arguments to a method (§4.2-4.4). ⚫ To use method overloading and know ambiguous overloading (§4.5). ⚫ To determine the scope of local variables (§4.6). ⚫ To learn the concept of method abstraction (§4.7). ⚫ To know how to use the methods in the Math class (§4.8). ⚫ To design and implement methods using stepwise refinement (§4.10). ⚫ To write recursive methods (§4.11 Optional). ⚫ To group classes into packages (§4.12 Optional)

Introducing Methods o Methods O Allow programmers to modularize programs o Makes program development more manageable o Software reusability Avoid repeating code O Local variables Declared in method definition O Parameters Communicates information between methods via method calls Introduction to Java Programming, revised by Dai-kaiyu

Liang,Introduction to Java Programming,revised by Dai-kaiyu 3 Introducing Methods ⚫ Methods Allow programmers to modularize programs ⚫Makes program development more manageable ⚫Software reusability ⚫Avoid repeating code Local variables ⚫Declared in method definition Parameters ⚫Communicates information between methods via method calls

Similar to a boss(caller) asking a worker (called method)to complete a task man worker worker workers worker workers Hierarchical boss-method/worker-method relationship Introduction to Java Programming, revised by Dai-kaiyu

Liang,Introduction to Java Programming,revised by Dai-kaiyu 4 Hierarchical boss-method/worker-method relationship. main worker1 worker2 worker3 worker4 worker5 Similar to a boss (caller) asking a worker (called method) to complete a task

ntroducing Methods o General format of method Cant be omitted modifier return-value-type method-name( parameter-list method body: declarations and statements o Method can also return values return expression; Can't define a method in another me thod Introduction to Java Programming, revised by Dai-kaiyu

Liang,Introduction to Java Programming,revised by Dai-kaiyu 5 Introducing Methods ⚫General format of method: ⚫Method can also return values: return expression; Can’t define a method in another method modifier return-value-type method-name( parameter-list ) { method body: declarations and statements } Can’t be omitted

Introducing Methods a method is a collection of statements that are grouped together to perform an operation Define a method Invoke a method modifier return value type method name formal parameters method k header public static int max (int numl, int num2)( int z= max(x, y) int resulti method actual parameters if(numl> num2 parameter list (arguments) result return value result return result Introduction to Java Programming, revised by Dai-kaiyu

Liang,Introduction to Java Programming,revised by Dai-kaiyu 6 Introducing Methods A method is a collection of statements that are grouped together to perform an operation. public static int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; } modifier return value type method name formal parameters return value method body method header parameter list Define a method Invoke a method int z = max(x, y); actual parameters (arguments)

Introducing Methods, cont Method signature is the combination of the method name and the parameter list The variables defined in the method header are known as formal parameters .When a method is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument Introduction to Java Programming, revised by Dai-kaiyu

Liang,Introduction to Java Programming,revised by Dai-kaiyu 7 Introducing Methods, cont. •Method signature is the combination of the method name and the parameter list. •The variables defined in the method header are known as formal parameters. •When a method is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument

Introducing Methods, cont a method may return a value. The return Value Type is the data type of the value the method returns If the method does not return a value. the return value Type is the keyword yoid. the return valueType in the main method is void public static void main( String args[]( Introduction to Java Programming, revised by Dai-kaiyu

Liang,Introduction to Java Programming,revised by Dai-kaiyu 8 Introducing Methods, cont. •A method may return a value. The returnValueType is the data type of the value the method returns. • If the method does not return a value, the returnValueType is the keyword void. the returnValueType in the main method is void. public static void main( String args[ ] { }

Calling Methods Example 4. 1 Testing the max method This program demonstrates calling a method max to return the largest of the int values Testmax RI un Introduction to Java Programming, revised by Dai-kaiyu

Liang,Introduction to Java Programming,revised by Dai-kaiyu 9 Calling Methods Example 4.1 Testing the max method This program demonstrates calling a method max to return the largest of the int values TestMax Run

Calling Methods, cont the value of pass the value of j oublic static void main(st args) i public static int max(int numl, int num2) Int int k= max (1, )i if (numl num2 result System. out. println( else The maximum between h+i+ result num and+ j Introduction to Java Programming, revised by Dai-kaiyu

Liang,Introduction to Java Programming,revised by Dai-kaiyu 10 Calling Methods, cont. public static void main(String[] args) { int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k); } public static int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; } pass the value of i pass the value of j

点击下载完整版文档(PPT)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共91页,可试读20页,点击继续阅读 ↓↓
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有