Solution Manual For Programming With Microsoft Visual Basic 2015 7th Edition

You might also like

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

Solution Manual for Programming with Microsoft Visual Basic 2015, 7th Edition

Solution Manual for Programming with Microsoft


Visual Basic 2015, 7th Edition

To download the complete and accurate content document, go to:


https://testbankbell.com/download/solution-manual-for-programming-with-microsoft-vi
sual-basic-2015-7th-edition/

Visit TestBankBell.com to get complete for all chapters


Programming with Microsoft Visual Basic 2015, Seventh Edition 6-1

Chapter 6
The Repetition Structure
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

• 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.
Programming with Microsoft Visual Basic 2015, Seventh Edition 6-2

Lecture Notes

Overview
Chapter 6 introduces repetition structures. Students learn how to create pretest and posttest
loops, and how to nest loop structures. The use of counter and accumulator variables is also
presented. This chapter contains three lessons:
• Lesson A covers how to code the repetition structure and how to depict it in pseudocode
and flowcharts.
• Lesson B introduces the use of the list box control and how to determine which item in
the list was selected and calculating a periodic payment.
• Lesson C covers the use of priming and update reads, nesting of repetition structures,
refreshing the screen, and delaying program execution.

Lesson A Objectives
After studying Lesson A, students should be able to:
• Differentiate between a looping condition and a loop exit condition
• Explain the difference between a pretest loop and a posttest loop
• Include pretest and posttest loops in pseudocode and in a flowchart
• Write a Do…Loop statement
• Stop an infinite loop
• Utilize counters and accumulators
• Abbreviate assignment statements using the arithmetic assignment operators
• Code a counter-controlled loop using the For…Next statement

Teaching Tips
Previewing the Monthly Payment Application
1. Use Figure 6-1 to review the sample application to be completed in this chapter.

2. Explain the concept of a list box and why it would be preferred over a text box.

Teaching Run the completed program to demonstrate the final result that will be achieved
Tip in this chapter.

Repeating Program Instructions


1. Remind students that the three basic programming structures are sequence, selection,
and repetition.
© 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.
Programming with Microsoft Visual Basic 2015, Seventh Edition 6-3

2. Introduce the concept of the repetition structure (or loop).

3. Use Figure 6-2 to explain the difference between a looping condition and a loop exit
condition.

The Projected Sales Application

1. Use the problem specification in Figure 6-5 and the sample run in Figure 6-5 to
introduce the Projected Sales application.

2. Use Figure 6-7 to introduce the concepts of the pretest loop and posttest loop.

Teaching Explain to students that usage of pretest and posttest loops depends on when the
Tip condition should logically be tested.

The Do...Loop Statement


1. Introduce the concept of the Do...Loop statement, and point out that it can be used to
create either a pretest loop or a posttest loop.

2. Define the term loop body.

3. Describe the two variations of syntax for the Do...Loop statement shown in Figure 6-8,
and discuss the example code.

Teaching Point out the availability of the Exit Do statement to provide an early exit from a
Tip Do...Loop statement.

Teaching
Remind students that conditions must evaluate to either True or False.
Tip

4. Use Figure 6-9 to describe the processing steps for the loop examples.

5. Use Figure 6-10 to describe the flowchart for the loop examples.

Teaching Ask students to describe the differences between the pretest and posttest loops in
Tip these examples.

© 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.
Programming with Microsoft Visual Basic 2015, Seventh Edition 6-4

6. Point out the importance of understanding when the condition is tested and how the
condition expression is evaluated.

Teaching Ensure that students understand that a posttest loop will always be processed at
Tip least once because the condition is tested after the body of the loop has executed.

Coding the Modified Projected Sales Application

1. Use Figures 6-11 through 6-14 to review the steps needed to code the modified
application.

2. Define the terms overflow error, infinite loop and endless loop.

Counters and Accumulators


1. Explain the difference between a counter variable and an accumulator variable.

2. Introduce the concepts of initializing and updating a variable.

3. Explain the difference between incrementing and decrementing.

Students new to programming often have difficulty understanding why a loop is


Teaching needed for accumulating a value. They tend to think in terms of adding all the
Tip values up first and then storing them in a variable. It is helpful to use a small
sample of code and “play computer” to illustrate this process.

4. Use Figures 6-15 and 6-16 to illustrate updating counters and accumulators.

Teaching Explain that when a negative increment value is involved, programmers use the
Tip term “decrementing.”

The Addition Application

1. Use Figure 6-17 to explain the need for a Start Over button and review the problem
specification, pseudocode, and a sample run for the Addition application.

2. Discuss the functions of the Multiline, ReadOnly, and ScrollBars properties of text
boxes.

