Object-Oriented Approach to Programming Logic and Design 4th Edition Joyce Farrell Solutions Manual instant download
Object-Oriented Approach to Programming Logic and Design 4th Edition Joyce Farrell Solutions Manual instant download
https://testbankdeal.com/product/object-oriented-approach-to-
programming-logic-and-design-4th-edition-joyce-farrell-solutions-
manual/
https://testbankdeal.com/product/object-oriented-approach-to-
programming-logic-and-design-4th-edition-joyce-farrell-test-bank/
https://testbankdeal.com/product/programming-logic-and-design-
comprehensive-7th-edition-joyce-farrell-solutions-manual/
https://testbankdeal.com/product/programming-logic-and-design-
comprehensive-7th-edition-joyce-farrell-test-bank/
https://testbankdeal.com/product/management-of-strategy-concepts-
international-edition-10th-edition-ireland-solutions-manual/
Research In Psychology Methods and Design 7th Edition
Goodwin Test Bank
https://testbankdeal.com/product/research-in-psychology-methods-and-
design-7th-edition-goodwin-test-bank/
https://testbankdeal.com/product/society-the-basics-12th-edition-
macionis-test-bank/
https://testbankdeal.com/product/international-financial-reporting-
standards-an-introduction-3rd-edition-needles-solutions-manual/
https://testbankdeal.com/product/revel-for-psychology-1st-edition-
marin-test-bank/
Financial Management Theory and Practice 13th Edition
Brigham Test Bank
https://testbankdeal.com/product/financial-management-theory-and-
practice-13th-edition-brigham-test-bank/
An Object-Oriented Approach to Programming Logic and Design, Fourth Edition 6-1
Chapter 6
Using Methods
At a Glance
• Objectives
• Teaching Tips
• Quick Quizzes
• Additional Projects
• Additional Resources
• Key Terms
An Object-Oriented Approach to Programming Logic and Design, Fourth Edition 6-2
Lecture Notes
Overview
Chapter 6 presents another universal topic in object-oriented programming. Methods are a
fundamental concept and provide the foundation for a program. The chapter begins by
presenting the advantages of modularization and what is involved in modularizing a
program. Next, local and global variables and constants are described. This is followed by a
discussion regarding the creation of various methods using single variable arguments,
multiple variable arguments, array elements as arguments, and arrays themselves as
arguments. The topic of returning a variable to the calling method is also covered. The end
of the chapter discusses various important topics, including overloading methods, avoiding
ambiguous code, and the convenience of predefined methods.
Chapter Objectives
In this chapter, your students will learn about:
• The advantages of modularization
• Modularizing a program
• Declaring local and global variables and constants
• Methods that require parameters
• Methods that return a value
• Passing an array to a method
• Overloading methods
• Using predefined methods
Teaching Tips
Understanding the Advantages of Modularization
1. Introduce the concept of modularization, which is the process of breaking down the
programming process into smaller, more manageable units. Mention that modules are
also called subroutines, procedures, functions, or methods, depending on the
programming language you are working with. This book will use the term method.
2. Discuss the advantages of modularization using the bulleted list on Page 205.
3. Explain that methods provide a way to achieve abstraction, making complex tasks look
simple.
1. Explain that most well-written methods execute a single, finite task, making them easier
to understand than a large program.
1. Discuss another advantage of breaking down a task into methods; it can be more easily
divided among various people. Commercial software applications are developed this
way.
2. Give some real-world examples of reusability, such as reusing plumbing and heating
systems in the construction of a new home.
3. Introduce reliability, which is the feature of programs and methods that assures you
each has been tested and proved to function correctly.
Modularizing a Program
1. Explain that application classes have the option of containing additional methods that
the main() method can use.
3. Explain that when a main() method needs to use another method, it calls the method,
or invokes it, using the method’s name.
4. Describe the flowchart symbol used to represent a method— a rectangle with a bar
across the top. Note that the method name goes inside the rectangle.
5. Introduce the example in Figure 6-1 on Page 209, which produces a customer’s bill
using only a main() method.
An Object-Oriented Approach to Programming Logic and Design, Fourth Edition 6-4
6. Review Figure 6-2 on Page 210 and discuss the modularization that is accomplished by
the addition of the displayAddressInfo() method.
7. Explain some of the advantages of the modularized program shown in Figure 6-2. That
is, the main() method remains short and easy to follow, and the method in the
modularized version is easily reusable.
9. Note that when using the displayAddressInfo() method within its own class, it
is not necessary to use the full name. Using it within another class requires identifying it
by its full name.
10. Explain that there are no fixed rules for how to break down programs into methods; it
requires experience.
11. Define functional cohesion, which describes the extent to which a method’s statements
contribute to the specific task.
Remind students that when you call a method, the action is similar to putting a
Teaching DVD player on pause. You abandon your first action (watching a video), take
Tip care of some other task (for example, making a sandwich), and when the
secondary task is complete, you return to the main task exactly where you left
off.
2. Using Figure 6-3 on Page 213, show an example of a method that contains three named
constant declarations.
3. Explain that variables and constants declared in a method are usable only within that
method. Note that the terms local, in scope, and visible are other ways of referring to
variables that are known only within a method. When the method ends, its variables are
said to go out of scope.
4. Make sure students understand that local variables and constants cannot be used by the
main() method.
An Object-Oriented Approach to Programming Logic and Design, Fourth Edition 6-5
5. Mention that in the last example, the variables in the main() method are local to main
and not visible within the displayAddressInfo() method.
6. Define portable methods, which are self-contained units that are easily transportable to
other applications. Note that the definitions for variables and constants should come
with the method.
7. Explain global variables and constants, which are known to the entire class. They are
declared inside a class but outside any methods.
8. Describe how methods share data, noting that data can be passed into and returned out
of methods. Using the bulleted list on page 214, explain three requirements of calling a
method.
2. Give the example of a square() method that you can supply with a parameter that
represents the value to be squared.
5. Show Figure 6-5 on page 217, which illustrates how the billing program might look
when executed in a command-line environment.
An Object-Oriented Approach to Programming Logic and Design, Fourth Edition 6-6
6. Discuss the items that must be included within the method header’s parentheses: the
type of parameter and local name for the parameter.
7. Review that in the program in Figure 6-4, the value of the balance is stored in two
places in memory; main() stores it in the variable balance and passes it to
displayAddressInfo()as an argument. Note that the
displayAddressInfo() method accepts the parameter as amountDue.
2. Mention that most of the time, an argument is passed by value, forcing the method to
contain its own copy of the value.
4. Explain that the name of the variable passed in can be either the same or different as the
parameter in the method header.
5. Go through the logic in the program shown in Figure 6-6 on page 219, as an example of
passing an argument by value. Figure 6-7 shows the execution of the program in Figure
6-6.
8. Explain that some programming languages allow you to pass arguments by reference,
which gives the receiving method the address of the original variable rather than a copy
of its value. The way this is accomplished differs among programming languages but
typically involves placing a special symbol or specified keyword within the parentheses
in the method header.
An Object-Oriented Approach to Programming Logic and Design, Fourth Edition 6-7
Explain that the declaration of the argument in the method header is similar to
Teaching the declaration of a data item variable in the class. It is different, however, in that
Tip the argument can only be used within the body of the method, and its value must
be supplied when the method is called. That is, a value is assigned to an
argument when the method is invoked, and the argument does not retain its value
once the method is finished executing.
1. Explain that a method can require more than one parameter. Describe how to create and
use a method with multiple parameters using the bulleted list on page 220.
2. Use Figure 6-8 on page 221 to illustrate a call to a computeTax() method using two
values: the amount to be taxed and a percentage by which to tax it. Step through the
program logic, emphasizing variable declarations and use. Use the discussion on pages
221–222 as a guide.
3. Stress that a method invocation with multiple arguments requires that the arguments
passed in match the method’s argument declaration in number, type, and order.
Teaching The arguments sent to a method in a method call are often referred to as actual
Tip parameters. The variables in a method declaration that accept values from the
actual parameters are called the formal parameters.
2. Explain that if a programmer wants to retain a value that exists in a method, the
programmer can send the value back to the calling method. To do this, the method must
have a return type. Explain that a return type could be numeric, character, or string, as
well as other types that exist in the programming language you are using.
3. Define a void method, and explain that a method type is indicated in front of the method
name when the method is defined.
A method with arguments and a non-void return type can be compared to a food
Teaching processor. You provide input (food) to the food processor, press a button to
Tip “execute” it, and the food processor returns something that is probably in a
different form than it was when you put it in! The food processor manipulated
the input and provided something in return.
An Object-Oriented Approach to Programming Logic and Design, Fourth Edition 6-8
4. Use Figure 6-9 on pages 224–225 to illustrate a method returning a value. Step through
the program logic, emphasizing the returning variables’ declarations and use. Use the
discussion on page 225 as a guide.
5. Mention that a method’s declared return type must match the type of the value used in
the return statement; if it does not, the program will not compile.
6. Note that a called method may return a value directly to the calling method without
storing it. Use Figure 6-10 on page 226 to illustrate an example of a main() method
that uses a method’s returned value in an arithmetic statement directly without storing
it.
8. Note the problem that exists in Figure 6-11. The problem involves a violation of
structured logic, requiring each structure to contain one entry point and one exit point
by leaving the decision structure before it is complete.
9. Use Figure 6-12 on page 228 to illustrate a solution to the problem in Figure 6-11. Step
through the program logic, noting that the largest value is stored in a variable. Then,
when the decision structure is complete, the stored value is returned.
2. Describe that instead of passing an array element, you can pass an entire array as an
argument. Explain that arrays are passed by reference and that changes made to the
array in the called method are permanently reflected in the array.
3. Review the program in Figure 6-13 on pages 230–231, which creates an array of four
numeric values and passes the array to methods three times. Use the bulleted list on
page 229 to talk through these three calls.
Quick Quiz 1
1. ____ are the data items sent to methods.
Answer: Arguments
3. True or False: A method’s arguments must be declared with the argument type before
the argument name.
Answer: True
5. When passing values to arguments in a method, the values must match the argument
declarations in which three ways?
Answer: number, type, and order
Overloading Methods
1. Describe what is meant by overloading.
3. Use Figure 6-15 on page 233 to show a method that prints a message and the amount
due on a customer bill. The method receives a numeric parameter that represents the
customer’s balance and prints two lines of output. A second version that receives two
parameters is also shown in the figure. Step through the two programs to illustrate
overloading.
4. Use Figure 6-16 on page 234 to show two more versions of the printBill method:
one that uses two arguments and one that uses three. Step through the logic in each.
An Object-Oriented Approach to Programming Logic and Design, Fourth Edition 6-10
5. Note that overloading a method is never required, but the advantage to the method’s
clients is that they need only keep track of a single method name rather than multiple
names.
1. Explain an ambiguous method and how overloading a method creates a risk of creating
ambiguous methods.
2. Use Figure 6-17 on pages 236–237 to illustrate a program that contains an ambiguous
method call. Step through the program, explaining that each of the two versions of
printBill() in Figure 6-17 is a valid method on its own. However, when the two
versions exist in the same class, the compiler cannot determine which version to
execute.
3. Stress that the compiler determines which version of a method to call based on
argument data types only, not their identifiers.
2. Explain that using predefined methods saves time and effort. Give the example of
methods that print “Hello” on the screen, using the statements on page 238.
4. Describe the three things a programmer needs to know when using predefined methods,
using the bulleted list on page 239.
Teaching Note that a programmer does not need to know how a built-in method is
Tip implemented—that is, how the instruction statements are written within it. Built-
in methods are usually black boxes. You can use built-in methods without
worrying about their low-level implementation details.
Quick Quiz 2
1. True or False: A programmer can pass a single array element to a method in exactly the
same manner as passing a variable or constant.
Answer: True
2. True or False: An entire array is too big and impossible to pass as a variable to a called
method.
Answer: False
3. When an item is ____ to a method, the method receives the actual memory address
item.
Answer: passed by reference
2. Discuss the concept of reusability and how to design for reusability. Have the students
consider the actions they take to complete a homework assignment in a specific course
and determine if any similar actions are taken to complete homework in another course.
For example, “open book” and “read book” are two activities that are probably common
to many homework assignments. Discuss how these activities could be represented as
generic, reusable methods with arguments.
Additional Projects
1. Have the student select an object-oriented language of his or her choice and research the
various specifics of variable definition and use within that language. Have the student
write a short report and present it to the class.
2. Have the student write the pseudocode and a flowchart diagramming the process of
calculating student grades at the end of the semester. Ask the student to focus on
incorporating a called method (or two) and consider the variable declarations for doing
this.
Additional Resources
1. Methods (C# Programming Guide):
http://msdn.microsoft.com/en-us/library/ms173114.aspx
Key Terms
Abstraction is the process of paying attention to important properties while ignoring
nonessential details.
Actual parameters are the arguments in a method call.
Ambiguous methods are overloaded methods for which the compiler cannot determine
which version to use.
Arguments are the data items sent to methods.
Calling or invoking a method causes it to execute.
Encapsulation is the feature of methods that provides for their instructions and data to
be contained in the method.
Formal parameters are the variables in the method declaration that accept the values
from the actual parameters.
Functional cohesion describes the extent to which a method’s statements contribute to
the same task.
Functional decomposition is the act of reducing a large program into more manageable
methods.
Global variables and constants are known to an entire class.
Implementation hiding is a principle of object-oriented programming that describes
the encapsulation of method details within a class.
In scope describes items that are visible in a method.
The interface to a method includes the method’s return type, name, and arguments.
The interface is the part that a client sees and uses.
Local describes data items that are usable only within the method in which they are
declared.
A method body contains all of the statements in a method.
The method declaration consists of the return type and signature of the method.
A method header is the first line of a method. It is the entry point to a method, and it
provides an identifier, parameter list, and frequently, other information.
A method’s client is a program or other method that uses the method.
A method’s return statement marks the end of the method and identifies the point at
which control returns to the calling method.
A method’s type is its return type.
Modularization is the process of converting a large program into a set of shorter
methods.
Modules are small program units that are combined to make programs. Programmers
also refer to modules as subroutines, procedures, functions, or methods.
Out of scope describes data items that are no longer visible to a method.
When you overload a method, you write multiple methods with a shared name but
different parameter lists.
Overloading involves supplying diverse meanings for a single identifier.
A parameter list is the list of parameters in a method header.
Parameters are the data items received by methods.
Passed by reference describes parameters received by a method as memory addresses.
Passed by value describes parameters received by a method as a copy.
An Object-Oriented Approach to Programming Logic and Design, Fourth Edition 6-14
21.
— Mitä te tahdotte?
— Hyvä, hyvä! lupasi herra Kummitus, mutta Leila selitti että Keiju
suolakalalla tarkoitti ainoastaan lettuja.
Ha-ha-ha-ha-ha-haa!
Rä-kä-kä-kä-kää!
— Voi rakas Pulski, huusi äiti perään, elä vaan putoa! Mutta Pulski
keikutti pyrstöään ja nauraa räkätti vastaan.
— Eihän tästä nyt tule mitään! huokasi äiti ja katui että oli
tuskastunut lapsiin eilisiltana ja saanut Kummituksen kujeilemaan.
Armeijani tänne! Mars! Ein zwei drei! Silloin tulivat metsästä kaikki
hiiret ja myyrät ja kärpät ja oravat ja alkoivat siivota pirttiä
perunoista. Leila ja Heila ja Keiju läksivät pakoon, kun sellainen
elukkajoukko ryntäsi pirttiin. Mutta kun herra Kummitus näki että
talossa oli aikamoinen sekamelska, niin jo juoksi hänkin hätään ja
kohennusraudalla hätisteli koko Saksan keisarin sota-armeijan ulos
pirtistä, viskasi siirappitynnyrin alas mäestä, pisteli letut ja pullat ja
suolakalat omiin taskuihinsa, kiipesi sitten uuninpiipun päälle katolle,
paukutteli kämmeniään yhteen ja alkoi loitsia:
Updated editions will replace the previous one—the old editions will
be renamed.
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the
terms of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or
expense to the user, provide a copy, a means of exporting a copy, or
a means of obtaining a copy upon request, of the work in its original
“Plain Vanilla ASCII” or other form. Any alternate format must
include the full Project Gutenberg™ License as specified in
paragraph 1.E.1.
• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
1.F.4. Except for the limited right of replacement or refund set forth
in paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.
Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.
testbankdeal.com