Chapter 6 - Bobgill.com

advertisement

Clearly Visual Basic: Programming with Visual Basic 2008

Chapter 6

The Secret Code

Objectives

• Include a comment in the Code Editor window

• Use the Val function to convert text to a number

• Write expressions containing arithmetic operators

• Write an assignment statement

Clearly Visual Basic: Programming with Visual Basic 2008 2

The Fun Starts Here

• Coding the algorithm

– Fifth step in the problem-solving process

• Figure 6-1

– Shows the Addison Smith problem’s interface

• Figure 6-2

– Shows the problem’s output, input, and algorithm

Clearly Visual Basic: Programming with Visual Basic 2008 3

Clearly Visual Basic: Programming with Visual Basic 2008 4

Clearly Visual Basic: Programming with Visual Basic 2008 5

The Fun Starts Here (continued)

• Comment: internal documentation

– Message to person reading the code

– Makes code more readable

– Created by placing an apostrophe (’) before the text that represents the comment

Clearly Visual Basic: Programming with Visual Basic 2008 6

Open the Commission Calculator Project and insert the comment statement

Clearly Visual Basic: Programming with Visual Basic 2008 7

The Val Function

• Characters entered in a text box

– Can be numbers, letters, or special characters

• Function

– A predefined procedure that performs a specific task and then returns a value after completing the task

• Val function

– Temporarily converts one or more characters to a number and then returns the number

Clearly Visual Basic: Programming with Visual Basic 2008 8

The Val Function (continued)

• Val( text )

– Syntax of Val function

• Argument

– Information passed to the function while function is processing

• When an invalid character is encountered in the text argument

– Val function stops the conversion process at that point

Clearly Visual Basic: Programming with Visual Basic 2008 9

NOTE: The value (VAL) of Text or an empty box is ZERO

Clearly Visual Basic: Programming with Visual Basic 2008 10

Who’s in Charge of This Operation?

• Figure 6-5

– Lists the most commonly used arithmetic operators available in Visual Basic

• Precedence numbers

– Indicate order in which computer performs operation in an expression

– Parentheses can be used to override order of precedence

Clearly Visual Basic: Programming with Visual Basic 2008 11

Clearly Visual Basic: Programming with Visual Basic 2008 12

Clearly Visual Basic: Programming with Visual Basic 2008 13

Who’s in Charge of This Operation?

(continued)

• Integer division operator ( \ )

– Used to divide two integers (whole numbers) and then return the result as an integer

• Modulus operator (Mod)

– Returns the remainder of the division

Clearly Visual Basic: Programming with Visual Basic 2008 14

Expression

2^3

4 * -3

25/4

25\4

25 Mod 4

7 + 6 * (5-2)

Result

8

-12

6.25

6

1

25

Mini Quiz:

1. Write an expression to add 100 to the contents of lblTotal

2. If the user enters $67.45 in txtTax.Text, what does Val(txtTax.Text) return?

3. If the users enters 23 in txtTotal, what is the Val(txtTotal.Text) Mod 2

Insert the code to calculate the commission lblCommission.Text = Val(txtSales.Text) * Val(txtRate.Text)

Clearly Visual Basic: Programming with Visual Basic 2008 16

Review Questions

1. Comments in VB start with an:

‘apostrophe

2. If the user enters $5 in txtPrice and 3 in the txtQuantity, what is: Val(txtPrice) * Val(txtQuantity)? 0 zero 3 15 None of These

3. If the user enters 75 in txtNum, which changes it to a negative 75 (-75)?

-txtNum.Text = Val(txtNum.Text) -txtNum.Text = txtNum.Text

txtNum.Text = -Val(txtNum.Text) None of These

4. If txtNum contains value 82, which equals 4?

(Val(txtNum.Text)+6)/22 Val(txtNum.Text) Mod 6

Val(txtNum.Text) \ 20

5. Which expression will NOT calculate correctly?

lblTotal.Text =Val(txtSales1.text)+ Val(txtSales2.text) lblTotal.Text – Val(txtSales.Text + txtSales2.Text) lblTotal.Text = Val(txtRed.Text) * 2 lblTotal.Text =Val(txtBlue.Text) * 1.1

Open Commission Calculator- Code the Calculate Button

Lock the program!!

Form

Change the Rate

Box, so that it accepts the rate as a percent and not a decimal. For example, you would enter 6 in for the rate instead of .06

Test the solution with

2000 and 10 for the percent- Results: 200

Clearly Visual Basic: Programming with Visual Basic 2008 18

Open Tip Calculator- Code the Calculate Button

Clearly Visual Basic: Programming with Visual Basic 2008 19

Vans and More Depot Project Page 112- #5

Create an application for Vans and More Depot, which rents vans for company outings. Each van can transport 10 people. When you enter the number of people in the applications, the program will tell you how many vans you need and how many people need alternate transportation. For example if you enter 48 people, the program will tell you that you need 4 vans and alternate transportation for 8 people

Clearly Visual Basic: Programming with Visual Basic 2008 20

Modified Vans and More Depot Project Page 113- #7

Modify Vans and More Depot so that each van can transport 10 people, each car can transport 5 people, and the rest will have to find other transportation. When you enter the number of people in the applications, the program will tell you how many vans you need, how many cars you need, and how many people need alternate transportation. For example if you enter 48 people, the program will tell you that you need 4 vans, 1 car and 3 people need to find alternative transportations

Clearly Visual Basic: Programming with Visual Basic 2008 21

Page 113 #8 Sun Project

Program will ask for Name, Hours, and Rate. Then it will figure out the net pay after subtracting 20% for Federal Withholding tax,

8% for Social Security tax and 2% for State Income Tax

Clearly Visual Basic: Programming with Visual Basic 2008 22

Page 114- #9 RM Sales (Sales Solution)

Program RM Sales that asks for the amount of sales for each region.

The program will compute the sales percentage of each region

Clearly Visual Basic: Programming with Visual Basic 2008 23

Summary

• Fifth step in the problem-solving process

– Code the problem’s algorithm

• When you want a procedure to make a calculation

– Tell it how to make the calculation and where to store the result

• Use comments to internally document a project’s code

• Val function

– Temporarily converts one or more characters to a number and then returns number

Clearly Visual Basic: Programming with Visual Basic 2008 24

Summary (continued)

• Arithmetic operators having same precedence number

– Evaluated from left to right in an expression

• Parentheses

– Can be used to override order of precedence for arithmetic operators

• Assignment statement

– Used to assign a value to something while an application is running

Clearly Visual Basic: Programming with Visual Basic 2008 25

Download