3. Remind students of the need to use TryParse to convert the input to a number.
© 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.
Programming with Microsoft Visual Basic 2015, Seventh Edition 6-5

4. Use Figure 6-18 to walk through the code in the loop of the Addition application.

Arithmetic Assignment Operators

1. Use Figure 6-21 to review the syntax and examples of the arithmetic assignment
operators.

The For...Next Statement


1. Introduce the concept of the For...Next statement.

2. Define the term counter-controlled loop.

3. Describe the syntax and examples of the For...Next statement shown in Figure 6-22.

4. Point out that you can declare the counter variable within the For...Next statement itself.

Teaching
You may wish to review the concept of block scope at this time.
Tip

5. Describe the effects of using a positive value and a negative value for the stepvalue
clause.

6. Use Figure 6-23 to walk through the processing steps for the example in the textbook.

Teaching When introducing loop structures, it is a good idea to use a simple example and
Tip “play computer,” showing the actions that occur within each iteration of the loop.

A Different Version of the Projected Sales Application

1. Use Figure 6-24 to review the problem specification for the Projected Sales application.

2. Use Figure 6-25 to describe the code for the btnCalc control’s Click event procedure in
the Monthly Payment Calculator application.

3. Describe the output from this program, shown in Figure 6-26.

Teaching Remind students that the computer’s ability to process the same instructions
Tip repetitively is one of its most important features.

© 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.
Programming with Microsoft Visual Basic 2015, Seventh Edition 6-6

Comparing the For…Next and Do…Loop Statements

1. Using Figure 6-27, point out the similarities and differences between the For…Next and
Do…Loop statements.

Lesson A Summary
1. Summarize the repetition structure (loop).

2. Review the Do...Loop statement and the concept of a posttest loop.

3. Summarize how to depict a Do...Loop statement in a flowchart.

4. Review the use of counter variables.

5. Review the use of accumulator variables.

6. Review the arithmetic assignment operators.

7. Review the For...Next loop and the concept of a pretest loop.

8. Summarize how to depict a For...Next loop in a flowchart.

Quick Quiz 1
1. A For...Next statement creates a(n) ____________________ loop.
Answer: pretest

2. True or False: You can use a Do...Loop statement to create a posttest loop.
Answer: True

3. True or False: A diamond shape is used to flowchart a For...Next loop.


Answer: False

4. A(n) ____________________ variable is used to accumulate a total.


Answer: accumulator

Lesson B Objectives
After studying Lesson B, students should be able to:
• Include a list box on a form
• Add items to a list box
• Clear the items from a list box
• Sort the items in a list box
© 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.
Programming with Microsoft Visual Basic 2015, Seventh Edition 6-7

• Select a list box item from code


• Determine the selected item in a list box
• Calculate a periodic payment using the Financial.Pmt method

Teaching Tips
Creating the Monthly Payment Application
1. Review the TOE chart for the application shown in Figure 6-33.

Teaching Point out that this application has been partially coded. Students will complete
Tip only a portion of the code.

Including a List Box in an Interface


1. Introduce the ListBox tool and the list box control.

Teaching
Compare and contrast the list box control to the combo box control.
Tip

2. Describe the effects of the SelectionMode property.

Teaching
You can demonstrate multi-selection in Windows Explorer.
Tip

3. Use Figure 6-34 to describe the correct location and size of the ListBox.

4. Use Figure 6-35 to describe the correct sequence for the TabIndex property.

Adding Items to a List Box

1. Introduce the concepts of a collection and the Items collection of a list box.

2. Introduce the concept of an index.

Teaching
Stress the concept of zero-relative counting used with indexes.
Tip

© 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.
Programming with Microsoft Visual Basic 2015, Seventh Edition 6-8

3. Use Figure 6-36 to introduce the Add method of the Items collection, and describe its
syntax.

Clearing Items from a List Box

1. Use Figure 6-37 to introduce the Clear method and describe its syntax.

The Sorted Property

1. Introduce the Sorted property of the list box and explain its effects using Figure 6-38
as an example.

2. Point out that a list box will be sorted in dictionary order, not numerical order.

Numeric values can be forced to sort in numerical order by ensuring that all
Teaching
values have the same number of digits. Use left padding with zeros to enforce
Tip
this.

3. Review the guidelines in the GUI Design Tip box.

Coding the Monthly Payment Application


1. Use Figure 6-39 to describe the process of adding an item to a list box control.

Teaching Remind students that IntelliSense will assist in the creation of correct code for
Tip the Add method of the Items collection.

2. Use Figure 6-40 to describe the results of running this application.

The SelectedItem and SelectedIndex Properties

