Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 6 The Secret Code.

25
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 6 The Secret Code

Transcript of Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 6 The Secret Code.

Page 1: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 6 The Secret Code.

Clearly Visual Basic: Programming with Visual Basic 2008

Chapter 6 The Secret Code

Page 2: 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

Page 3: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 6 The Secret Code.

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

Page 4: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 6 The Secret Code.

Clearly Visual Basic: Programming with Visual Basic 2008 4

Page 5: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 6 The Secret Code.

Clearly Visual Basic: Programming with Visual Basic 2008 5

Page 6: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 6 The Secret Code.

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

Page 7: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 6 The Secret Code.

Clearly Visual Basic: Programming with Visual Basic 2008 7

Open the Commission Calculator Project and insert the comment statement

Page 8: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 6 The Secret Code.

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

Page 9: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 6 The Secret Code.

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

Page 10: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 6 The Secret Code.

Clearly Visual Basic: Programming with Visual Basic 2008 10

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

Page 11: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 6 The Secret Code.

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

Page 12: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 6 The Secret Code.

Clearly Visual Basic: Programming with Visual Basic 2008 12

Page 13: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 6 The Secret Code.

Clearly Visual Basic: Programming with Visual Basic 2008 13

Page 14: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 6 The Secret Code.

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

Page 15: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 6 The Secret Code.

Expression Result

2^3 8

4 * -3 -12

25/4 6.25

25\4 6

25 Mod 4 1

7 + 6 * (5-2) 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

Page 16: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 6 The Secret Code.

Clearly Visual Basic: Programming with Visual Basic 2008 16

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

Page 17: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 6 The Secret Code.

Review Questions1.Comments in VB start with an: ‘apostrophe2.If the user enters $5 in txtPrice and 3 in the txtQuantity, what is: Val(txtPrice) * Val(txtQuantity)? 0zero 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 6Val(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) * 2lblTotal.Text =Val(txtBlue.Text) * 1.1

Page 18: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 6 The Secret Code.

Clearly Visual Basic: Programming with Visual Basic 2008 18

Open Commission Calculator- Code the Calculate Button

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 with2000 and 10 for the percent- Results: 200

Lock the program!!Lock the

Form

Page 19: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 6 The Secret Code.

Clearly Visual Basic: Programming with Visual Basic 2008 19

Open Tip Calculator- Code the Calculate Button

Page 20: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 6 The Secret Code.

Clearly Visual Basic: Programming with Visual Basic 2008 20

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

Vans and More Depot Project Page 112- #5

Page 21: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 6 The Secret Code.

Clearly Visual Basic: Programming with Visual Basic 2008 21

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

Modified Vans and More Depot Project Page 113- #7

Page 22: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 6 The Secret Code.

Clearly Visual Basic: Programming with Visual Basic 2008 22

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

Page 23: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 6 The Secret Code.

Clearly Visual Basic: Programming with Visual Basic 2008 23

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

Page 24: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 6 The Secret Code.

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

Page 25: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 6 The Secret Code.

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