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

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
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 3 Control Statements.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 2 Primitive Data Types and Operations.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 1 Introduction to Computers, Programs, and Java.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 0 course intro Programming Language(Using Java).ppt
- 复旦大学:《程序设计》课程教学资源(Java Lab)Game of Life_题目要求.pdf
- 复旦大学:《程序设计》课程教学资源(Java Lab)Game of Life.ppt
- 复旦大学:《程序设计》课程教学资源(Java Lab)验证哥德巴赫猜想.ppt
- 复旦大学:《程序设计》课程教学资源(Java Lab)Ansi Temple.ppt
- 复旦大学:《程序设计》课程教学资源(Java Lab)富人Smith的生日 If else Switch.ppt
- 复旦大学:《程序设计》课程教学资源(Java Lab)3.ppt
- 复旦大学:《程序设计》课程教学资源(Java Lab)环境配置与第一个程序的运行.ppt
- 复旦大学:《程序设计》课程教学资源(Java Lab)第一个java程序.ppt
- 《高级Web技术》参考资料:语义Web课件 A Semantic Web Primer_Chapter8 Conclusion and Outlook.ppt
- 《高级Web技术》参考资料:语义Web课件 A Semantic Web Primer_Chapter7 Ontology Engineering.ppt
- 《高级Web技术》参考资料:语义Web课件 A Semantic Web Primer_Chapter6 Applications.ppt
- 《高级Web技术》参考资料:语义Web课件 A Semantic Web Primer_Chapter5 Logic and Inference:Rules.ppt
- 《高级Web技术》参考资料:语义Web课件 A Semantic Web Primer_Chapter4 Web Ontology Language:OWL.ppt
- 《高级Web技术》参考资料:语义Web课件 A Semantic Web Primer_Chapter3 Describing Web Resources in RDF.ppt
- 《高级Web技术》参考资料:语义Web课件 A Semantic Web Primer_Chapter2 Structured Web Documents in XML.ppt
- 《高级Web技术》参考资料:语义Web课件 A Semantic Web Primer_Chapter1 The Semantic Web Vision.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 5 Arrays.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 6 Objects and Classes.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 7 String.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 8 Inheritance and Polymorphism.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 9 Abstract Classes and Interfaces.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 10 Object-Oriented Modeling(oom).ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 11 Getting Started with GUI Programming.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 12 Event-Driven Programming.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 13 Creating User Interfaces.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 14 Applets, Images, and Audio.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 15 Exceptions and Assertions.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 16 Simple Input and Output.ppt
- 复旦大学:《信息安全》教学课件_01 Classical Encryption Techniques.pdf
- 复旦大学:《信息安全》教学课件_02 Classical Encryption Techniques(cont.).pdf
- 复旦大学:《信息安全》教学课件_03 Modern Block Ciphers.pdf
- 复旦大学:《信息安全》教学课件_04 Public Key Cryptography, RSA.pdf
- 复旦大学:《信息安全》教学课件_05 Message authentication and Hash function.pdf
- 复旦大学:《信息安全》教学课件_06 The Intro to Information Security.pdf
- 复旦大学:《信息安全》教学课件_07-08 Public Key Infrastructure(PKI)公钥基础设施——公钥技术的应用.pdf
- 复旦大学:《信息安全》教学课件_09 Authentication and supplements.pdf