1. Point out that like the combo box, the list box has the SelectedItem property and
SelectedIndex property that provide the same type of information.

2. Review the examples for using the SelectedItem and SelectedIndex properties in Figure
6-41.

3. Introduce the concept of the default list box item, and point out that the default list box
item can be selected using code as shown in Figure 6-42.

© 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.
Programming with Microsoft Visual Basic 2015, Seventh Edition 6-9

Teaching
Setting a default item is a Windows standard that users expect in an application.
Tip
4. Describe the results of running this program shown in Figure 6-43.

5. Review the guidelines for the default list box item in the GUI Design Tip box.

The SelectedValueChanged and SelectedIndexChanged Events

1. Introduce the concepts of the SelectedValueChanged event and


SelectedIndexChanged event of the list box control.

Coding the Calculate Button’s Click Event Procedure

1. Review the pseudocode for the btnCalc_Click procedure shown in Figure 6-44.

The Financial.Pmt Method


1. Use Figure 6-46 to describe some of the methods in the Financial class.

2. Introduce the Financial.Pmt method to calculate a periodic payment.


3.
4. Review the syntax and examples of the Financial.Pmt method in Figure 6-47.

5. Describe the results of running this application shown in Figure 6-49.

6. Review the completed application’s code shown in Figure 6-50.

Lesson B Summary
1. Review how to add a list box control to a form.

2. Review the steps involved in allowing a user to multi-select items in a list box.

3. Summarize the process of adding items to a list box.

4. Summarize the use of the Sorted property for determining the order in which items will
appear in a list box.

5. Review the use of the SelectedItem property or SelectedIndex property to obtain the
value of the currently selected item.

6. Review the use of the SelectedValueChanged or SelectedIndexChanged event


procedures to perform tasks when an item is selected in a list box.

7. Review the Financial.Pmt method to calculate a periodic payment on either a loan or an


investment.
© 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.
Programming with Microsoft Visual Basic 2015, Seventh Edition 6-10

Quick Quiz 2
1. Which property of a list box will provide the position of the selected item in the list?
Answer: SelectedIndex

2. When using a list box control, the event named ____________________ will occur
when the user makes a selection.
Answer: SelectedValueChanged
SelectedIndexChanged

3. True or False: You cannot sort the items in a list box.


Answer: False

4. True or False: The list box only supports allowing a user to make a single choice.
Answer: False

Lesson C Objectives
After studying Lesson C, students should be able to:
• Explain the purpose of the priming and update reads
• Nest repetition structures
• Refresh the screen
• Delay program execution

Teaching Tips
The Electric Bill Application
1. Use Figure 6-55 to introduce the problem specification, pseudocode, and sample run for
the Electric Bill application.

2. Explain the need for a priming read and the difference between a priming read and an
update read.

3. Review the code in the btnCalc_Click procedure in Figures 6-56 and 6-57.

4. Review the completed code for the btnCalc_Click procedure in Figure 6-58 and the
sample run in Figure 6-59.

Nested Repetition Structures


1. Point out that like other statements, repetition statements can be nested. Use Figure 6-60
as an example of how loops are nested for a clock application.

© 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.
Programming with Microsoft Visual Basic 2015, Seventh Edition 6-11

2. Introduce the concepts of inner and outer loops.

3. Review the interface shown in Figure 6-61 for the Clock application.

The Refresh and Sleep Methods


1. Introduce the Refresh method for redrawing the interface.

2. Introduce the Sleep method for pausing the program’s execution. Explain the
millisecond time unit.

3. Review the code for the Refresh and Sleep methods shown in Figure 6-62.

Students may have a tough time understanding the logic behind the workings of a
Teaching clock. The application displays minutes from 0 to 2 and seconds from 0 to 5.
Tip This will add to the confusion. Even though they will code for the simple
version, you should explain the concept using 60 seconds and 60 minutes.

Trixie at the Diner


1. Using Figure 6-63, introduce the problem specification and the solution that requires a
loop.

2. Explain the need for the modifications illustrated in Figure 6-64.

The Savings Account Application

1. Describe the nested repetition structures in the code for the procedure shown in Figure
6-65.

2. Describe the role played by the inner loop and the role played by the outer loop as
shown in Figure 6-66.

A Caution about Real Numbers

1. Explain what real numbers are.

2. Show the sample output in Figures 6-67 and 6-68 as well as the modifications to the
nested loop shown in Figure 6-69.

Lesson C Summary
1. Review the process of nesting repetition structures.

2. Review the use of the Refresh method.


© 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.
Programming with Microsoft Visual Basic 2015, Seventh Edition 6-12

3. Review the use of the Sleep method.

Quick Quiz 3
1. True or False: Repetition structures cannot be nested within other types of structures
such as If...Then...Else statements.
Answer: False

