0% found this document useful (0 votes)
50 views35 pages

Farrell JavaP 10e Ch03 PowerPoint

Uploaded by

qkjrm9kf28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views35 pages

Farrell JavaP 10e Ch03 PowerPoint

Uploaded by

qkjrm9kf28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Java Programming,

10e
Chapter 03: Using Methods

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
1
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Chapter Objectives
By the end of this chapter, you should be able to:
03.01 Describe method calls and placement
03.02 Identify the parts of a method
03.03 Add parameters to methods
03.04 Create methods that return values
03.05 Describe blocks and scope
03.06 Overload a method
03.07 Avoid ambiguity

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
3.1 Understanding Method Calls and Placement (1 of
3)
Method
• A program module
• Contains a series of statements
• Carries out a task
Execute a method
• Invoke or call from another method
• Calling method (client method)
• Makes a method call
Called method
• Invoked by a calling method

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
3.1 Understanding Method Calls and Placement (2 of
3)

Figure 3-3: CompanyInfo class that calls the


displayHours() method

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
3.1 Understanding Method Calls and Placement (3 of
3)
Advantages to creating a separate method instead of adding statement to
the original application include:
• Keeps the main () method short and easy to follow
• Using a well-named method makes it easy to see the overall intent
• A method is easily reusable

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
3.2 Understanding Method Construction (1 of 5)
Every method must include two parts:
• Method header
• Provides information about how other methods can interact with it
• Also called method declaration
• Method body
• Contains statements that carry out the work of the method
• Between a pair of curly braces
• Called its implementation

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
3.2 Understanding Method Construction (2 of 5)
Method header
• Optional access specifiers
• Optional static modifier
• A return type
• An identifier
• Parentheses (may or may not be empty)

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
3.2 Understanding Method Construction (3 of 5)
Access Specifiers
• public, private, protected, or package (default)
• Any method that can be used without instantiation an object requires the
static modifier

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
3.2 Understanding Method Construction (4 of 5)
Return Type
• Describes the type of data the method sends back to the calling method
• If no data is returned to the method, the return value is void
Method Name
• Can be any legal identifier
• Must be one word
• No embedded spaces
• Cannot be a Java keyword

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
3.2 Understanding Method Construction (5 of 5)
Parentheses
• Every method header contains a set of parentheses that follow the identifier
• May contain data to be sent to the method
• Fully qualified identifier
• A complete name that includes the class

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 3.1: Knowledge Check
What happens when no data is returned to the method?
a. The return value is void
b. The program stops because of a logical error
c. The result is ambiguous
d. The method is skipped over

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 3.1: Knowledge Check Answer
What happens when no data is returned to the method?

Answer: a. The return value is void


Not all methods return a value to their calling methods. A methods that returns
no data has a return type of void. The main() method in an application must
have a return type of void.

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
3.3 Adding Parameters to Methods (1 of 5)
Arguments
• Data items you use in a call to a method
Parameters
• Data items received by the method
• Methods that receive data produce different results depending on data
received

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
3.3 Adding Parameters to Methods (2 of 5)
Implementation hiding
• Encapsulation of method details within a class
• The calling method needs to understand only the interface to the called
method
Interface
• The only part of a method that the client sees or with which it interacts

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
3.3 Adding Parameters to Methods (3 of 5)
Creating a method that receives a single parameter
• If a method receives a parameter, two additional items are required within
the parentheses:
• Parameter type
• Local name for parameter
Local variable
• Known only within the boundaries of the method

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
3.3 Adding Parameters to Methods (4 of 5)
Creating a Method That Requires Multiple Parameters
• A method can require more than one parameter
• List the arguments within the call to the method
• Separate with commas
• Each parameter requires a data type and identifier
Signature
• Combination of method name and number, types, and order of arguments

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
3.3 Adding Parameters to Methods (5 of 5)
Arguments sent to the method must match the parameters listed in the
method declaration by:
• Number
• Type
• Method signature
• Method name
• Number, types, and order of the arguments

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
3.4 Creating Methods That Return Values (1 of 3)
A method ends when any of the following events occur:
• Method completes all of its statements
• Method throws an exception
• Method reaches a return statement

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
3.4 Creating Methods That Return Values (2 of 3)
Return statement
• Causes a value to be sent from the called method back to the calling method
• The return type can be any type used in Java
Method’s type
• Common name for method’s return type

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
3.4 Creating Methods That Return Values (3 of 3)
Unreachable statements
• A statement placed after a method’s return statement
• Can never execute
• Dead code

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 3.2: Discussion

