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

Solution Manual for Programming with Microsoft Visual Basic 2017 8th by Zak

Solution Manual for Programming with Microsoft


Visual Basic 2017 8th by Zak

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


https://testbankbell.com/download/solution-manual-for-programming-with-microsoft-vi
sual-basic-2017-8th-by-zak/

Visit TestBankBell.com to get complete for all chapters


Programming with Microsoft Visual Basic 2017, Eighth Edition 7-1

Chapter 7
String Manipulation
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


1. Overview

2. Focus Objectives

3. Teaching Tips

4. Mini Quizzes

5. Apply Objectives

6. Teaching Tips

7. Mini Quizzes

8. Class Discussion Topics

9. Additional Projects

10. Additional Resources

11. Key Terms

Lecture Notes
© 2018 Cengage. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Programming with Microsoft Visual Basic 2017, Eighth Edition 7-2

Overview
In many cases, an application’s code will need to manipulate (process) string data in some
way. For example, it may need to look at the first character in an inventory part number to
determine the part’s location in the warehouse. Or, it may need to search an address to
determine the street name. Or, it may need to verify that the input entered by the user is in the
expected format. In this chapter’s Focus on the Concepts lesson, you will learn several ways of
manipulating strings in Visual Basic.

In the Apply the Concepts lesson, you will learn how to code applications that create check
digits and passwords. You will also learn how to generate random numbers. You will use the
concepts from the Focus lesson to code the applications in the Apply lesson.

Focus on the Concepts Objectives


F-1 Length property
F-2 Insert method
F-3 PadLeft and PadRight methods
F-4 Contains and IndexOf methods
F-5 Substring method
F-6 Character array
F-7 Remove method
F-8 Trim, TrimStart, and TrimEnd methods
F-9 Replace method
F-10 Like operator

Teaching Tips
F-1 Length Property
1. Introduce the Length property of the string object.

2. Use Figure 7-1 to describe the syntax and examples for using the Length property.

3. Go over the requirements of the length property in the Product ID application.

Teaching Run the completed program to demonstrate the final result that will be achieved
Tip in this chapter. Students will recognize this game as being similar to hangman.

4. Show Figure 7-3 as an example of the interface.

F-2 Insert Method


© 2018 Cengage. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Programming with Microsoft Visual Basic 2017, Eighth Edition 7-3

• Introduce the Insert method for inserting characters in a string.

• Use Figure 7-4 to describe the syntax and examples of the Insert method.

Teaching It is not uncommon to have to use several string methods to manipulate user
Tip input.

• Discuss the Insert method’s startIndex argument.

F-3 PadLeft and PadRight Methods


1. Introduce the PadLeft method and PadRight method, which are used to align the
characters in a string.

2. Review the syntax and examples of PadLeft and PadRight in Figure 7-5.

3. Go over the use of Insert and PadLeft methods in the coding of the Net Pay application.

4. Describe the purpose of the Net Pay application, which allows the user to enter the
amount of an employee’s net pay. It then displays the net pay with a leading dollar sign,
asterisks, and two decimal places.

5. Discuss the code in Figure 7-6.

6. Describe the output from the application, which is shown in Figure 7-7.

Mini-Quiz 7-1
1. Write a statement that assigns the number of characters in the strZip variable to the
intNum variable.
Answer: intNum = strZip.Length
2. Write a statement that uses the Insert method to change the contents of the strState
variable from “Ky” to “Kentucky”.
Answer: strState = strState.Insert(1, "entuck")
3. Write a statement that uses the PadRight method to change the contents of the strBonus
variable from “100” to “100$$$”.
Answer: strBonus = strBonus.PadRight(6, "$"c)

F-4 Contains and IndexOf Methods

© 2018 Cengage. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Programming with Microsoft Visual Basic 2017, Eighth Edition 7-4

1. Introduce the Contains method for determining if a string contains a specific sequence
of characters.

2. Introduce the IndexOf method for determining the position of a specific sequence of
characters within a string.

3. Introduce the concept of an index for strings. Stress the fact that the first character in a
string has an index of 0.

4. Use Figure 7-8 to describe the syntax and examples of the Contains and IndexOf
methods.

Spend some time on the concept of zero-relative numbering because this issue
Teaching
will come up again when discussing arrays. This is often a stumbling block for
Tip
students who are new to programming.

5. Describe the purpose of the City and State application.

6. Discuss the code shown in Figure 7-9, focusing on the use of the IndexOf method.

7. Show Figure 7-10 to examine the interface showing the comma’s index.

