Java Programming 8th Edition Joyce Farrell Solutions Manual 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Java Programming, Eighth Edition 5-1

Solution Manual for Java Programming 8th Edition


Farrell 1285856910 9781285856919
Full download link at:
Solution manual: https://testbankpack.com/p/solution-manual-for-java-
programming-8th-edition-farrell-1285856910-9781285856919/
Test bank: https://testbankpack.com/p/test-bank-for-java-programming-8th-
edition-farrell-1285856910-9781285856919/

Chapter 5
Making Decisions
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, Teaching
Tips, Class Discussion Topics, and Additional Projects and Resources. 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

 Objectives

 Teaching Tips

 Quick Quizzes

 Class Discussion Topics

 Additional Projects

 Additional Resources
© 2016 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.
Java Programming, Eighth Edition 5-2

 Key Terms

© 2016 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.
Java Programming, Eighth Edition 5-3

Lecture Notes

Overview
Chapter 5 introduces decision structures using the if, if…else, and switch
statements. Students will learn to execute program statements based on the result of a
Boolean expression. They will also learn to use the logical operators AND, OR, and
NOT, as well as the conditional operator. This chapter presents a number of tips on
avoiding common programming errors when making decisions.

Objectives
• Plan decision-making logic
• Make decisions with the if and if…else statements
• Use multiple statements in if and if…else clauses
• Nest if and if…else statements
• Use AND and OR operators
• Make accurate and efficient decisions
• Use the switch statement
• Use the conditional and NOT operators
• Assess operator precedence
• Add decisions and constructors to instance methods

Teaching Tips
Planning Decision-Making Logic
1. Define pseudocode and flowcharts. Suggest uses for each tool.

2. Explain the difference between a sequence structure and a decision structure. Note
that Figure 5-1 illustrates a sequence structure, while Figure 5-2 shows a decision
structure.

3. Remind students that a Boolean value can have one of two values: true or false.
Every computer decision results in a Boolean value.

Teaching Note that a decision in a program can only have one of two possible outcomes:
Tip true or false.

© 2016 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.
Java Programming, Eighth Edition 5-4

Two Truths and a Lie


1. Discuss the two truths and a lie on page 248.

The if and if…else Statements

1. Introduce the if statement. Figure 5-3 shows the decision structure represented by a
single if statement.

2. Remind students about the relational operators ( ==, <, >, <=, >=, and != ) from Chapter
2.

Teaching Discuss how if statements make video games fun. Draw your discussion from
Tip popular games you know.

Pitfall: Misplacing a Semicolon in an if Statement

1. Review Figure 5-4, which shows a common mistake of ending the if statement with a
semicolon. Note that this will cause the empty statement containing only the
semicolon to be executed regardless of whether the condition is true or false.
During class, write code that illustrates this pitfall.

Pitfall: Using the Assignment Operator Instead of the Equivalency Operator

1. Discuss the problems with using a single equal sign instead of a double equal sign when
trying to make an equality comparison. Write code that shows this pitfall in class.

Pitfall: Attempting to Compare Objects Using the Relational Operators

1. Remind students that the relational operators can only be used to compare primitive
values and variables, not objects. During class, write code that shows this pitfall.

Teaching To emphasize the last pitfall, it can be helpful to teach students about comparing
Tip strings here rather than waiting until Chapter 7.

The if…else Statement

1. Introduce the if…else statement. Note that while an if statement is called a single-
alternative if, an if…else structure is called a dual-alternative if. The flowchart
for this logic is shown in Figure 5-5.

© 2016 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.
Java Programming, Eighth Edition 5-5

2. During class, write code that uses an if…else statement. A good example is a
pass/fail scenario.

Two Truths and a Lie


1. Discuss the two truths and a lie on page 252.

You Do It
1. Students should follow the steps in the book on pages 253–254 to create a Java
application that uses an if…else statement.

Using Multiple Statements in if and if…else Clauses

1. Using Figure 5-7, inform students that an if statement can be followed by more than
one statement that will execute when the condition is true as long as the statements are
enclosed in a block.

