Week 15 May 10 Review 1

advertisement
1
Week 15
May 10
Review
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Final Exam
• Tuesday, May 17
• 12:45 – 2:45
• BRH-104
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
2
Data - Variables and Constants
• Variable
– Memory locations that hold data that can be changed
during project execution
– Dim customerName as String
Declaration
Variable name
Data type
• Named Constant
– Memory locations that hold data that cannot be changed
during project execution
– Const salesTaxRate as Decimal = .0775
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
3
Data Types (p 87 Table 3.1)
•
•
•
•
•
•
•
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)
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
4
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
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
5
Arithmetic Operations
Operator
+
–
*
/
\
Mod
^
Operation
Addition
Subtraction
Multiplication
Division
Integer Division
Modulus – Remainder of division
Exponentiation
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
6
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
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
7
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
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
8
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
=, +=, -=, *=, /=, \=, &=
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
9
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)
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
10
Creating a New Sub Procedure
11
• 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
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
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
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
12
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
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
13
Sub Procedure Example
14
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
Sub Procedure
.ShowDialog( )
End With
End Sub
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Writing Function Procedures
• In the Editor window enclose the lines
of 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
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
15
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
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
16
Function Example
17
Private Sub calculateButton_Click( )
Calling
Dim salesDecimal As Decimal
Procedure
salesDecimal = Decimal.Parse(salesTextBox.Text)
commissionLabel.Text = Commission(salesDecimal.ToString("C"))
End Sub
Private Function Commission(ByVal salesAmountDecimal As Decimal) _
As Decimal
If salesAmountDecimal < 100D Then
Commission = 0D
Else
Function
Commission = 0.15 * salesAmountDecimal
End If
End Function
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Functions with Multiple Arguments
18
• 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
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Object-Oriented (OO) Program
• Objects
Class
– Consist of one
or more data
values which
Data
define the state Message
or properties
of the object
– Encapsulated by
a set of functions (methods) that can be applied to that
object
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
19
Object-Oriented (OO) Program
20
• Class
– Defines:
• Characteristics of the data contained by objects of
the class
• Functions that can be applied to the objects of the
class
Class
Data
Data
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Data
In VB.net
•
•
•
•
Create a class
Define the properties of the class (data)
Build the accessor methods
Encapsulate the data
Build the constructors
with methods
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
21
Object-Oriented Terminology
•
•
•
•
•
Encapsulation
Inheritance
Polymorphism
Reusable Classes
Multitier Applications
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
22
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
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
23
Encapsulation
24
Class
Message
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Data
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
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
25
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
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
26
Inheritance Example
• Base Class
– Person
• Subclasses
– Employee
– Customer
– Student
27
Person
-Name
-Address
-Phone
Properties
Employee
Subclass
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Customer
Student
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
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
28
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
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
29
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
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
30
Decision: If…Then…Else – General Form
If (condition) Then
statement(s)
[ElseIf (condition) Then
statement(s)]
[Else
statement(s)]
End If
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Condition True
False
Statement
Statement
31
Decision: If…Then…Else - Example
unitsDecimal = Decimal.Parse(unitsTextBox.Text)
If unitsDecimal < 32D Then
freshmanRadioButton.Checked = True
Else
freshmanRadioButton.Checked = False
End If
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
32
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
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
33
Decision: The Six Relational Operators
•
•
•
•
•
•
Greater Than
Less Than
Equal To
Not Equal To
Greater Than or Equal To
Less Than or Equal to
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
>
<
=
<>
>=
<=
34
Decision: Compound Conditions
35
Condition 2
Condition 2
Condition 1
• Join conditions using logical operators
OR T
F
– Or
If one or both conditions True,
entire condition is True
T
T
T
– And Both conditions must be True
for entire condition to be True
F
T
F
– Not
Reverses the condition, a
Condition 1
True condition will evaluate False
and vice versa
AND T
F
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
T
T
F
F
F
F
Decision: Compound Condition Examples
36
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
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
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
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
37
Decision: Nested If Statements
If tempInteger > 32 Then
If tempInteger > 80 Then
commentLabel.Text = "Hot"
Else
commentLabel.Text = "Moderate"
End If
Else
commentLabel.Text = "Freezing"
End If
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
38
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
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
39
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
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
40
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
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Top of Loop
Condition,
Pretest
(condition
checked
before the
loop exectures
Bottom of
Loop Condition,
Posttest
(condition
checked after
the loop
executes)
41
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
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
42
Iteration: Do While vs. Do Until
43
• Do While a condition is true or false
Loop
userEntry = False
Do while errorFlag = False
…
If len(customerName.textbox) > 0 Then
…
userEntry = True
Else
…
End If
Loop
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Condition
False
True
Condition
True
False
Iteration: Do While vs. Do Until: Pretest
44
• Do While a condition is true or false
Loop
userEntry = False
Do until errorFlag = True
…
If len(customerName.textbox) > 0 Then
…
userEntry = True
Else
…
End If
Loop
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
Condition
True
False
Condition
True
False
Iteration: Do While vs. Do Until: Posttest
• Do While a condition is true or false
Loop
userEntry = True
Do
…
If len(customerName.textbox) > 0 Then
False
Condition
…
True
userEntry = True
Else
userEntry
…
= True
End If
Loop Until userEntry = True
Condition
(or Loop While userEntry = False)
False
True
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
45
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
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
46
Iteration: The For and Next Statements General Form
For LoopIndex = InitialValue To TestValue [Step Increment]
' Statements in loop.
Next [LoopIndex]
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
47
Iteration: For/Next Loop
• For example:
Dim customerCount as Integer
For customerCount = 1 to 10
...
If customerType = “Regular” Then
…
Else
End If
Next
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
48
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
R. Ching, Ph.D. • MIS Dept. • California State University, Sacramento
49
Download