Mini-Quiz 7-2
1. Write a statement that uses the Contains method to determine whether the strAddress
variable contains the string “Elm St.” (in uppercase, lowercase, or a combination of uppercase
and lowercase). Assign the return value to the blnIsContained variable.
Answer: blnIsContained = strAddress.ToUpper.Contains("ELM ST.")
2. Write a statement that uses the IndexOf method to determine whether the strAddress
variable contains the string “Elm St.” (in uppercase, lowercase, or a combination of uppercase
and lowercase). Assign the return value to the intIndex variable.
Answer: intIndex = strAddress.ToUpper.IndexOf("ELM ST.")
3. What does the IndexOf method return when the string does not contain the subString?
Answer: -1
4. What does the Contains method return when the string does not contain the subString?
Answer: False

Teaching
Remind students that the character index for a string is zero-relative.
Tip

F-5 Substring Method

© 2018 Cengage. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Programming with Microsoft Visual Basic 2017, Eighth Edition 7-5

1. Introduce the Substring method for accessing any number of characters contained in a
string.

2. Use Figure 7-11 to describe the syntax and examples of the Substring method.

3. Introduce the Rearrange Name application and its purpose, which is to provide a text
box for entering a person’s first name followed by a space and the person’s last name.
The application rearranges the name so that the last name comes first, followed by a
comma, a space, and the first name.

4. Discuss the code shown in Figure 7-12, focusing on the use of the Substring method.

5. Describe the output from the application, which is shown in Figure 7-13.

F-6 Character Array


1. Introduce the term Character array and note that it’s often called array of characters.

2. Look at Figure 7-14 and describe the unique index that represents the character position
in a string.

3. Discuss how strName(0) refers to the first character in the string stored in the
strName variable.

4. Discuss that strName(0) is equivalent to using strName.Substring(0, 1).

5. Introduce the First Name application and its purpose, which is to provide a text box for
entering a person’s first name.

6. Discuss the code found in Figure 7-15, pointing out the For...Next loop.

7. Show the final interface and example output in Figure 7-16.

Mini-Quiz 7-3
1 The strItem variable contains the string “XMredBQ”. Write a statement that uses the
Substring method to assign the string “red” to the strColor variable.
Answer: strColor = strItem.Substring(2, 3)
2. The strName variable contains the string “Jane H. Doe”. Write a statement that uses the
Substring method to assign the string “H” to the strInitial variable.
Answer: strInitial = strName.Substring(5, 1)
3. Rewrite the statement from Question 2 using the H character’s index.
Answer: strInitial = strName(5)

F-7 Remove Method


© 2018 Cengage. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Programming with Microsoft Visual Basic 2017, Eighth Edition 7-6

1. Introduce the Remove method for removing characters from anywhere in the string.

Teaching Removing leading and trailing blanks becomes very important when the data will
Tip be stored in a database.

2. Use Figure 7-17 to describe the syntax and examples of the Remove method.

3. Go over that the Remove method’s startIndex argument is the index of the first
character to be removed from the copy of the string.

F-8 Trim, TrimStart, and TrimEnd Methods


1. Remind students of the Trim method for removing spaces from the beginning and end
of a string.

2. Introduce the TrimStart method and TrimEnd method.

3. Figure 7-18 shows the syntax and examples of the Trim, TrimStart, and TrimEnd
methods.

4. Introduce the Tax Calculator application and its purpose, which is to calculate the
amount of sales tax to charge a customer.

5. Go over the code in Figure 7-19, focusing on the TrimEnd method.

6. Use Figure 7-20 as a visual example of the output displayed in the interface.

Mini-Quiz 7-4
1. The strAmount variable contains the string “1,234”. Write a statement that uses the
Remove method to change the variable’s contents to “1234”.
Answer: strAmount = strAmount.Remove(1, 1)
2. The strTotal variable contains the string “***75.67”. Write a statement that uses the
TrimStart method to change the variable’s contents to “75.67”.
Answer: strTotal = strTotal.TrimStart("*"c)
3. If the strDue variable contains the string “$8.50$”, what will the strDue.TrimStart("$"c)
method return?
Answer: “8.50$”
4. If the strDue variable contains the string “$8.50$”, what will the strDue.Trim("$"c)
method return?
Answer: “8.50”

© 2018 Cengage. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Programming with Microsoft Visual Basic 2017, Eighth Edition 7-7

F-9 Replace Method


1. Introduce the Replace method.

2. Look at Figure 7-21 to show the syntax and examples of the Replace method.

F-10 Like Operator