2. The (inner/outer) ____________________ loop will be processed more times than the
(inner/outer) ____________________ loop.
Answer: inner, outer

3. To pause program execution, use the ____________________ method.


Answer: Sleep

Class Discussion Topics


1. What are the advantages and disadvantages of the list box and the combo box controls?
When should a list box be used instead of a combo box? When is the combo box
preferable to a list box? Describe some guidelines for when to use each.

2. Consider the SelectionModeProperty of the list box control. Although it can allow the
user to select multiple items at the same time, does this change how the program’s code
must be written?

3. Discuss why an index starts with a 0 instead of a 1.

Additional Projects
1. Have students create an interface that allows a user to select a state code from a list box
and use the select case statement to print out the state name. You can limit the number
of states in the list box to make the assignment coding shorter.

2. Ask students to experiment with the sorted property of the list box control by adding
only numeric values to the list. How does dictionary sort affect the order when the
Sorted property is set to True?

Additional Resources
1. An Introduction to Loops:
www.homeandlearn.co.uk/NET/nets3p1.html
© 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.
Programming with Microsoft Visual Basic 2015, Seventh Edition 6-13

2. “How Do I” Videos for Visual Basic:


http://msdn.microsoft.com/en-us/vbasic/bb466226.aspx

Key Terms
➢ Accumulator—a numeric variable used for accumulating (adding together) something
➢ Add method—the Items collection’s method used to add items to a list box
➢ Arithmetic assignment operators—composed of an arithmetic operator followed by
the assignment operator; used to abbreviate an assignment statement that has the
following format, in which variableName is the name of the same variable:
variableName = variableName arithmeticOperator value
➢ Collection—a group of individual objects treated as one unit
➢ Counter—a numeric variable used for counting something
➢ Counter-controlled loop—a loop whose processing is controlled by a counter; the loop
body will be processed a precise number of times
➢ Decrementing—adding increments of a negative number
➢ Default list box item—the item automatically selected in a list box when the interface
appears on the screen
➢ Do...Loop statement—a Visual Basic statement that can be used to code both pretest
loops and posttest loops
➢ Endless loop—a loop whose instructions are processed indefinitely; also called an
infinite loop
➢ Financial.Pmt method—used to calculate the periodic payment on either a loan or an
investment
➢ For...Next statement—a Visual Basic statement that is used to code a specific type of
pretest loop, called a counter-controlled loop
➢ Incrementing—another name for updating
➢ Infinite loop—another name for an endless loop
➢ Initializing—the process of assigning a beginning value to a memory location, such as
a counter or an accumulator variable
➢ Items collection—the collection composed of the items in a list box
➢ List box—a control used to display a list of choices from which the user can select zero
choices, one choice, or multiple choices
➢ Loop—another name for the repetition structure
➢ Loop body—the instructions within a loop
➢ Loop exit condition—the requirement that must be met for the computer to stop
processing the loop body instructions
➢ Looping condition—the requirement that must be met for the computer to continue
processing the loop body instructions
➢ Multiline property—determines whether a text box can accept and display only one
line of text or multiple lines of text
➢ Posttest loop—a loop whose condition is evaluated after the instructions in its loop
body are processed

© 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.
Solution Manual for Programming with Microsoft Visual Basic 2015, 7th Edition

Programming with Microsoft Visual Basic 2015, Seventh Edition 6-14

➢ Pretest loop—a loop whose condition is evaluated before the instructions in its loop
body are processed
➢ Priming read—the input instruction that appears above the loop that it controls; used to
get the first input item from the user
➢ ReadOnly property—controls whether the user is allowed to change the contents of a
text box during run time
➢ Real numbers—numbers with a decimal place
➢ Refresh method—can be used to refresh (redraw) a form
➢ Repetition structure—the control structure used to repeatedly process one or more
program instructions; also called a loop
➢ ScrollBars property—a property of a text box; specifies whether the text box has
scroll bars
➢ SelectedIndex property—stores the index of the item selected in a list box
➢ SelectedIndexChanged event—occurs when an item is selected in a list box
➢ SelectedItem property—stores the value of the item selected in a list box
➢ SelectedValueChanged event—occurs when an item is selected in a list box
➢ SelectionMode property—determines the number of items that can be selected in a list
box
➢ Sleep method—can be used to delay program execution
➢ Sorted property—specifies whether the list box items should appear in the order they
are entered or in sorted order
➢ Update read—the input instruction that appears within a loop and is associated with
the priming read
➢ Updating—the process of adding a number to the value stored in a counter or
accumulator variable; also called incrementing

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

Visit TestBankBell.com to get complete for all chapters

You might also like