2. Point out that a programmer must enclose the statements within curly braces.
Otherwise, only the first statement will be executed when the condition is true. Execute
code similar to Figure 5-8 during class to demonstrate this point.

3. Review Figure 5-9, which illustrates a fully functional if…else statement. Execute
similar code in your class environment.

4. Review scope issues by examining the code on page 258. Explain why the sum variable
is out of scope and how to fix the problem.

Many programmers always use a block enclosed in curly braces following an if


Teaching statement even if there is only one program statement that will execute. That
Tip way, if they later modify the if statement to include multiple statements, they
do not have to worry about forgetting to use a block.

Two Truths and a Lie


1. Discuss the two truths and a lie on page 258.

You Do It
1. Students should follow the steps in the book on pages 259–260 to create a Java
application that uses multiple statements in if and else clauses.

© 2016 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.
Java Programming, Eighth Edition 5-6

Quick Quiz 1
1. Programmers often use ____, a tool that helps them plan a program’s logic by writing
plain English statements.
Answer: pseudocode

2. A(n) ____ structure involves choosing between alternative courses of action based on
some value within a program.
Answer: decision

3. True or False: In a Java if statement, the Boolean expression, such as


(someVariable == 10), does not have to appear within parentheses.
Answer: False

4. True or False: Just as you can block statements to depend on an if, you can also block
statements to depend on an else.
Answer: True

5. True or False: The following code illustrates a valid condition to check whether your
age is at least 21.
if(age > 21)
Answer: False

Nesting if and if…else Statements

1. Using Figure 5-12, explain the concept of nested if statements.

2. Use Figure 5-13 to illustrate the importance of correctly matching the else statements.
Remind students that the first if statement written is the last to match to an else.

3. Using an example your students understand, provide an example of a nested if


statement.

Two Truths and a Lie


1. Discuss the two truths and a lie on page 262.

You Do It

1. Students should follow the steps in the book on page 263 to add a nested if statement
to the existing AssignVolunteer2 project created in the previous You Do It activity.

Using Logical AND and OR Operators


1. Spend a few minutes discussing the need for logical operators.
© 2016 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.
Java Programming, Eighth Edition 5-7

2. Briefly define AND and OR operators, providing a simple example. For instance,
compare and contrast using AND and OR to refine a search for the perfect mate.

Avoid using business examples to illustrate the logical operators. Students may
not understand how a business operates, and they can get bogged down in
Teaching
superfluous details. Instead, draw from their knowledge base to illustrate AND
Tip
and OR. Use sports, video games, dating, or anything else students are likely to
understand.

The AND operator

1. Introduce the logical AND operator ( && ). Explain that this operator is used to
combine two Boolean values. If both values are true, the combined value is also
true; otherwise, it is false. An example is shown in Figure 5-15.

Teaching Students may find it helpful to draw a chart of the outcome of using the logical
Tip AND operator for all different possible values of the Boolean expressions.

2. Using the code in Figure 5-15, explain that the logical AND operator can often be used
in place of nested if statements.

3. Discuss the Don’t do it callout on page 264. Contrast this with the valid example shown
in the code block preceding the callout.

4. Write an example that properly uses the && operator in an if statement.

The OR operator

1. Introduce the logical OR operator ( || ). This operator combines two Boolean values
and evaluates to true when either or both of the values is true.

2. Using the code in Figure 5-16, explain how to replace a nested if statement with a
logical OR.

3. Write an example that properly uses the || operator in an if statement.

Short-Circuit Evaluation

1. Define short-circuit evaluation as it applies to if statements. Point out that this is


meant to improve efficiency in Java programs.

© 2016 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.
Java Programming, Eighth Edition 5-8

2. In an if statement containing an && operator, Java will not evaluate the second part of
the && test when the first part is false.

3. In an if statement containing an || operator, Java will not evaluate the second part of
the || test when the first part is true.

Two Truths and a Lie


1. Discuss the two truths and a lie on page 267.

You Do It
1. Students should follow the steps in the book on pages 267–269 to create a Java
application that demonstrates how short-circuiting works with the && operator.

Making Accurate and Efficient Decisions


1. Define accuracy and efficiency.

Making Accurate Range Checks

