variables, constants, methods, and calculations chapter 3 - review

Post on 25-Dec-2015

223 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Variables, Constants, Methods, and Calculations

Chapter 3 - Review

Variables

Creating a space in memory to hold data to be utilized while the application is running.

Every variable has a:NameData typeScopeLifetime

Selecting a Data Type for a Variable

Each variable must be assigned a data type

Data type: the type of data the variable can store

Each data type is a class

Selecting a Name for a Variable

Identifier: descriptive name given to a variable

Use a meaningful name that reflects the purpose of the variable

Use Hungarian Notation (intNumber, strName)

Variable names must conform to naming rules (no spaces, no reserved words, start with characters, no punctuation except the underscore _)

Declaring a Variable

Declaration statement: used to declare, or create, a variable

Declaration statement includesScope keyword: Dim or Private or StaticName of the variableData type Initial value (optional)

Assigning Data to an Existing Variable

Assignment statement:Used to assign values to properties of

controlsUsed to assign values to variables

Assignment operator: (=)Value on the right of the = operator is

assigned to the variable on the left of the = operator

Option Explicit ON

Option Explicit ON = must declare (create) the variable before using

Protects against misspelled variable names in code

Placed in the General Declarations section of code editor

Option Explicit ON

Data Typing

Implicit type conversion: can occur if the value on the right side of an assignment statement is not the same data type as the variable on the left side

Example:

25.65 converted to Integer = 26

25.45 converted to Integer = 25

Data Typing

Promoting: when a value is converted to another data type that stores larger numbers

Demoting: when a value is converted to another data type that stores only smaller numbersData loss can occur with demoting

Option Strict ON

Option Strict ON = prevents possible loss of data by not allowing any implicit type conversionsCan be used to enforce correct data typing

Placed in the General Declarations section of the code editor

General Declaration

Program Design

TOE ChartsPseudocodeFlowcharts

All are visual tools to help design the logical flow of the program.

TOE Charts

Task

Object

Event

TOE Charts

TOE Charts help the programmer to think through what the application will do and what it will need to run properly.

1. Input

2. Calculate

3. Print out to screen or paper

4. Clear used data from controls

5. Exit program

TOE Chart – Task

1 – Input

2 – Calculate

3 – Print to screen or paper

4 – Exit program

5 – Clear form

TOE Charts

TOE Charts provide a visual tool to identify the tasks of the application, the objects used in the user interface and the events (clicking, double-clicking) that

trigger the code.TOE Charts identifies the tasks, objects

and events of the FORM.

Perry Primary SchoolMainForm Task Object Event

1.Calculate the sum of and the difference between the two numbers 2.Display the sum and difference in the sumLabel and differenceLabel controls

computeButton Click

End the application exitButton Click

Display the sum and difference (from computeButton)

sumLabel, differenceLabel

None

Get and display two numbers firstTextBox, secondTextBox

None

Display the DialogForm optionsButton Click

Display the SplashScreenForm MainForm LoadDialogForm Task Object Event

Change the color of the MainForm’s text to red

redButton Click

Change the color of the MainForm’s text to black

blackButton Click

SplashScreenForm Task Object Event

Close the SplashScreenForm splashTimer Tick

Pseudocode

Pseudocode identifies the steps of a procedure and writes it in short phases.

Each procedure will have it’s own pseudocode.

Skate-Away Pseudocode

Flowcharts

Flowcharts identifies the steps of a procedure and diagrams it using special symbols.

Flowcharts

Oval – start / stop symbol

Rectangles – process symbols• represents tasks such as making calculations

Parallelogram – input / output symbol• represent input tasks – getting information from user • represent output tasks – displaying information

Lines – flowlines symbol• represents logical flow• connects the symbols

Flowcharts

Flowcharts identifies the steps of a procedure and diagrams it using special symbols.

Each procedure will have it’s own flowchart.

General Design Guide

1. Input

2. Calculate

3. Output (print to screen and/or paper)

4. Clear old data

5. End program

Code Flow

Step – by – step

General Design Guide

1. Input

2. Calculate

3. Output (print to screen and/or paper)

4. Clear old data

5. End program

General Design Guide

1. Input Validate

2. Calculate

3. Output (print to screen and/or paper)

4. Clear old data

5. End program

General Design Guide

1. Input Validate Convert

2. Calculate

3. Output (print to screen and/or paper)

4. Clear old data

5. End program

Using the TryParse MethodMethod: a specific portion of a class’s

instructions that performs a task for the class

TryParse method: Part of every numeric data type’s classUsed to convert a string to that numeric data

type

Using the TryParse MethodTryParse method has 4 arguments

String: string value to be convertedVariable: location to store the result IFormatProvider (optional): specifies

formattingNumberStyles (optional): allows formatting

characters to be in the data to be converted

Using the TryParse MethodMethod: a specific portion of a class’s

instructions that performs a task for the class

TryParse method: Part of every numeric data type’s classUsed to convert a string to that numeric data

type

Using the TryParse MethodTryParse method has 4 arguments

String: string value to be convertedVariable: location to store the result IFormatProvider (optional): specifies

formattingNumberStyles (optional): allows formatting

characters to be in the data to be converted

General Design Guide

1. Input Validate Convert

2. Calculate

3. Output (print to screen and/or paper)

4. Clear old data

5. End program

General Design Guide

1. Input

2. Calculate

3. Output (print to screen and/or paper)

4. Clear old data

5. End program

General Design Guide

Calculate RULE 1: There is an order in which the

computer performs the operation in an expression

RULE 2: All variables must be of the same data type to be able to do mathematical calculations

Arithmetic Operators – Precedence Order

General Design Guide

Calculate RULE 1: There is an order in which the

computer performs the operation in an expression

RULE 2: All variables must be of the same data type to be able to do mathematical calculations

General Design Guide

Calculate RULE 1: There is an order in which the

computer performs the operation in an expression

RULE 2: All variables must be of the same data type to be able to do mathematical calculations

When possible, have the TryParse Method convert the user input (String Data Type) to the numeric data type needed for the calculation.

General Design Guide

1. Input

2. Calculate

3. Output (print to screen and/or paper)

4. Clear old data

5. End program

General Design Guide

1. Input

2. Calculate

3. Output (print to screen and/or paper)

4. Clear old data

5. End program

General Design Guide

1. Input

2. Calculate

3. Output (print to screen and/or paper) Convert data type to STRING Format output

4. Clear old data

5. End program

General Design Guide

Output / Display RULE: Output to be displayed on the

screen must be of the data type STRING.

General Design Guide

Output / Display RULE: Output to be displayed on the

screen must be of the data type STRING. RULE: Values (data) assigned to the Text

Property must be of the data type STRING.

General Design Guide

Output / Display RULE: Output to be displayed on the

screen must be of the data type STRING. RULE: Values (data) assigned to the Text

Property must be of the data type STRING.

Provide proper formatting of the output as required.

Formatting Numeric Output

Formatting: specifying the number of decimal places and any special characters to display

Format specifier: specifies the type of formatting to use (“C”) (“N”) (“D”) (“P”)

Precision specifier: controls the number of significant digits or zeros to the right of the decimal point (“C2”)(“N1”)

Format Specifier

Format Specifier and Precision Specifier

Formatting Numeric Output (continued)

Formatting Numeric Output (continued)

top related