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