1. Define a range check. Using the program examples in Figures 5-19 through 5-21,
explain how to code an accurate range check.

2. During class, code a range check on a topic that will resonate with students, such as
assigning letter grades based on percentages.

Do not use the trailing else to pick up the last condition. Your nested
Teaching
if…else if structures should test all possible conditions, leaving the else
Tip
for an error clause.

Making Efficient Range Checks

1. Describe how Java processes nested ifs. Java will compare each if condition until it
either finds a match or has exhausted all possible conditions.

2. For more efficient if statements, you should place the more commonly used conditions
near the top of the nested if…else if structure.

© 2016 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.
Java Programming, Eighth Edition 5-9

Using && and || Appropriately

1. Using the examples on page 273, point out how logical errors can result from the
improper use of && and ||.

Teaching
Remind students to always carefully evaluate whether to use && or ||.
Tip

Two Truths and a Lie


1. Discuss the two truths and a lie on page 274.

Quick Quiz 2
1. ____ if statements are particularly useful when two conditions must be met before
some action is taken.
Answer: Nested

2. The AND operator is written as ____.


Answer: two ampersands or &&

3. A(n) ____ is a series of if statements that determine whether a value falls within a
specified range.
Answer: range check

4. True or False: Under some circumstances, the following println() statement will
execute.
if (payRate < 5.65 && payRate > 60)
System.out.println("Error in pay rate");
Answer: False

5. What is wrong with the following condition? Correct it.


if (grade > 70 || grade <= 80)
Answer: The condition is true for every number.
if (grade > 70 && grade <= 80)

Using the switch Statement

1. Introduce the switch statement. This type of statement compares an integer, a


char, or a String value to a series of possible values and executes program
statements when a match is made.

2. Switches are appropriate when you have a series of values to match. They do not work
well when matching ranges of values.
© 2016 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.
Java Programming, Eighth Edition 5-10

3. Switches in general are a little more efficient to run than nested if…else if
structures.

4. Discuss the four keywords listed on page 275 that are used in a switch statement.
Note that Figure 5-24 contains the same logic as Figure 5-23 but uses a switch
statement rather than a series of if…else statements.

Teaching Point out that the switch statement cannot be used for non-discrete data such
Tip as float or double. It can be used for int, long, char, and String.

Two Truths and a Lie


1. Discuss the two truths and a lie on page 278.

You Do It
1. Students should follow the steps in the book on pages 279–280 to create a Java
application that uses a switch statement.

Using the Conditional and NOT Operators


1. Explain the use of the conditional operator ( ? ). The syntax for this operator is shown
on page 280.

2. Suggest that students avoid using the conditional operator. It is convenient but leads to
code that is harder to read.

Teaching
The conditional operator is the only ternary operator in the Java language.
Tip

Using the NOT Operator

1. Describe the use of the NOT operator ( ! ) to give the opposite value to a Boolean
expression. An example program using this operator is shown in Figure 5-30.

Now is a good time to discuss DeMorgan’s laws:


Teaching
!(A && B) becomes !A || !B
Tip
!(A || B) becomes !A && !B.

© 2016 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.
Java Programming, Eighth Edition 5-11

Two Truths and a Lie


1. Discuss the two truths and a lie on page 282.

Understanding Operator Precedence


1. Describe the order or precedence that applies to the operators that have been introduced
in this book. They are listed in Table 5-1.

2. Remind students that, just as with mathematical operator precedence, parentheses can
be used to clarify or change the order in which operators are evaluated. Review Figure
5-31 for an example.

3. If possible, write code during class that demonstrates common logical errors that require
parentheses to fix.

Two Truths and a Lie


1. Discuss the two truths and a lie on page 284.

Adding Decisions and Constructors to Instance Methods

1. Discuss how to use if statements in set methods and constructors for data validation.

2. During your lecture, create a class that validates data in the set and constructor methods.
Add a method that utilizes a decision structure to calculate a value.

Teaching Data validation is extremely important. Spend a good portion of time having
Tip students verify data.

You Do It
1. Students should follow the steps in the book on pages 286–288 to create a Java
application that uses a switch statement.

Don’t Do It
1. Review the section, discussing each point in class.

