1 / 49

Week 15 May 10

Week 15 May 10. Review. Final Exam. Tuesday, May 17 12:45 – 2:45 BRH-104. Data - Variables and Constants. Variable Memory locations that hold data that can be changed during project execution Dim customerName as String Named Constant

Télécharger la présentation

Week 15 May 10

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Week 15May 10 Review

  2. Final Exam • Tuesday, May 17 • 12:45 – 2:45 • BRH-104

  3. Data - Variables and Constants • Variable • Memory locations that hold data that can be changed during project execution • Dim customerName as String • Named Constant • Memory locations that hold data that cannot be changed during project execution • Const salesTaxRate as Decimal = .0775 Declaration Variable name Data type

  4. Boolean Byte (0 to 255) Char Date String Decimal Object Short (-32,768 to 32,767) Integer (-2,147,483,648 to 2,147,483,647) Long (larger whole numbers) Single (floating point accuracy to 6 digits) Double (floating point accuracy to 14 digits) Data Types (p 87 Table 3.1)

  5. Declaring Variables • Declared inside a procedure using a Dim statement • Declared outside a procedure using Public, Private or Dim statements • Always declare the variable’s data type • May declare several variables with one statement • Use IntelliSense to assist in writing statements

  6. Arithmetic Operations • Operator Operation • + Addition • – Subtraction • * Multiplication • / Division • \ Integer Division • Mod Modulus – Remainder of division • ^ Exponentiation

  7. Order of Operations • Order of precedence in arithmetic expressions from highest to lowest 1. Any operation inside parentheses 2. Exponentiation 3. Multiplication and division 4. Integer division 5. Modulus 6. Addition and subtraction

  8. Mathematical Examples • Note the use of parentheses to control order of precedence 3+4*2 = 11 Multiply then add (3+4)*2 = 14 Parentheses control: add then multiply 8/4*2 = 4 Same level, left to right: divide then multiply

  9. Using Calculations in Code • Perform calculations in assignment statements • What appears on right side of assignment operator is assigned to item on left side • Assignment operators =, +=, -=, *=, /=, \=, &=

  10. Writing General Procedures • A general procedure is reusable code which can be called from multiple procedures • Useful for breaking down large sections of code into smaller units • Two types • Sub Procedure performs actions • Function performs actions AND returns a value (the return value)

  11. Creating a New Sub Procedure • In the Editor window enclose the lines of code with a set of Private Sub and End Sub statements • To use the Sub Procedure, call it from another procedure • Code in a Sub Procedure cannot be executed unless called from another procedure Private Sub ProcedureName( ) ' Statements in the procedure. End Sub

  12. Passing Arguments to Procedures (page 205) • Declare variable as local and pass to any called procedures • If a sub procedure names an argument, any call to the procedure must supply the argument • Name of local variable does not need to match name in sub procedure argument list • Number of arguments, sequence and data type must match

  13. Passing Arguments ByVal or ByRef • ByVal (default) • Sends a copy, original cannot be altered • ByRef • Sends a reference to the memory location where the original is stored and therefore the original can be altered • Examples page 206

  14. Sub Procedure Example Private Sub changeTitleButtonColor_Click( ) Dim originalColor As Color originalColor = titleLabel.ForeColor SelectColor(originalColor) titleLabel.ForeColor = ColorDialog1.Color End Sub Calling Procedure Private Sub SelectColor(incomingColor As Color) With ColorDialog1 .Color = incomingColor .ShowDialog( ) End With End Sub Sub Procedure

  15. Writing Function Procedures • In the Editor window enclose the linesof code with Private Function( ) and End Function statements • To use the Function, Call it by using it in an expression • Pass arguments ByVal or ByRef Private Function FunctionName( ) As Datatype ' Statements to execute. End Function

  16. Returning the Result of a Function • To return a value to the calling procedure set up a return value • The return value will be placed by VB in a variable with the SAME name as the Function's name OR • Use the Return statement to return the value

  17. Function Example Private Sub calculateButton_Click( ) Dim salesDecimal As Decimal salesDecimal = Decimal.Parse(salesTextBox.Text) commissionLabel.Text = Commission(salesDecimal.ToString("C")) End Sub Calling Procedure Private Function Commission(ByVal salesAmountDecimal As Decimal) _ As Decimal If salesAmountDecimal < 100D Then Commission = 0D Else Commission = 0.15 * salesAmountDecimal End If End Function Function

  18. Functions with Multiple Arguments • Functions can receive one or more arguments (values) • Sequence and data type of arguments in Call must exactly match arguments in function header Private Function Payment(ByVal rateDecimal As Decimal, _ ByVal timeDecimal As Decimal, ByVal amountDecimal _ As Decimal) As Decimal paymentLabel.Text = Payment(Decimal.Parse(rateTextBox.Text), _ Decimal.Parse(yearsTextBox.Text), _ Decimal.Parse(principalTextBox.Text)).ToString( )End Function

  19. Object-Oriented (OO) Program • Objects • Consist of oneor more datavalues whichdefine the stateor propertiesof the object • Encapsulated bya set of functions (methods) that can be applied to that object Class Method Method Data Message Method Method

  20. Object-Oriented (OO) Program • Class • Defines: • Characteristics of the data contained by objects of the class • Functions that can be applied to the objects of the class Class Method Method Method Method Method Method Data Data Data Method Method Method Method Method Method

  21. In VB.net • Create a class • Define the properties of the class (data) • Build the accessor methods • Build the constructors Encapsulate the data with methods

  22. Object-Oriented Terminology • Encapsulation • Inheritance • Polymorphism • Reusable Classes • Multitier Applications

  23. Encapsulation • Combination of characteristics of an object along with its behavior in "one package" • Cannot make object do anything it does not already "know" how to do • Cannot make up new properties, methods, or events • Sometimes referred to as data hiding; an object can expose only those data elements and procedures that it wishes

  24. Encapsulation Class Method Method Data Message Method Method

  25. Inheritance • Ability to create a new class from an existing class • Original class is called Base Class, Superclass, or Parent Class • Inherited class is called Subclass, Derived Class, or Child Class • For example, each form created is inherited from the existing Form class • Purpose of inheritance is reusability

  26. Inheritance (continued) • Examine first line of code for a form in the Editor Inherited Class: Subclass, Derived Class,Child Class Public Class Form1 Inherits System.Windows.Forms.Form Original Class: Base Class, Superclass, Parent Class

  27. Person -Name -Address -Phone Employee Customer Student Inheritance Example • Base Class • Person • Subclasses • Employee • Customer • Student Properties Subclass

  28. Polymorphism • Methods having identical names but different implementations • Overloading • Several argument lists for calling the method • Example: MessageBox.Show method • Overriding • Refers to a class that has the same method name as its base class • Method in subclass takes precedence

  29. Reusability • Big advantage of OOP over traditional programming • New classes created can be used in multiple projects • Each object created from the class can have its own properties

  30. Decision: If Statements • Used to make decisions • If true, only the Then clause is executed, if false, only Else clause, if present, is executed • Block If…Then…Else must always conclude with End If • Then must be on same line as If or ElseIf • End If and Else must appear alone on a line • Note: ElseIf is 1 word, End If is 2 words

  31. Decision: If…Then…Else – General Form If (condition) Then statement(s) [ElseIf (condition) Then statement(s)] [Else statement(s)] End If True Condition False Statement Statement

  32. Decision: If…Then…Else - Example unitsDecimal = Decimal.Parse(unitsTextBox.Text) If unitsDecimal < 32D ThenfreshmanRadioButton.Checked = True ElsefreshmanRadioButton.Checked = False End If

  33. Decision: Conditions • Test in an If statement is based on a condition • Six relational operators are used for comparison • Negative numbers are less than positive numbers • An equal sign is used to test for equality • Strings can be compared, enclose strings in quotes (see Page 142 for ANSI Chart, case matters) • JOAN is less than JOHN • HOPE is less than HOPELESS • Numbers are always less than letters • 300ZX is less than Porsche

  34. Decision: The Six Relational Operators • Greater Than > • Less Than < • Equal To = • Not Equal To <> • Greater Than or Equal To >= • Less Than or Equal to <=

  35. Decision: Compound Conditions Condition 1 • Join conditions using logical operators • Or If one or both conditions True, entire condition is True • And Both conditions must be True for entire condition to be True • Not Reverses the condition, a True condition will evaluate False and vice versa OR T F T T T Condition 2 F T F Condition 1 AND T F T T F Condition 2 F F F

  36. Decision: Compound Condition Examples If maleRadioButton.Checked And_ Integer.Parse(ageTextBox.Text) < 21 Then minorMaleCountInteger += 1 End If If juniorRadioButton.Checked Or seniorRadioButton.Checked Then upperClassmanInteger += 1 End If

  37. Decision: Combining And and Or Example If saleDecimal > 1000.0 Or discountRadioButton.Checked _ And stateTextBox.Text.ToUpper( ) <> "CA" Then ' Code here to calculate the discount. End If

  38. Decision: Nested If Statements If tempInteger > 32 Then If tempInteger > 80 Then commentLabel.Text = "Hot" Else commentLabel.Text = "Moderate" EndIf Else commentLabel.Text = "Freezing" End If

  39. Iteration: Do/Loops • Repeating a series of instructions • An iteration is a single execution of the statement(s) in the loop • Used when the exact number of iterations is unknown

  40. Iteration: Do/Loops (continued) • Terminates based on a specified condition • Loop While a condition is True • Loop Until a condition becomes True • Condition can be placed at • Top of loop - Pretest • Bottom of loop - Posttest

  41. Iteration: The Do and Loop Statements -General Form Do {While |Until} condition ' Statements in loop. Loop OR Do ' Statements in loop. Loop {While | Until} condition Top of Loop Condition, Pretest (condition checked before the loop exectures Bottom of Loop Condition, Posttest (condition checked after the loop executes)

  42. Iteration: Pretest vs. Posttest • Pretest, loop may never be executed since tested BEFORE running • Do While … Loop • Do Until … Loop • Posttest, loop will always be executed at least once • Do … Loop While • Do … Loop Until

  43. Iteration: Do While vs. Do Until • Do While a condition is true or falseuserEntry = FalseDo while errorFlag = False … If len(customerName.textbox) > 0 Then … userEntry = True Else … End IfLoop Condition False True Condition False True Loop

  44. Iteration: Do While vs. Do Until: Pretest • Do While a condition is true or falseuserEntry = FalseDo until errorFlag = True … If len(customerName.textbox) > 0 Then … userEntry = True Else … End IfLoop Condition True False Condition False True Loop

  45. Iteration: Do While vs. Do Until: Posttest • Do While a condition is true or falseuserEntry = TrueDo … If len(customerName.textbox) > 0 Then … userEntry = True Else … End IfLoop Until userEntry = True (or Loop While userEntry = False) Condition False True Loop userEntry = True Condition False True

  46. Iteration: For/Next Loops • Use when you know the number of iterations • Uses a numeric counter variable, called Loop Index, to control number of iterations • Loop Index is incremented at the bottom of the loop on each iteration • Step value can be included to specify the incrementing amount to increment Loop Index, step can be a negative number

  47. Iteration: The For and Next Statements - General Form For LoopIndex = InitialValue To TestValue [Step Increment] ' Statements in loop. Next [LoopIndex]

  48. Iteration: For/Next Loop • For example:Dim customerCount as IntegerFor customerCount = 1 to 10 ... If customerType = “Regular” Then … Else End IfNext

  49. Iteration: Exiting For/Next Loops • In some situations you may need to exit the loop prematurely • Use the Exit For statement inside the loop structure • Generally the Exit For statement is part of an If statement

More Related