programming language 2 (pl2) - pbworksbasilissachin.pbworks.com/w/file/fetch/69509891/337.3 apply...

102
Programming Language 2 (PL2)

Upload: phamhanh

Post on 21-Aug-2018

238 views

Category:

Documents


2 download

TRANSCRIPT

Programming Language 2 (PL2)

337.3.1 – Illustrate the logical flow of program in sequence, selection and iteration structure

337.3.2 – Apply selection and repetitive structure

Repetition structure (or loop): a structure that repeatedly processes one or more program instructions until a condition is met

Pretest loop

The condition is evaluated before the instructions within the loop are processed

The instructions may be processed zero or more times

Posttest loop

The condition is evaluated after the instructions within the loop are processed

The instructions are always processed at least once

Repetition statements in Visual Basic Do...Loop

For...Next

For Each...Next

Do...Loop statement: codes both a pretest or posttest loop

Use While or Until to code the condition for the loop

Repetition symbol in a flowchart is the diamond

Figure 6-1: How to use the Do…Loop statement

Figure 6-1: How to use the Do…Loop statement (continued)

Figure 6-2: Processing steps for the pretest loop example

Figure 6-3: Processing steps for the posttest loop example

Figure 6-4: Pseudocode and flowchart for the pretest loop example

Figure 6-5: Pseudocode and flowchart for the posttest loop example

Figure 6-6: Examples showing that the pretest and posttest loops do not always produce the same results

Counter: a numeric variable used for counting

Accumulator: a numeric variable used for accumulating (adding together)

Initializing: assigning a beginning value to a counter or accumulator variable

Updating (or incrementing): adding a number to the value of a counter or accumulator variable

Counters are always incremented by a constant value, usually 1

Function: a predefined procedure that performs a specific task and returns a value

InputBox function: displays a predefined dialog box that allows the user to enter data

Contains a text message, an OK button, a Cancel button, and an input area

InputBox function returns: The user’s entry if the user clicks the OK button

An empty string if the user clicks the Cancel button or the Close button on the title bar

Figure 6-7: Example of a dialog box created by the InputBox function

• InputBox function parameters:

– prompt: the message to display inside the dialog box

– title: the text to display in the dialog box’s title bar

– defaultResponse: a prefilled value for the user input

Figure 6-8: How to use the InputBox function

Figure 6-8: How to use the InputBox function (continued)

• Requirements: display the average amount the

company sold during the prior year

• Input: the amount of each salesperson’s sales

• Priming read: used to obtain the first input

• Must verify that a variable does not contain the

value 0 before using it as a divisor

Figure 6-9: Pseudocode for the Sales Express application

Figure 6-10: Sales Entry dialog box

Figure 6-11: Average sales amount displayed in the interface

Figure 6-2: Code for the calcButton in the Sales Express application

List box: displays a list of choices from which the user can select zero or more choices

SelectionMode property: controls the number of choices a user can select

None: user can scroll but not select anything

One: user can select one item

MultiSimple and MultiExtended: user can select multiple items

Items collection: a collection of the items in a list box

Collection: a group of one or more individual objects treated as one unit

Add method: adds an item to the list box’s Items collection

Items to be added must be converted to String

Load event of a form: occurs when an application is started and the form is displayed for the first time

Figure 6-13: How to add items to a list box

Figure 6-14: Add methods entered in the form’s Load event procedure

Sorted property: Determines if the list box items are sorted

Sort order is dictionary order

Figure 6-15: Items added to the animalListBox and codeListBox

Index: A unique number that identifies an item in a collection

Is zero-relative: the first item has index of 0

Figure 6-16: How to access an item in a list box

Items.Count property: stores the number of items in a list box

Count value is always one higher than the highest index in the list box

Figure 6-17: How to determine the number of items in a list box

SelectedItem property: Contains the value of the selected item in the list

If nothing is selected, it contains the empty string

SelectedIndex property: Contains the index of the selected item in the list

If nothing is selected, it contains the value -1