Quick Quiz 3
1. In a switch statement, ____ is followed by one of the possible values for the test
expression and a colon.
© 2016 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.
Java Programming, Eighth Edition 5-12

Answer: case

2. True or False: You are never required to use a switch structure; you can always
achieve the same results with nested if statements.
Answer: True

3. You use the ____, which is written as the exclamation point ( ! ), to negate the result of
any Boolean expression.
Answer: NOT operator

4. Which operator has the highest precedence?


Answer: Logical NOT

5. True or False: if statements cannot be in a class’s set method.


Answer: False

Class Discussion Topics


1. Do you think that decision structures are commonly used in programming? Why or why
not?

2. Under what circumstances might you choose to replace a nested if statement with a
logical AND or OR statement?

3. Provide examples in science, medicine, or any other topic where if statements are
required.

4. Have the class list their favorite video games. Let them determine where decisions are
made in the program while they play the game.

Additional Projects
1. Create a program that prompts the user to enter two values: a movie rating and his or
her age. Using a decision structure, determine whether the user would be allowed to see
the movie in a theater based on the rating and age entered. Finally, display the result of
this decision to the user.

2. In this chapter, you have been introduced to sequence and decision control structures.
There is one other control structure that is commonly used in programming: the
repetition structure. Using the Internet, research this structure and write a one-paragraph
description of it.

3. Create the card game Hi-Low. The winner of a hand is the player with the highest card.
Ties may be ignored.
© 2016 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.
Java Programming, Eighth Edition 5-13

Additional Resources
1. Summary of Java operators:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/opsummary.html

2. Using the switch statement including demos:


http://download.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

3. The Pseudocode Programming Process:


www.coderookie.com/2006/tutorial/the-pseudocode-programming-process/

4. Truth Tables, Logic, and DeMorgan’s Laws:


www.cs.utah.edu/~germain/PPS/Topics/truth_tables_and_logic.html

Key Terms
 Boolean values: true or false values. Every computer decision results in a Boolean
value.
 Conditional operator: requires three expressions separated with a question mark and a
colon, and is used as an abbreviated version of the if…else structure.
 Conditional OR operator: used between Boolean expressions to determine whether
either expression is true. The OR operator is written as two pipes ( || ).
 Decision structure: a logical structure that involves choosing between alternative
courses of action based on some value within a program.
 Dual-alternative if: a decision structure that takes one of two possible courses of
action.
 else clause: the portion of the if…else statement that executes when the condition
is false.
 Empty statement: contains only a semicolon.
 Equivalency operator: written as two equal signs ( == ); compares values and returns
true if they are equal.
 Flowchart: a tool that helps programmers plan a program’s logic by writing the steps in
diagram form as a series of shapes connected by arrows.
 if clause: the portion of the if…else statement that executes when the condition is
true.
 if statement: the simplest statement you can use to make a decision. You use it to
write a single-alternative decision.
 if…else statement: provides the mechanism to perform one action when a Boolean
expression evaluates as true, and to perform a different action when a Boolean
expression evaluates as false.
 Logical AND operator: used between Boolean expressions to determine whether both
are true. The AND operator is written as two ampersands ( && ).
 Logical OR operator: used between Boolean expressions to determine if either is
true. The OR operator is written as two ampersands ( || ).
© 2016 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.
Java Programming, Eighth Edition 5-14

 Nested if statements: statements in which an if structure is contained within another


if structure.
 NOT operator: negates the result of any Boolean expression. Written as an
exclamation point ( ! ).
 Pseudocode: a tool that helps programmers plan a program’s logic by writing plain
English statements.
 Range check: a series of statements that determine within which of a set of ranges a
value falls.
 Sequence structure: a logical structure in which one step follows another
unconditionally.
 Short-circuit evaluation: describes the feature of the AND and OR operators in which
evaluation is performed only as far as necessary to make a final decision.
 Single-alternative if: a decision structure that performs an action, or not, based on
one alternative.
 switch statement: uses up to four keywords to test a single variable against a series
of exact integer or character values. The keywords are switch, case, break, and
default.
 Ternary operator: an operator that needs three operands.

© 2016 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.

You might also like