1. Introduce the Like operator and the use of the comparison operators for comparing
strings.

2. Use Figure 7-22 to describe the syntax and examples of the Like operator.

3. Introduce the Inventory application and its purpose, which is to verify that the inventory
number entered by the user consists of three letters followed by two numbers.

4. Go over the code in Figure 7-23, looking at the condition to add to the If clause.

5. Use Figure 7-24 as an example of the output in the interface.

6. Go over testing the application.

Mini-Quiz 7-5
1. The strTotal variable contains the string “***75.67”. Write a statement that uses the
Replace method to change the variable’s contents to “75.67”
Answer: strTotal = strTotal.Replace("*","")
2. Write an If clause that determines whether the strInput variable contains two numbers
followed by an uppercase letter.
Answer: If strInput Like "##[A-Z]" Then
3. Write an If clause that determines whether the strInput variable contains a dollar sign
followed by a number and a letter (in uppercase or lowercase).
Answer: If strInput Like "[$]#[a-zA-Z]" Then

Apply the Concepts Objectives


A-1 Code the Check Digit application
A-2 Code the Password application
A-3 Generate random integers
A-4 Code the Guess a Letter application
A-5 Code the Guess the Word Game application

© 2018 Cengage. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Programming with Microsoft Visual Basic 2017, Eighth Edition 7-8

Teaching Tips
A-1 Code the Check Digit Application
1. Introduce the idea of the check digit.

2. Introduce the Check Digit Application and its purpose, which is used to validate the
number’s authenticity.

3. Show Figure 7-25 for a list of algorithms used to create check digits.

4. Figure 7-26 shows the interface for the Check Digit application.

5. Go over the code in Figure 7-27, focus on the txtIsbn.Text.Length.

6. Look at the code in Figures 7-28 through 7-30.

7. Figure 7-31 shows the completed interface with the output filled in.

A-2 Code the Password Application


1. Figure 7-33 demonstrates the interface for the Password application.

2. The purpose of the application is to create a password based on the first letter of a string
of words.

3. Go over the code in Figures 7-34 through 7-36.

4. Show the completed interface with output in Figure 7-37.

5. Go over the code in Figure 7-38 for the completed btnCreate_Click procedure.

A-3 Generate Random Integers


1. Discuss the idea that random numbers are used in many computer game programs.

2. Introduce the idea of a pseudo-random number generator.

3. Figure 7-39 provides the syntax and examples of generating random integers.

4. Introduce the Random object in Visual Basic.

5. Introduce the Random.Next method.

© 2018 Cengage. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Programming with Microsoft Visual Basic 2017, Eighth Edition 7-9

Mini-Quiz 7-6
1. Write a Dim statement that creates a Random object named randGen.
Answer: Dim randGen As New Random
2. Using the Random object created in Question 1, write a statement that assigns a random
integer from 25 to 100 (including 100) to the intNum variable.
Answer: intNum = randGen.Next(25, 101)
3. Using the Random object created in Question 1, write a statement that assigns a random
integer from 2 to 25 (excluding 25) to the intNum variable.
Answer: intNum = randGen.Next(2, 25)

A-4 Code the Guess a Letter Application


1. Inform students that random numbers will be used in the Guess a Letter application.

2. Show off the interface and pseudocode in Figure 7-40.

3. The purpose of the application is to generate a number and then use the number to select
a letter of the alphabet, the user then attempts to guess the letter by inputting into the
text field labeled “Guess.”

4. Introduce the terms Enabled property and Focus method.

5. Look at the code in Figures 7-41 through 7-43.

6. Look at the final result in Figure 7-44 of the Guess a Letter application.

Mini-Quiz 7-7
1. Write a statement that prevents the btnCalc control from responding to the user.
Answer: btnCalc.Enabled = False
2. Write a statement that reverses the statement from Question 1.
Answer: btnCalc.Enabled = True
3. Write a statement that moves the insertion point to the txtName control.
Answer: txtName.Focus()

A-5 Code the Guess the Word Game Application


1. Explain that the Guess the Word Game application uses many of the concepts learned in
the Focus section of this chapter.

2. The application interface is shown in Figure 7-45.

3. Go over all the parameters of the game and how it supposed to function

4. Talk about the MaxLength property and the PasswordChar property.

© 2018 Cengage. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Programming with Microsoft Visual Basic 2017, Eighth Edition 7-10

5. Go over coding the btnNewWord_Click procedure and the code in Figure 7-47.

6. Discuss the coding of the btnTryLetter_Click procedure and the code in Figures 7-50
through 7-53.