Default list box item: the item that is selected by default when the interface first appears

Figure 6-18: Item selected in the animalListBox

Figure 6-19: How to use the SelectedItem and SelectedIndex properties

Figure 6-20: How to select the default list box item

Figure 6-21: Code to select the default item in each list box

• SelectedValueChanged and SelectedIndexChanged events:

– Occur when a user selects an item in a list box

– Occur when a code statement selects an item in a list box

Figure 6-22: SelectedValueChanged and SelectedIndexChanged event procedures

Figure 6-23: Result of processing the SelectedValueChanged and SelectedIndexChanged event procedures

• Allows the user to enter a product ID

• Searches for the ID in a list box

• If found, highlights the ID

Figure 6-24: Pseudocode for the Product Finder application

Figure 6-25: Sample run of the application when an ID is found

Figure 6-26: Sample run of the application when an ID is not found

Figure 6-27: Code for the form’s Load event and findButton’s Click event procedures

For...Next statement: processes a set of instructions a known number of times

Is a pretest loop

Counter variable: used to keep track of the number of times the loop has been processed

Startvalue, endvalue, and stepvalue items Control the number of times the loop is processed

Must evaluate to numeric values

Can be positive or negative

A negative stepvalue causes the loop counter to count down

Figure 7-1: How to use the For…Next statement

Figure 7-2: Processing tasks for the For…Next statement

Figure 7-3: Processing steps for the code shown in Example 1

Flowchart symbol for the For...Next loop is a hexagon

Values for the counter variable, startvalue, stepvalue, and endvalue are shown within the hexagon

Figure 7-4: Pseudocode and flowchart for Example 1

Financial.Pmt method Calculates a periodic payment on a loan or investment

Returns the periodic payment as a Double type value

Rate and number of periods arguments must be expressed in the same units (monthly, annual, etc.)

Also calculates the amount that must be saved each period to accumulate a specific sum

Figure 7-5: How to use the Financial.Pmt method

Figure 7-5: Sample run of the Monthly Payment Calculator application

Figure 7-7: Two procedures in the Monthly Payment Calculator application

Figure 7-7: Two procedures in the Monthly Payment Calculator application (continued)

Windows standard: highlight the existing text when a text box receives the focus

SelectAll method: selects all text in a text box

Enter event: occurs when the text box receives the focus

Figure 7-8: How to select the existing text in a text box

Figure 7-9: The principalTextBox’s Enter event procedure

Figure 7-10: Result of processing the Enter event procedure

TextChanged event Occurs when a change is made in a control’s Text property

Change may be made by user or the program

Figure 7-11: New principal entered in the Principal text box

Figure 7-13: Status of the paymentsLabel after entering a value in the text box

Figure 7-12: The principalTextBox’s TextChanged event procedure

Figure 7-14: The termListBox’s SelectedValueChanged event procedure

Combo Box control: Similar to a list box with a list of choices

May contain a text field that allows the user to type an entry that is not on the list

List portion may be hidden

Three styles of combo boxes: Simple

DropDown (the default)

DropDownList

Figure 7-15: Examples of the combo box styles

Figure 7-15: Code used to fill the combo boxes with values

Items.Add method: used to add items to a combo box

SelectedItem property: contains the value of the selected item in the list

Text property: contains the value that appears in the text portion of the control (item selected or typed in)

Items.count property: used to obtain the number of items in the combo box

Sorted property: used to sort the items in the list

Figure 7-17: Sample run of the Monthly Payment Calculator application using a combo box

Figure 7-15a: Code for the Monthly Payment Calculator using a combo box

Figure 7-15b: Code for the Monthly Payment Calculator using a combo box

Most applications need to manipulate string data in some fashion String properties and methods are used to manipulate string data

Figure 7-19a: How to use the Length property

Figure 7-19b: How to use the Length property

TrimStart method: removes one or more characters from the beginning of a string

TrimEnd method: removes one or more characters from the end of a string

