Programming Logic and Design, Ninth Edition 9-1
Chapter 9
Advanced Modularization Techniques
A Guide to this Instructor’s Manual:
We have designed this Instructor’s Manual to supplement and enhance your teaching
experience through classroom activities and a cohesive chapter summary.
This document is organized chronologically, using the same headings that you see in the
textbook. Under the headings you will find: lecture notes that summarize the section, Teacher
Tips, Classroom Activities, and Lab Activities. Pay special attention to teaching tips and
activities geared towards quizzing your students and enhancing their critical thinking skills.
In addition to this Instructor’s Manual, our Instructor’s Resources also contain PowerPoint
Presentations, Test Banks, and other supplements to aid in your teaching experience.
At a Glance
Instructor’s Manual Table of Contents
Overview
Chapter Objectives
Teaching Tips
Quick Quizzes
Class Discussion Topics
Additional Projects
Additional Resources
Key Terms
© 2017 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Programming Logic and Design, Ninth Edition 9-2
Lecture Notes
Overview
Chapter 9 introduces students to several method-related topics. The chapter begins with
a discussion of the different number of parameters that can be passed to a method: zero,
one, or multiple. Students will learn how to declare and call each type of method.
Students will next learn about returning values from methods and passing arrays into
methods. The following sections deal with method overloading, hiding the mechanics of
a method, understanding to what degree methods depend on each other, using
predefined methods, method design issues, and recursion.
Chapter Objectives
In this chapter, your students will learn about:
The parts of a method
Methods with no parameters
Methods that require parameters
Methods that return a value
Passing arrays to methods
Overloading methods
Using predefined methods
Method design issues, including implementation hiding, cohesion, and coupling
Recursion
Teaching Tips
The Parts of a Method
1. Remind students of the definition of a method and describe the features of methods,
including the number of methods permitted in a program, method naming rules, the
composition of a method, and variable scope rules. The calling program or method is
the called method’s client.
2. Define the terms method header, method body, implementation, method return
statement.
3. Explain that variables can be declared within the method, thus making them local to
that method.
4. Note the difference between a local declaration and a global declaration.
© 2017 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Programming Logic and Design, Ninth Edition 9-3
5. Explain the importance of providing parameters to methods, and describe how a
parameter is declared within a method header.
Teaching
Remind students that the topics discussed above were covered in Chapter 2.
Tip
Emphasize that a programmer need not know the details of how a meth
Teaching in order to use the method. A good example to illustrate this would be t
Tip students the actual code of the C++ statements cout and cin. Then ex
all programmers need to include are the arguments.
Using Methods with No Parameters
1. Using the example program in Figure 9-1, discuss the different options for rewriting the
program to include explanatory text in a chosen language.
2. Explain that when calling a method from a program or other method, you should know
these four things:
What the method does in general— why you are calling the method
The name of the called method
What type of information to send to the method, if any
What type of return data to expect from the method, if any
Creating Methods that Require Parameters
1. Explain the concept of arguments to the method or parameters to the method.
Teaching
Many programmers use the terms argument and parameter interchangeably.
Tip
2. Review the need for a parameter list.
3. Define a method’s signature.
4. Walk through the moon weight program using Figures 9-3 and 9-4 as an example of a
program that passes an argument to a method.
5. Explain what happens to a variable when it is passed by value into a method.
6. Review Figures 9-5 and 9-6 showing the program that calls a method in which the
argument and parameter have the same identifier.
© 2017 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Programming Logic and Design, Ninth Edition 9-4
Creating Methods that Require Multiple Parameters
1. Note that methods can accept more than one parameter. The parameters are listed in the
method header’s parentheses separated by commas. Use the computeTax() method
shown in Figure 9-7 as an example. Stress that parameters must be passed into a method
in the same order that they are defined in the method header parentheses.
2. Explain the difference between actual parameters and formal parameters.
Creating Methods that Return a Value
1. Describe how methods send information back to their calling program or method via a
return value. Explain how to declare the return type for a method. Note that methods
need not return a value. If a method does not return a value, it is called a void method.
A method’s return type is known more succinctly as a method’s type, method’s return
type is known more succinctly as a method’s type.
2. Review the example program in Figure 9-8. Point out the use of the return statement
and that it is not the same as the return statement in the main routine.
3. Discuss the different ways that a program can use the value returned by a method either
by assigning it to a variable or by outputting it. Review Figure 9-9 as an example of a
program that uses a method’s return value directly without storing it.
4. Explain that a program statement that calls a method requires more computer time and
resources than a statement that does not call any outside methods. Programmers use the
term overhead to describe any extra time and resources required by an operation.
5. Note that while it is technically possible to have more than one return statement in a
method, this is not correct structured logic. See Figure 9-10 for an example. The correct
structured logic approach is illustrated in Figure 9-11.
Using an IPO Chart
1. Using the example in Figure 9-12, describe the use of an IPO chart. Explain why and
when programmers use this type of tool.
Quick Quiz 1
1. (True/False) When you pass a data item into a method from a calling program, it is
called a parameter to the method.
Answer: False
© 2017 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Programming Logic and Design, Ninth Edition 9-5
2. (True/False) Each time a method executes, any parameter variables listed in the method
header are redeclared.
Answer: True
3. The ____ for a method indicates the data type of the value that the method will send
back to the location where the method call was made.
Answer: return type
4. A(n) ____ chart is a tool that identifies and categorizes each item needed within the
method as pertaining to input, processing, or output.
Answer: IPO
Passing an Array to a Method
1. Note that an element from an array can be passed to a method like any other variable or
constant. An example is shown in Figures 9-13 and 9-14.
2. Explain that an entire array can be passed to a method. The method’s parameter list
includes the array name and data type followed by square brackets. When an array is
passed to a method, it is passed by reference. This means that any changes made to the
array data will persist when control returns to the calling program. To illustrate this,
review the program in Figures 9-15 and 9-16.
Teaching Students may find it useful to write a small program to test the pass by reference
Tip principle for themselves.
Overloading Methods
1. Define the term overloading. Be sure to point out the fact that overloaded methods
must have different parameter lists. Explain how the compiler chooses which method to
execute based on the arguments passed to the method call.
Teaching Learn more about method overloading:
Tip http://en.wikipedia.org/wiki/Method_overloading.
2. Review the example programs in Figures 9-17 and 9-18 that demonstrate method
overloading.
3. Note that overloading a method is an example of polymorphism—the ability of a
method to act appropriately according to the context. Literally, polymorphism means
“many forms.”
© 2017 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Programming Logic and Design, Ninth Edition 9-6
4. Explain when you overload a method, you write multiple methods with a shared name
but different parameter lists. When you call an overloaded method, the language
translator understands which version of the method to use based on the arguments used.
A good way to illustrate overloading is to use a math example. A single method
may be called to find the area of a square (one argument is passed because all
sides are equal) or the area of a rectangle (two arguments are passed: one for
length and one for width). The code determines what method to call based on the
methods’ signatures.
Sample code:
public class SquareRec
{
Teaching // This method is for a square
Tip public static double Area(double side)
{
return side * side;
}
// This method is for a rectangle
public static double Area(double width, double length)
{
return width * length;
}
Avoiding Ambiguous Methods
1. Stress that methods with the same name and same parameter lists but different return
types are not overloaded. Instead, they are ambiguous methods. Use Figure 9-19 to
illustrate this issue.
Using Predefined Methods
1. Describe the different types of prewritten methods that programmers may use. Discuss
the benefits of using prewritten methods.
Teaching Point out that in the open source community, there are large libraries of
Tip prewritten methods in many programming languages.
2. Review the four details required to use a prewritten method, which are outlined in the
text.
© 2017 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Programming Logic and Design, Ninth Edition 9-7
Method Design Issues: Implementation Hiding, Cohesion, and Coupling
1. Introduce the program qualities listed in the text and discussed in the following
sections.
Understanding Implementation Hiding
1. Define the term implementation hiding, and explain why this is beneficial for the
method’s client.
2. Stress the three things (name, type sent, and return type) that methods must know when
calling another method.
3. Define the interface to the method that calling methods need to understand.
4. Explain that programmers refer to hidden implementation as a black box.
Microsoft Windows is a black box. We know what to put into it (i.e., how to use
Teaching
the interface) and we know what we got out of it, but we have no idea how it
Tip
works internally.
Increasing Cohesion
1. Discuss beginning programmers’ difficulty when deciding what to include in a method.
Introduce the concept of cohesion. Explain that when methods demonstrate functional
cohesion, all of the operations contribute to a single goal.
An example of a method that is not functionally cohesive is a method called File.
Teaching The File method can open the file, lock the file, read the file, modify the file, and
Tip close the file. That’s too many different things for one method to do. Ask
students to fix it.
2. Explain how to determine whether a method is functionally cohesive. Note that some
programming environments implement arbitrary rules about method length, but it is
best to focus on cohesion.
Reducing Coupling
1. Define the term coupling, and explain how to determine whether two methods have
tight coupling or loose coupling.
© 2017 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Programming Logic and Design, Ninth Edition 9-8
2. Review the method used to determine the degree of coupling outlined in the text.
Understanding Recursion
1. Explain that recursion occurs when a method is defined in terms of itself and that a
recursive method calls itself. A sample recursive method is shown in Figure 9-20, and
the output is shown in Figure 9-21.
2. Describe how the computer stores the address to which a method should return on the
stack. Use the example in the text to illustrate how this works.
3. Note that the stack has a limited size; so if a recursive method is not properly written, it
will cause the stack to overflow.
4. Explain that recursive cases are the input values that cause a method to recur and the
input values that make the recursion stop is called the base case or terminating case.
Teaching Stress the importance of providing a terminating condition for a recursive
Tip function.
5. Review the program listing in Figure 9-22 and output in Figure 9-23. Point out the
conditional statement that checks whether number is equal to 1. Walk through the
execution of this program using the outline in the text. Note that a nonrecursive version
of the program can also be written, as shown in Figure 9-24.
Quick Quiz 2
1. (True/False) When you pass an array element to a method, any changes to it are not
permanent and are not reflected in the array declared in the main program.
Answer: True
2. Arrays, unlike simple built-in types, are passed by ____.
Answer: reference
3. When you overload a method, you write multiple methods with a shared name but
different ____ lists.
Answer: parameter
4. Methods with identical names that have identical parameter lists but different return
types are ____.
Answer: ambiguous
© 2017 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Programming Logic and Design, Ninth Edition 9-9
5. (True/False) Functionally cohesive methods usually are less reliable than those that
have low cohesion.
Answer: False
6. ____ occurs when a method calls itself.
Answer: Recursion
Class Discussion Topics
1. How does a programmer determine what arguments should be passed into a method
versus provided by other means (global or local to the method)?
2. How does a programmer determine the methods that should make up a program,
bearing in mind the principles of information hiding, cohesion, and coupling?
3. One advantage to writing a program that is subdivided into methods is that such a
structure allows different programmers to write separate methods, thus dividing the
work. Would you prefer to write a large program by yourself, or to work on a team in
which each programmer produces one or more methods? Why?
Student answers will differ. Some students will prefer the team approach whereas others
will prefer the autonomy of developing a major system on their own. Those who are
drawn to programming often are loners who prefer to work without the "interference" of
others.
4. In this chapter, you learned that hidden implementations are often said to exist in a
black box. What are the advantages and disadvantages to this approach in both
programming and real life?
Most people have no idea how images and sound get to their television screen. It is a
benefit that implementation hiding exists in that you do not need to worry about the
details; instead, you just enjoy the programs. The drawback is that it is virtually
impossible for you to fix your own television or even diagnose the problem if one
should arise. Similarly, in programming, you can use methods written by others, and
that can be a great convenience, but, most of the time, you are not allowed to tweak the
methods to your specifications.
Additional Projects
1. Ask students to further adapt the program shown in Figure 9-1 to use an additional
parameter to the method.
© 2017 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Programming Logic and Design, Ninth Edition 9-10
2. Ask students to explore recursive methods on the Internet. What are some examples of
programming problems that can be solved recursively?
Additional Resources
1. Article about parameters:
http://en.wikipedia.org/wiki/Parameter_(computer_science)
2. Article about overloading a method based on return type in Java:
http://today.java.net/pub/a/today/2008/07/31/return-type-based-method-
overloading.html
3. Learn more about cohesion:
http://www.freetutes.com/systemanalysis/sa6-cohesion.html
4. Article about recursion:
http://en.wikipedia.org/wiki/Recursion
Key Terms
Ambiguous methods – those that the compiler cannot distinguish because they have
the same name and parameter types.
Argument to the method – a value passed to a method in the call to the method.
Base case or terminating case of a recursive method – describes the value that ends
the repetition.
Black box – an analogy that programmers use to refer to hidden method
implementation details.
Client – a program or other method that uses the method.
Cohesion – a measure of how the internal statements of a method serve to accomplish
the method’s purpose.
Coupling – a measure of the strength of the connection between two program methods.
Functional cohesion – occurs when all operations in a method contribute to the
performance of only one task. Functional cohesion is the highest level of cohesion; you
should strive for it in all methods you write.
Global – describes data items known to all the methods in a program.
Implementation – describes the body of a method or the statements that carry out the
tasks of a method.
Implementation hiding – a programming principle that describes the encapsulation of
method details.
Interface to a method – includes the method’s return type, name, and arguments. It is
the part that a client sees and uses.
IPO chart – identifies and categorizes each item needed within the method as
pertaining to input, processing, or output.
Local – variables are declared within each method that uses them.
© 2017 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Programming Logic and Design, Ninth Edition 9-11
Loose coupling – occurs when methods do not depend on others.
Method – a program module that contains a series of statements that carry out a task.
Method body – contains all the statements in the method.
Method header – precedes a method; the header includes the method identifier and
possibly other necessary identifying information.
Method return statement – marks the end of a method and identifies the point at
which control returns to the calling method.
Method’s type – the type of its return value.
Overhead – refers to all the resources and time required by an operation.
Overload a method – create multiple versions with the same name but different
parameter lists.
Overloading – involves supplying diverse meanings for a single identifier.
Parameter list – all the data types and parameter names that appear in a method header.
Parameter to a method – a data item passed into the method from the outside.
Passed by reference – the method receives the actual memory address item. Arrays are
passed by reference.
Passed by value – a copy of its value is sent to the method and stored in a new memory
location accessible to the method.
Polymorphism – the ability of a method to act appropriately depending on the context.
Recursion – occurs when a method is defined in terms of itself.
Recursive cases – describes the input values that cause a recursive method to execute
again.
Recursive method – a method that calls itself.
Return type – indicates the data type of the value that the method will send back to the
location where the method call was made.
Signature – includes method name and argument list.
Stack – a memory location that holds addresses to which methods should return.
Tight coupling – occurs when methods excessively depend on each other; it makes
programs more prone to errors.
Void method – returns no value.
© 2017 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.