250 likes | 583 Views
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.
E N D
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
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
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
Open the Commission Calculator Project and insert the comment statement Clearly Visual Basic: Programming with Visual Basic 2008
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
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
NOTE: The value (VAL) of Text or an empty box is ZERO Clearly Visual Basic: Programming with Visual Basic 2008
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
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
Mini Quiz: • Write an expression to add 100 to the contents of lblTotal • If the user enters $67.45 in txtTax.Text, what does Val(txtTax.Text) return? • 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
Review Questions • Comments in VB start with an: ‘apostrophe • If the user enters $5 in txtPrice and 3 in the txtQuantity, what is: Val(txtPrice) * Val(txtQuantity)? 0zero 3 15 None of These • If the user enters 75 in txtNum, which changes it to a negative 75 (-75)? -txtNum.Text = Val(txtNum.Text) -txtNum.Text = txtNum.TexttxtNum.Text = -Val(txtNum.Text) None of These • If txtNum contains value 82, which equals 4?(Val(txtNum.Text)+6)/22 Val(txtNum.Text) Mod 6Val(txtNum.Text) \ 20 • 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
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 Clearly Visual Basic: Programming with Visual Basic 2008
Open Tip Calculator- Code the Calculate Button Clearly Visual Basic: Programming with Visual Basic 2008
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
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
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
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
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
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