1. Explain the advantages of creating a separate method that can be called in


the main() method.

2. Describe the roles that the following play in a method: parameter, return
statement, and blocks.

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 3.2: Discussion Debrief
1. Some advantage to creating a separate method include:
• Using a method call keeps the main() method short and easy to
follow.
• It is easy to see the intent of the separate statements when you use a
well-named method.
• Methods are easily reusable.

2. A parameter is used when a method needs to receive a data item. A return


statement causes a method to end, and the program’s logic to return to the
calling method. A block is the code between a pair of curly braces within a
class or method; inner blocks can be nested inside an outer block.

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
3.5 Understanding Blocks and Scope (1 of 5)
Block of code
• Any code between a pair of curly braces
• Also called a block

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
3.5 Understanding Blocks and Scope (2 of 5)
Outer block
• Begins at first opening curly brace
• Ends at last closing curly brace at end of method
Inner block
• Begins with second opening curly brace
• Ends with first closing curly brace
• Contains two executable statements:
• Nested within outer block

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
3.5 Understanding Blocks and Scope (3 of 5)

Figure 3-20: A method with nested blocks

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
3.5 Understanding Blocks and Scope (4 of 5)
Come into scope
• When a variable comes into existence
• After it is declared
Goes out of scope
• At the end of the block in which it is declared
Scope level
• A variable’s block

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
3.5 Understanding Blocks and Scope (5 of 5)
Redeclare a variable
• Declare a variable more than once in a block
• Illegal action
• You can declare a variable within one method of a class and use the same
variable name in another method of the class

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
3.6 Overloading a Method (1 of 2)
Overloading
• Allows you to use one identifier to execute diverse tasks
• Writing multiple methods in the same scope with the same name but
different parameter lists
• Lists must have different numbers of parameters
• Lists must have parameter data types in different orders
• Multiple methods share a name
• Compiler understands which to use based on arguments in the method call

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
3.6 Overloading a Method (2 of 2)

Figure 3-28: The calculateInterest()


method with two double parameters

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
3.7 Learning about Ambiguity (1 of 1)
Ambiguous situation
• Where the compiler cannot determine which method to use
• Caused by overloading
• If a program contains potentially ambiguous situations, it will run problem-
free if you do not make ambiguous method calls

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 3.3: Think, Pair, Share

1. Form pairs or groups of 2 to 4 class members.


2. Create an application with a main() method that prompts the user for
three values to be used in a calculation.
3. Create a method that uses the three values to perform at least four
calculations.
4. Use overloading as part of your programming.
5. Make a note of any ambiguity issues that may occur.

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 3.3: Think, Pair, Share Debrief

• What three values did your program prompt users to input?


• List the type of calculations that your program performed.
• What overload methods did you select? Why?
• What possible ambiguity issues might occur? How will you prevent them?

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Don’t Do It

• Don’t place a semicolon at the end of a method header.


• Don’t try to use a variable that is out of scope.
• Don’t assume that a constant is still a constant when passed to a method’s
parameter.
• Don’t try to overload methods by giving them different return types.

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Self-Assessment

1. Does the order in which methods appear in a class affect the order in which
methods are called or executed?
2. What type of information should you include in the method header?
3. How does creating a method that receives a single parameter differ from
creating a method that receives multiple parameters?
4. What makes a statement an unreachable statement? Why should you avoid
them?
5. What happens when you redeclare a variable?

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Summary

Click the link to review the objectives for this presentation.


Link to Objectives

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.

You might also like