ebook img

Visual Basic Programs to Accompany Programming Logic and Design PDF

219 Pages·2010·3.6 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Visual Basic Programs to Accompany Programming Logic and Design

SIXTH EDITION M I C R O S O F T ® V I S U A L B A S I C ® P R O G R A M S T O A C C O M P A N Y PROGRAMMING LOGIC AND DESIGN BY JO ANN SMITH Australia • Brazil • Japan • Korea • Mexico • Singapore • Spain • United Kingdom • United States Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Microsoft® Visual Basic® Programs to © 2011 Course Technology, Cengage Learning Accompany Programming Logic and Design, ALL RIGHTS RESERVED. No part of this work covered by the copyright Sixth Edition herein may be reproduced, transmitted, stored, or used in any form or by Jo Ann Smith any means graphic, electronic, or mechanical, including but not limited to photocopying, recording, scanning, digitizing, taping, Web distribution, Executive Editor: Marie Lee information networks, or information storage and retrieval systems, except Acquisitions Editor: Amy Jollymore as permitted under Section 107 or 108 of the 1976 United States Copyright Senior Product Manager: Alyssa Pratt Act, without the prior written permission of the publisher. Development Editor: Mary Pat Shaff er For product information and technology assistance, contact us at Content Project Manager: Jennifer Feltri Cengage Learning Customer & Sales Support, 1-800-354-9706 Art Director: Marissa Falco For permission to use material from this text or product, Proofreader: Suzanne Ciccone submit all requests online at www.cengage.com/permissions Further permissions questions can be e-mailed to Indexer: Sharon Hilgenberg [email protected] Compositor: Integra Library of Congress Control Number: 2010928662 ISBN-13: 978-0-538-74625-0 ISBN-10: 0-538-74625-4 Course Technology 20 Channel Center Street Boston, MA 02210 USA Cengage Learning is a leading provider of customized learning solutions with offi ce locations around the globe, including Singapore, the United Kingdom, Australia, Mexico, Brazil, and Japan. Locate your local offi ce at: international.cengage.com/region Cengage Learning products are represented in Canada by Nelson Education, Ltd. For your lifelong learning solutions, visit course.cengage.com Visit our corporate Web site at cengage.com. Some of the product names and company names used in this book have been used for identifi cation purposes only and may be trademarks or regis- tered trademarks of their respective manufacturers and sellers. Any fi ctional data related to persons or companies or URLs used through- out this book is intended for instructional purposes only. At the time this book was printed, any such data was fi ctional and not belonging to any real persons or companies. Course Technology, a part of Cengage Learning, reserves the right to revise this publication and make changes from time to time in its content without notice. The programs in this book are for instructional purposes only. They have been tested with care, but are not guaranteed for any particular intent beyond educational purposes. The author and the publisher do not off er any warranties or representations, nor do they accept any liabilities with respect to the programs. Printed in the United States of America 1 2 3 4 5 6 7 14 13 12 11 10 Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Brief Contents iii Preface . . . . . . . . . . . . . . . . . xi CHAPTER 1 An Introduction to Visual Basic and the Visual Basic PPrrooggrraammmmiinngg EEnnvviirroonnmmeenntt .. .. 11 CHAPTER 2 Variables, Constants, Operators, and Writing Programs Using Sequential Statements . . . . . . . . . . . . . . . . 11 CHAPTER 3 Writing Structured Visual Basic Programs. . . 31 CHAPTER 4 Writing Programs that Make Decisions . . . . 46 CHAPTER 5 Writing Programs Using Loops . . . . . . . 74 CHAPTER 6 Using Arrays in Visual Basic Programs . . . . 96 CHAPTER 7 File Handling and Applications . . . . . . 112 CHAPTER 8 Advanced Array Techniques . . . . . . . . 127 CHAPTER 9 Advanced Modularization Techniques . . . . 144 CHAPTER 10 Creating a Graphical User Interface (GUI) Using the Visual Studio Integrated Development Environment (IDE) . . . . . . 169 Index . . . . . . . . . . . . . . . . . 193 Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Contents iv Preface . . . . . . . . . . . . . . . . . xi CHAPTER 1 An Introduction to Visual Basic and the Visual Basic PPrrooggrraammmmiinngg EEnnvviirroonnmmeenntt .. .. 11 The Visual Basic Programming Language . . . . . . . . . . 2 Three Types of Visual Basic Programs . . . . . . . . . . . . 2 An Introduction to Object-Oriented Terminology. . . . . . . . 3 The Structure of a Visual Basic Program. . . . . . . . . . . 5 The Visual Basic Development Cycle. . . . . . . . . . . . . 6 Writing Visual Basic Source Code . . . . . . . . . . . . . 7 Compiling a Visual Basic Program . . . . . . . . . . . . . 7 Executing a Visual Basic Program . . . . . . . . . . . . . 9 Exercise 1-1: Understanding How to Compile and Execute Visual Basic Programs . . . . . . . . . . .10 Lab 1.1: Compiling and Executing a Visual Basic Program . . . . . . . . . . . . . . . . . . . . .10 CHAPTER 2 Variables, Constants, Operators, and Writing Programs Using Sequential Statements . . . . . . . . . . . . . . . . 11 Variables . . . . . . . . . . . . . . . . . . . . . . . . .12 Variable Names . . . . . . . . . . . . . . . . . . . . .12 Visual Basic Data Types . . . . . . . . . . . . . . . . . .13 Exercise 2-1: Using Visual Basic Variables, Data Types, and Keywords . . . . . . . . . . . . . . .14 Declaring and Initializing Variables. . . . . . . . . . . . . .14 Exercise 2-2: Declaring and Initializing Visual Basic Variables. . . . . . . . . . . . . . . . . . . . .15 Lab 2.1: Declaring and Initializing Visual Basic Variables. . .16 Constants. . . . . . . . . . . . . . . . . . . . . . . . .17 Unnamed Constants . . . . . . . . . . . . . . . . . . .17 Named Constants . . . . . . . . . . . . . . . . . . . .17 Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. CONTENTS Exercise 2-3: Declaring and Initializing Visual Basic Constants . . . . . . . . . . . . . . . . . . . .18 Lab 2.2: Declaring and Initializing Visual Basic Constants . . . . . . . . . . . . . . . . . . . .18 Arithmetic and Assignment Operators . . . . . . . . . . . .19 Arithmetic Operators. . . . . . . . . . . . . . . . . . .19 v Assignment Operators and the Assignment Statement. . . .20 Precedence and Associativity . . . . . . . . . . . . . . .21 Exercise 2-4: Understanding Operator Precedence and Associativity . . . . . . . . . . . . . . . . . . . .22 Lab 2.3: Arithmetic and Assignment Operators . . . . . . .23 Sequential Statements, Comments, and Interactive Input Statements . . . . . . . . . . . . . . . . . . . . . . .24 Exercise 2-5: Understanding Sequential Statements. . . . .28 Lab 2.4: Using Sequential Statements in a Visual Basic Program . . . . . . . . . . . . . . . . . . . . .29 CHAPTER 3 Writing Structured Visual Basic Programs. . . 31 Using Flowcharts and Pseudocode to Write a Visual Basic Program. . . . . . . . . . . . . . . . . . . . . .32 Lab 3.1: Using Flowcharts and Pseudocode to Write a Visual Basic Program . . . . . . . . . . . . .36 Writing a Modular Program in Visual Basic . . . . . . . . . .38 Lab 3.2: Writing a Modular Program in Visual Basic . . . . .45 CHAPTER 4 Writing Programs that Make Decisions . . . . 46 Boolean Operators. . . . . . . . . . . . . . . . . . . . .47 Comparison Operators . . . . . . . . . . . . . . . . . .47 Logical Operators . . . . . . . . . . . . . . . . . . . .48 Comparison and Logical Operator Precedence and Associativity . . . . . . . . . . . . . . . . . . . .49 Comparing Strings. . . . . . . . . . . . . . . . . . . . .52 Decision Statements. . . . . . . . . . . . . . . . . . . .53 The If Statement . . . . . . . . . . . . . . . . . . . .54 Exercise 4-1: Understanding If Statements . . . . . . . .55 Lab 4.1: Using If Statements . . . . . . . . . . . . . .56 The If Then Else Statement. . . . . . . . . . . . . .57 Exercise 4-2: Understanding If Then Else Statements. .58 Lab 4.2: Using If Then Else Statements . . . . . . . .60 Nested If Statements . . . . . . . . . . . . . . . . . .60 Exercise 4-3: Understanding Nested If Statements . . . .62 Lab 4.3: Using Nested If Statements . . . . . . . . . . .63 Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. CONTENTS The Select Case Statement . . . . . . . . . . . . . .64 Exercise 4-4: Using a Select Case Statement. . . . . . . . . . . 67 Lab 4.4: Using a Select Case Statement . . . . . . . .67 Using Decision Statements to Make Multiple Comparisons . . . . . . . . . . . . . . . . . . . . . .68 Using AND Logic. . . . . . . . . . . . . . . . . . . . .69 vi Using OR Logic . . . . . . . . . . . . . . . . . . . . .69 Exercise 4-5: Making Multiple Comparisons in Decision Statements . . . . . . . . . . . . . . . . .70 Lab 4.5: Making Multiple Comparisons in Decision Statements . . . . . . . . . . . . . . . . .72 CHAPTER 5 Writing Programs Using Loops . . . . . . . 74 Writing a Do While Loop in Visual Basic . . . . . . . . . .75 Exercise 5-1: Using a Do While Loop . . . . . . . . . .76 Using a Counter to Control a Loop . . . . . . . . . . . . .77 Exercise 5-2: Using a Counter-Controlled Do While Loop . .78 Lab 5.1: Using a Counter-Controlled Do While Loop. . . .78 Using a Sentinel Value to Control a Loop. . . . . . . . . . .79 Exercise 5-3: Using a Sentinel Value to Control a Do While Loop . . . . . . . . . . . . . . . . . . .81 Lab 5.2: Using a Sentinel Value to Control a Do While Loop . . . . . . . . . . . . . . . . . . .81 Writing a For Loop in Visual Basic . . . . . . . . . . . . .82 Exercise 5-4: Using a For Loop. . . . . . . . . . . . . .84 Lab 5.3: Using a For Loop . . . . . . . . . . . . . . . .84 Writing a Do Until Loop in Visual Basic . . . . . . . . . .85 Exercise 5-5: Using a Do Until Loop . . . . . . . . . .86 Lab 5.4: Using a Do Until Loop. . . . . . . . . . . . .86 Nesting Loops. . . . . . . . . . . . . . . . . . . . . . .87 Exercise 5-6: Nesting Loops . . . . . . . . . . . . . . .88 Lab 5.5: Nesting Loops . . . . . . . . . . . . . . . . .88 Accumulating Totals in a Loop . . . . . . . . . . . . . . .89 Exercise 5-7: Accumulating Totals in a Loop . . . . . . . .91 Lab 5.6: Accumulating Totals in a Loop . . . . . . . . . .92 Using a Loop to Validate Input . . . . . . . . . . . . . . .93 Exercise 5-8: Validating User Input . . . . . . . . . . . .94 Lab 5.7: Validating User Input. . . . . . . . . . . . . . .95 CHAPTER 6 Using Arrays in Visual Basic Programs . . . . 96 Array Basics . . . . . . . . . . . . . . . . . . . . . . .97 Declaring Arrays. . . . . . . . . . . . . . . . . . . . .97 Initializing Arrays . . . . . . . . . . . . . . . . . . . .98 Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. CONTENTS Accessing Array Elements . . . . . . . . . . . . . . . .99 Staying Within the Bounds of an Array . . . . . . . . . . .99 Using Constants with Arrays . . . . . . . . . . . . . . 100 Exercise 6-1: Array Basics . . . . . . . . . . . . . . . 101 Lab 6.1: Using Arrays . . . . . . . . . . . . . . . . . 102 Searching an Array for an Exact Match . . . . . . . . . . 102 vii Exercise 6-2: Searching an Array for an Exact Match . . . 105 Lab 6.2: Searching an Array for an Exact Match . . . . . 106 Parallel Arrays. . . . . . . . . . . . . . . . . . . . . . 107 Exercise 6-3: Parallel Arrays . . . . . . . . . . . . . . 110 Lab 6.3: Parallel Arrays . . . . . . . . . . . . . . . . 111 CHAPTER 7 File Handling and Applications . . . . . . 112 File Handling . . . . . . . . . . . . . . . . . . . . . . 113 Opening a File for Reading . . . . . . . . . . . . . . . 113 Reading Data from an Input File . . . . . . . . . . . . . 114 Reading Data Using a Loop and EOF. . . . . . . . . . . 114 Opening a File for Writing. . . . . . . . . . . . . . . . 115 Writing Data to an Output File . . . . . . . . . . . . . . 115 Exercise 7-1: Opening Files and Performing File Input . . . 117 Lab 7.1: Using an Input File. . . . . . . . . . . . . . . 118 Understanding Sequential Files and Control Break Logic. . . 119 Exercise 7-2: Accumulating Totals in Single-Level Control Break Programs . . . . . . . . . . . . . . . 124 Lab 7.2: Accumulating Totals in Single-Level Control Break Programs . . . . . . . . . . . . . . . 125 CHAPTER 8 Advanced Array Techniques . . . . . . . . 127 Sorting Data . . . . . . . . . . . . . . . . . . . . . . 128 Swapping Data Values . . . . . . . . . . . . . . . . . . 129 Exercise 8-1: Swapping Values . . . . . . . . . . . . . 129 Lab 8.1: Swapping Values . . . . . . . . . . . . . . . 130 Using a Bubble Sort . . . . . . . . . . . . . . . . . . . 130 The Main() Procedure . . . . . . . . . . . . . . . . 134 The fillArray() Procedure . . . . . . . . . . . . . 135 The sortArray() Procedure . . . . . . . . . . . . . 136 The displayArray() Procedure . . . . . . . . . . . 137 Exercise 8-2: Using a Bubble Sort. . . . . . . . . . . . 137 Lab 8.2: Using a Bubble Sort . . . . . . . . . . . . . . 138 Using Multidimensional Arrays . . . . . . . . . . . . . . 139 Exercise 8-3: Using Multidimensional Arrays . . . . . . . 142 Lab 8.3: Using Multidimensional Arrays . . . . . . . . . 142 Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. CONTENTS CHAPTER 9 Advanced Modularization Techniques . . . . 144 Writing Procedures with No Parameters . . . . . . . . . . 145 Exercise 9-1: Writing Procedures with No Parameters . . . 147 Lab 9.1: Writing Procedures with No Parameters . . . . . 148 Writing Procedures that Require a Single Parameter . . . . 148 viii Exercise 9-2: Writing Procedures that Require a Single Parameter. . . . . . . . . . . . . . . . . . 151 Lab 9.2: Writing Procedures that Require a Single Parameter . . . . . . . . . . . . . . . . . . . . . . 152 Writing Procedures that Require Multiple Parameters . . . . 152 Exercise 9-3: Writing Procedures that Require Multiple Parameters . . . . . . . . . . . . . . . . . 154 Lab 9.3: Writing Procedures that Require Multiple Parameters . . . . . . . . . . . . . . . . . 155 Writing Functions that Return a Value . . . . . . . . . . . 156 Exercise 9-4: Writing Functions that Return a Value . . . . 158 Lab 9.4: Writing Functions that Return a Value . . . . . . 159 Passing an Array and an Array Element to a Procedure or Function . . . . . . . . . . . . . . . . . . . . . . 160 Exercise 9-5: Passing Arrays to Procedures and Functions . . . . . . . . . . . . . . . . . . . . 163 Lab 9.5: Passing Arrays to Procedures and Functions. . . 164 Using Visual Basic’s Built-In Functions . . . . . . . . . . . 165 Exercise 9-6: Using Visual Basic’s Built-In Functions. . . . 167 Lab 9.6: Using Visual Basic’s Built-In Functions . . . . . . 167 CHAPTER 10 Creating a Graphical User Interface (GUI) Using the Visual Studio Integrated Development Environment (IDE) . . . . . . 169 Graphical User Interface Programs . . . . . . . . . . . . 170 The Visual Studio Integrated Development Environment . . . 170 Components of a Visual Basic Solution. . . . . . . . . . . 172 The Solution Folder . . . . . . . . . . . . . . . . . . 172 The Designer Window . . . . . . . . . . . . . . . . . 173 The Code Window . . . . . . . . . . . . . . . . . . . 174 Design-Time and Run-Time Operating Modes . . . . . . . . 175 Creating a Visual Basic IDE Program. . . . . . . . . . . . 176 Designing the Form for the Doubler Program . . . . . . . 177 Writing the Code for the Doubler Program . . . . . . . . 180 Exercise 10-1: Elements of a GUI in Microsoft Visual Studio. . . . . . . . . . . . . . . . . . . . . 181 Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. CONTENTS Lab 10.1: Creating a Visual Basic GUI Program in Microsoft Visual Studio . . . . . . . . . . . . . . . 182 A Programmer-Defined Class . . . . . . . . . . . . . . . 182 Creating a Programmer-Defined Class . . . . . . . . . . 183 Adding Properties to a Class . . . . . . . . . . . . . . 185 Adding Methods to a Class . . . . . . . . . . . . . . . 186 ix Exercise 10-2: Creating a Class in Visual Basic . . . . . . 189 Lab 10.2: Creating a Class in Visual Basic . . . . . . . . 190 Index . . . . . . . . . . . . . . . . . 193 Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.

Description:
The Visual Basic PAL is designed to be paired with the Sixth Edition of Joyce Farrell's Programming Logic and Design text. Together, the two books provide the perfect opportunity to learn the fundamentals of programming while gaining exposure to an actual programming language. Readers can discover h
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.