Summary
• Check digits are used for validating numbers, such as credit card numbers, bank account
numbers, product UPCs, and book ISBNs.
• To generate random integers, you first create a Random object to represent the pseudo-
random number generator. You then use the object’s Random.Next method to generate
a random integer. The method returns a number that is greater than or equal to its
minValue argument but less than its maxValue argument. Refer to the syntax and
examples shown earlier in Figure 7-39.
• You disable a control by setting its Enabled property to False either in the Properties
window or in a procedure’s code. You enable it by setting its Enabled property to True.
• You can send the focus to a control using the control’s Focus method.
• A text box’s MaxLength property specifies the maximum number of characters that the
text box will accept.
• A text box’s PasswordChar property specifies the character to use in place of each
character entered in the text box.
• Figure 7-54 contains a summary of the concepts covered in this chapter’s Focus lesson.

Class Discussion Topics

1. Can you describe situations in which it might be necessary to trim leading or trailing
characters from string data input by the user? Can you describe situations in which it
might be necessary to pad string data or to replace portions within a string? Consider
phone numbers, Social Security numbers, and address details.

2. Without looking at a menu in an application, can you name the order in which items
appear in the menu bar? Can you name the items commonly found on the FILE menu in
their correct order? What does this tell you about the importance of following Windows
standards?

Additional Projects
1. Ask students to create a small program that will accept a string input from the user and
then extract every other character. The result should be displayed in a label.

2. Ask students to modify the Guess the Word Game application. Modify the MenuStrip
control to include the following submenu items: Hint. Add appropriate access keys and
© 2018 Cengage. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Programming with Microsoft Visual Basic 2017, Eighth Edition 7-11

shortcut keys, and then write the code to provide the hint. The program should read the
unguessed letters, and the hint should be “The letter is between the letters X and X”. For
example, if the word is “CLOCK” and the user has not guessed the O, the program hint
would be “The letter is between the letters N and P”.

Additional Resources
1. String Manipulation in VB .NET:
www.homeandlearn.co.uk/NET/nets7p1.html

2. Adding Menus to a Visual Basic .NET Form:


www.homeandlearn.co.uk/NET/nets4p1.html

3. Add Shortcuts to Your Menu Items:


www.homeandlearn.co.uk/NET/nets4p4.html

Key Terms
➢ Array of characters—a string; also called a character array
➢ Character array—a string; a group (or array) of related characters
➢ Check digit—a digit that is added to either the beginning or end (but typically the end)
of a number for the purpose of validating the number’s authenticity
➢ Contains method—performs a case-sensitive search to determine whether a string
contains a specific sequence of characters; returns a Boolean value (True or False)
➢ Enabled property—used to enable (True) or disable (False) a control during run time
➢ Focus method—moves the focus to a specified control during run time
➢ IndexOf method—performs a case-sensitive search to determine whether a string
contains a specific sequence of characters; returns either –1 (if the string does not
contain the sequence of characters) or an integer that represents the starting position of
the sequence of characters
➢ Insert method—inserts characters anywhere in a string
➢ Length property—stores an integer that represents the number of characters contained
in a string
➢ Like operator—uses pattern-matching characters to determine whether one string is
equal to another string; performs a case-sensitive comparison
➢ MaxLength property—a property of a text box control; specifies the maximum
number of characters the control will accept
➢ PadLeft method—right-aligns a string by inserting characters at the beginning of the
string
➢ PadRight method—left-aligns a string by inserting characters at the end of the string
➢ PasswordChar property—specifies the character used to hide the user’s input
➢ Pseudo-random number generator—a mathematical algorithm that produces a
sequence of random numbers; in Visual Basic, the pseudo-random generator is
represented by an object whose data type is Random

© 2018 Cengage. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Solution Manual for Programming with Microsoft Visual Basic 2017 8th by Zak

Programming with Microsoft Visual Basic 2017, Eighth Edition 7-12

➢ Random object—represents the pseudo-random number generator in Visual Basic


Random.Next method—used to generate a random integer that is greater than or equal
to a minimum value but less than a maximum value
➢ Remove method—removes a specified number of characters located anywhere in a
string
➢ Replace method—replaces a sequence of characters in a string with another sequence
of characters
➢ Substring method—used to access any number of characters contained in a string
➢ Trim method—removes characters from both the beginning and end of a string
➢ TrimEnd method—removes characters from the end of a string
➢ TrimStart method—removes characters from the beginning of a string

© 2018 Cengage. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.

Visit TestBankBell.com to get complete for all chapters

You might also like