Trim method: removes one or more characters from both the beginning and end of a string

Each of these methods returns a string with the appropriate characters removed trimChars argument:

Comma-separated list of characters to be removed If omitted, spaces will be removed Default value is the space character: “ ”

Figure 7-20: How to use the TrimStart, TrimEnd, and Trim methods

Remove method: Removes characters from a string

Can remove one or more characters located anywhere in the string

Returns a string with the appropriate characters removed

startIndex argument: The position of a character in a string

Is zero-relative (starts with 0 as first position)

count argument: number of characters to remove

Figure 7-21: How to use the Remove method

Replace method: replaces a sequence of characters in a string with another sequence of characters

Arguments: string: the original String value

oldValue: sequence of characters you want to replace

newValue: replacement characters

Figure 7-22: How to use the Replace method

Mid statement: Replaces a specified number of characters in a string with characters from another string

Arguments: targetString: the string in which characters are to be replaced

replacementString: the replacement characters

start: the starting position for the replacement

count: number of characters to replace

Character position of characters in the string starts with 1 (not the same as index)

Count argument is optional

Figure 7-23: How to use the Mid statement

PadLeft method: inserts characters at the beginning of a string

PadRight method: inserts characters at the end of a string

Arguments: length: represents the total length of the desired resulting string

character: the character used to pad the string; default value is the space character

string: the string on which the operation takes place

Figure 7-24: How to use the PadLeft and PadRight methods

• Insert method: used to insert characters anywhere within a string

• Arguments:

– startIndex: specifies where in the string to insert the value

– string: the string being manipulated

– value: the character(s) to be inserted

Figure 7-25: How to use the Insert method

StartsWith method: determines whether a specific sequence of characters occurs at the beginning of a string

EndsWith method: determines whether a specific sequence of characters occurs at the end of a string

Arguments: subString: sequence of characters to be searched for

StartsWith and EndsWith methods both return Boolean values and perform a case-sensitive search

Figure 7-26: How to use the StartsWith and EndsWith methods

Figure 7-26: How to use the StartsWith and EndsWith methods (continued)

Contains method: Determines if a string contains a specific sequence of characters

Is a method of the String class

Returns a Boolean value

Performs a case-sensitive search

Arguments: subString: represents the sequence of characters to be searched for

string: the string in which to search

Figure 7-27: How to use the Contains method

IndexOf method: returns an integer representing the location of a substring within a string

Performs a case-sensitive search

Arguments: subString: sequence of characters to be searched for

string: the string to be searched

startIndex: the starting position for the search (zero-relative)

Figure 7-28: How to use the IndexOf method

Substring method: accesses any number of characters contained in a string

Arguments: startIndex: index of the first character to be accessed (zero-relative)

string: the string containing the characters to be accessed

count: number of characters to be accessed

Figure 7-29: How to use the Substring method

String.Compare method: compares two strings

Arguments: string1, string2: the two strings to be compared

ignoreCase: a Boolean value indicating whether to perform a case-sensitive (False) or case-insensitive (True) search

String.Compare returns an integer result: Returns 0 when string1 equals string2

Returns 1 when string1 > string2

Returns -1 when string1 < string2

String.Compare uses word sort rules: Numbers are less than lowercase letters

Lowercase letters are less than uppercase letters

Figure 7-30: How to use the String.Compare method

Like operator: Uses pattern-matching characters to determine if one string is equal to another

Returns a Boolean value (True/False)

Arguments: pattern: contains one or more pattern-matching characters

string: the string to be evaluated

charList: a listing of characters to be matched

Figure 7-31: How to use the Like operator

Figure 7-31: How to use the Like operator (continued)

Figure 6-29: User interface

• Creating the Roll ‘Em Game

• Sleep method: delays execution of the program

• Refresh method: redraws the user interface

• Grade Calculator

Figure 6-43: User interface

• Creating the Hangman Game

Figure 7-33: User interface

• Principal and Interest Calculator

Figure 7-46: User interface