98-361 software development fundamentals - elk … ·  · 2016-06-0498-361 software development...

52
1.1 Understand Computer Storage and Data Types 1.2 Understand Computer Decision Structures 1.3 Identify the Appropriate Method for Handling Repetition 1.4 Understand Error Handling MTA Software Fundamentals 1 Test 98-361 Software Development Fundamentals LESSON 1

Upload: doandien

Post on 11-Apr-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

1.1 Understand Computer Storage and Data Types

1.2 Understand Computer Decision Structures

1.3 Identify the Appropriate Method for Handling Repetition

1.4 Understand Error Handling

MTA Software Fundamentals 1 Test

98-361 Software Development Fundamentals

L E S S O N 1

Understand Computer Storage and Data Types

98-361 Software Development Fundamentals

L E S S O N 1 . 1

98-361 Software Development Fundamentals

Lesson Overview

Students will understand computer storage and data types.

In this lesson, you will learn:

How a computer stores programs and instructions in computer memory

Memory stacks and heaps

Memory size requirements for the various data storage types

Numeric data and textual data

L E S S O N 1 . 1

98-361 Software Development Fundamentals

Review Terms

Data type—a definition of a set of data that specifies the possible range of values of the set, the operations that can be performed on the values, and the way in which the values are stored in memory.

Garbage collection—a process for automatic recovery of heap memory.

Heap—a portion of memory reserved for a program to use for the temporary storage of data structures whose existence or size cannot be determined until the program is running.

Memory —a device where information can be stored and retrieved.

Stack—a region of reserved memory in which programs store status data such as procedure and function call addresses, passed parameters, and sometimes local variables.

L E S S O N 1 . 1

98-361 Software Development Fundamentals

How a computer stores programs in memory

A computer keeps data and programs in storage as follows:

Primary storage—Otherwise known as random access memory (RAM), it is made of memory chips. In common usage, it refers only to a computer’s main memory, the fast semiconductor storage (RAM) directly connected to the processor.

Secondary storage—Otherwise known as a hard drive, it consists of a read/write head that floats above rotating platters coated with a magnetic material.

L E S S O N 1 . 1

98-361 Software Development Fundamentals

Memory–Stacks and Heaps

Variables are stored in either a stack or heap based on their type:

Value types (e.g.: int, double, float) go on the stack.

Reference types (String, Object) go on the heap.

* Value types in classes are stored with the instance of the class on the heap.

The stack

Values in the stack are managed without garbage collection because items are added and removed from the stack as last in, first out (LIFO) every time you enter or exit a scope, like a method or statement

A StackOverFlowException occurs because you have used up all the available space in the stack.

L E S S O N 1 . 1

98-361 Software Development Fundamentals

Memory–Stacks and Heaps (continued)

The heap

A heap-based memory allocation occurs when we create a new object, at which point the compiler figures out how much memory is needed and allocates an appropriate amount of memory space and returns a reference representing the memory address.

A heap is used for dynamic allocation of memory.

The Microsoft .NET Framework uses garbage collection to free up space during run time.

Garbage collection is an automatic process for recovery of heap memory. Blocks of memory that had been allocated but are no longer in use are freed, and blocks of memory still in use may be moved to consolidate the free memory into larger blocks.

L E S S O N 1 . 1

98-361 Software Development Fundamentals

Boolean Data Types

L E S S O N 1 . 1

Type Precision

boolean True or False

98-361 Software Development Fundamentals

Integral Types

Type Range Size

sbyte -128 to 127 Signed 8-bit integer

byte 0 to 255 Unsigned 8-bit integer

char U+0000 to U+ffff Unicode 16-bit character

short -32,768 to 32,767 Signed 16-bit integer

ushort 0 to 65,535 Unsigned 16-bit integer

int -2,147,483,648 to 2,147,483,647 Signed 32-bit integer

uint 0 to 4,294,967,295 Unsigned 32-bit integer

long-9,223,372,036,854,775,808 to

9,223,372,036,854,775,807Signed 64-bit integer

ulong 0 to 18,446,744,073,709,551,615 Unsigned 64-bit integer

L E S S O N 1 . 1

98-361 Software Development Fundamentals

Floating-Point Type

Type Approximate Range Precision

double ±5.0e−324 to ±1.7e308 15-16 digits

L E S S O N 1 . 1

98-361 Software Development Fundamentals

Decimal Type

Type Approximate Range Precision

decimal ±1.0 × 10−28 to ±7.9 × 1028 28-29 significant digits

L E S S O N 1 . 1

98-361 Software Development Fundamentals

Using the Data Types

Dim numKids As Byte = 15Dim Letter As Char = "p"Dim worldPopulation As Integer = 669203027Dim lotsaMoney As Double = 24.0000004585279Dim testGrade As Decimal = 89.5Dim Bool As Boolean = False

The Prefix (intNumber) is not required but recommended.

Capitalization in not required but recommended.

L E S S O N 1 . 1

98-361 Software Development Fundamentals

Student Lab 1.1

L E S S O N 1 . 1

98-361 Software Development Fundamentals

Understand Computer Decision Structures

98-361 Software Development Fundamentals

L E S S O N 1 . 2

98-361 Software Development Fundamentals

Lesson Overview

Students will understand the decision structures used by computers.

In this lesson, you will learn:

Various decision structures used in all computer programming languages

if decision structures

Multiple decision structures such as if…else and switch/SelectCase

Reading flowcharts

Decision tables

Evaluating expressions

L E S S O N 1 . 2

98-361 Software Development Fundamentals

Review Terms

Relational operator

Logical operator

and, or, not

Boolean

Expression

Condition

Value

if

else

switch

break

case

Algorithm

Compound statement

Nested statements

Precedence

L E S S O N 1 . 2

98-361 Software Development Fundamentals

Decisions in Real Life

A decision is a statement that says that if something is true, then something will happen as a consequence.

For example:

If I do the dishes, then I will get my allowance.

If Pat gets a 90 percent or above, he will get an A.

Decisions are modeled in computers.

For example:

If volume > 10 Then

radio.setVolume(2)

Else

radio.setVolume(1)

End If

L E S S O N 1 . 2

98-361 Software Development Fundamentals

Relational Expressions

A relational operator is used to compare two values, resulting in a relational expression.

For example:

number > 16grade = “F”passing >= 60

A list of relational operators:

< less than

> greater than

<= less than or equal to

>= greater than or equal to

= equal to

<> not equal to

L E S S O N 1 . 2

98-361 Software Development Fundamentals

if-else Statement:

If condition thenstatement1

Elsestatement2

End if

condition

evaluated

statement1

true false

statement2

L E S S O N 1 . 2

98-361 Software Development Fundamentals

if-else if

Statement:

If condition1 Thenstatement1

Else If condition2statement2

Elsestatement3

End if

condition1

evaluated

statement1

true false

condition2

evaluated

statement3

falsestatement2

true

L E S S O N 1 . 2

98-361 Software Development Fundamentals

Select Case Statement:

Select (caseSwitch)

Case 1

Console.WriteLine("Case 1")

Case 2

Console.WriteLine("Case 2")

End Select

L E S S O N 1 . 2

98-361 Software Development Fundamentals

Decision Table

A concise way of demonstrating logic

Creates an association between conditions and actions to execute

Rules

Conditions Controller does not respond Y N

Green light flashing N Y

Actions Replace batteries Y N

Synchronize with console N Y

L E S S O N 1 . 2

98-361 Software Development Fundamentals

Fix the syntax errors

boolean isFun = Fboolean isKind = Fboolean isPopular = TIf isFun isKind <> isPopular

Console.Writeln("You are my friend.")Else

Console.Writeln("Who are you?")

L E S S O N 1 . 2

98-361 Software Development Fundamentals

Fix the syntax errors—Answers

Dim isFun As Boolean = FalseDim isKind As Boolean = FalseDim isPopular As Boolean = TrueIf isFun And isKind <> isPopular Then

Console.WriteLine("You are my friend.")ElseConsole.WriteLine("Who are you?“)End If

L E S S O N 1 . 2

98-361 Software Development Fundamentals

Student Lab 1.2.1 and 1.2.2

L E S S O N 1 . 2

98-361 Software Development Fundamentals

Identify the Appropriate Method for Handling Repetition

98-361 Software Development Fundamentals

L E S S O N 1 . 3

98-361 Software Development Fundamentals

Lesson Overview

Students will identify the appropriate method for handling repetition.

In this lesson, you will learn:

for loops

while loops

do..while loops

Recursion

L E S S O N 1 . 3

98-361 Software Development Fundamentals

Review Terms

for loop

while loop

do-while loop

Iteration

Recursion

Base case

L E S S O N 1 . 3

98-361 Software Development Fundamentals

Iterations in Real Life

An iteration is the act of repeating a set of steps to perform a task.

For example:

Turn the screwdriver until the screw is tight.

Rub my hands under the air dryer until they are dry.

Iterations are modeled in computers.

L E S S O N 1 . 3

98-361 Software Development Fundamentals

The while Loop

Allows code to be repeated so long as a Boolean condition is met.

The condition is evaluated before the code is executed.

Can be used when the number of iterations is not known before executing the loop.

Dim intCount As Integer = 0

While intCount < 5

Console.WriteLine(intCount)

intCount += 1

End While

Initialization

Condition

Increment

L E S S O N 1 . 3

98-361 Software Development Fundamentals

The for Loop

Allows code to be repeated using a loop counter to control how many times it repeats.

The condition is evaluated before the code is executed.

Used when the number of iterations is known before executing the loop.

For intCount = 1 To 10 Step 1Console.WriteLine("I repeat ten times“)intCount += 1

Next

Initialization Condition Interval

L E S S O N 1 . 3

98-361 Software Development Fundamentals

The for Loop Flowchart

execute the

loop body

false

updatetruecondition

?

initialization

If the condition is false on loop

entry, the loop body is never

executed (not even once).

L E S S O N 1 . 3

98-361 Software Development Fundamentals

while Loop vs. for Loop

Dim strLine as String

while strLine = “”

Console.WriteLine(“Enter a word:”)

strLine = Console.ReadLine()

Dim intCount as Interger

for intCount = 0 to 5

Console.WriteLine(“Enter a word:”)

line = Console.ReadLine()

intCount += 1

L E S S O N 1 . 3

You do not know the

number of loops

You do know the number

of loops

98-361 Software Development Fundamentals

The do-while Loop

Allows code to be repeated so long as a Boolean condition is met.

The condition is evaluated after the code is already executed once.

Can be used when the number of iterations is not known before executing the loop, but it assumes at least one execution.

Dim strLine As String = ""

Do

Console.WriteLine("Enter a word:")

line = Console.ReadLine()

Loop While strLine <> ""

L E S S O N 1 . 3

98-361 Software Development Fundamentals

The do-until Loop

Allows code to be repeated until a Boolean condition is met.

The condition is evaluated after the code is already executed once.

Can be used when the number of iterations is not known before executing the loop, but it assumes at least one execution.

Dim strLine As String = ""

Do

Console.WriteLine("Enter a word:")

line = Console.ReadLine()

Loop Until strLine <> ""

L E S S O N 1 . 3

98-361 Software Development Fundamentals

while Loop vs. do-while Loop

A do-while loop will execute at least once.

A while loop might not execute at all.

Dim strLine as String

while strLine = ""

Console.WriteLine("Enter a word:")

strLine = Console.ReadLine()

Do

Console.WriteLine("Enter a word:")

line = Console.ReadLine()

Loop While strLine <> ""

L E S S O N 1 . 3

Will execute at least once

Might not execute at all

98-361 Software Development Fundamentals

Counting from 1 to 10 with Different Loops

Dim i as Interger = 1

for i to 10

Console.WriteLine(i)

i += 1

Next

While i <= 10

Console.WriteLine(i)

i += 1

End While

Do

Console.WriteLine(i)

i += 1

Loop While i <= 10

L E S S O N 1 . 3

98-361 Software Development Fundamentals

When do you use which loops?

When do you want to use while loops?

When you don’t know how many times you will iterate.

while loops incorporate a test (condition), so they may not even execute once.

When do you want to use for loops?

When you don’t know the specific number of iterations.

for loops are easier to check for correctness because they are more organized.

When do you want to use do-while loops?

When you know you should execute a statement at least once.

L E S S O N 1 . 3

98-361 Software Development Fundamentals

Recursion

Recursion occurs when a method calls itself to solve another version of the same problem.

With each recursive call, the problem becomes simpler and moves toward a base case.

The base case is reached when no other recursive call is required.

A base case is the point when no other recursive calls are made.

L E S S O N 1 . 3

98-361 Software Development Fundamentals

Factorials

Function factorial(ByVal n As Integer) As Integer

If n <= 1 Then

Return 1

Else

Return factorial(n - 1) * n

End If

End Function

fact (4) 4 * fact (3)

fact (3) 3 * fact (2)

fact (2) 2 * fact (1)

fact (1)

1

2

6

fact (4) 24

L E S S O N 1 . 3

98-361 Software Development Fundamentals

Function identity(ByVal num As Integer) As IntegerIf (num < 1) Then

Return 10Else

Return num + identity(num - 2)End If

End Function

identity(num) Suspended actions Popping back up the stack

identity (6) 6 + identity(4) 22

identity (4) 4 + identity(2) 16

identity (2) 2 + identity(0) 12

identity (0) 10

Base case

Movement

towards base

case

Recursive call

L E S S O N 1 . 3

98-361 Software Development Fundamentals

Student Labs 1.3.1 and 1.3.2

L E S S O N 1 . 3

98-361 Software Development Fundamentals

Understand Error Handling

98-361 Software Development Fundamentals

L E S S O N 1 . 4

98-361 Software Development Fundamentals

Lesson Overview

Students will understand error handling.

In this lesson, you will learn:

Structured exception handling using the try, catch, finally,and throw keywords

L E S S O N 1 . 4

98-361 Software Development Fundamentals

Review Term

Exception

A problem or change in conditions that causes the microprocessor to stop what it is doing and handle the situation in a separate routine.

An exception is similar to an interrupt; both refer the microprocessor to a separate set of instructions.

L E S S O N 1 . 4

98-361 Software Development Fundamentals

How to Handle Exceptions

A try block is used by VB programmers to partition code that may be affected by an exception.

A catch block is used to handle any resulting exceptions.

A finally block can be used to execute code regardless of whether an exception is thrown—which is sometimes necessary, as code following a try/catch construct will not be executed if an exception is thrown.

A try block must be used with either a catch or a finallyblock, and it can include multiple catch blocks.

L E S S O N 1 . 4

98-361 Software Development Fundamentals

Example 1: try and catchDim SafeDivision as Integer(x, y)

Private Function Test(ByRef x As Integer, ByRef y AsInteger)Try

return (x / y)Catch ex As Exception

Messagebox.Show("Division by zero attempted!")Return 0

End TryEnd Function

L E S S O N 1 . 4

98-361 Software Development Fundamentals

Example 1: try and catch

When your application encounters an exceptional circumstance, such as a division by zero or a low memory warning, an exception is generated.

Use a try block around the statements that might throw exceptions.

Once an exception occurs within the try block, the flow of control immediately jumps to an associated exception handler, if one is present.

If no exception handler for a given exception is present, the exception is passed up the chain to the calling routine.

L E S S O N 1 . 4

98-361 Software Development Fundamentals

Example 2: try, catch, and finally

Private Sub Main()

Try

'statement which can cause exception

Catch ex As Exception

'statement for handling exception

Finally

'cleanup code

End Try

End Sub

L E S S O N 1 . 4

98-361 Software Development Fundamentals

Example 2: try, catch, and finally

A common usage of catch and finally together is to obtain and use resources in a try block, deal with exceptional circumstances in a catch block, and release the resources in the finally block.

Code in a finally block is executed even if an exception is thrown, thus allowing a program to release resources.

If no exception occurred inside the try block, the control directly transfers to the finally block.

L E S S O N 1 . 4

98-361 Software Development Fundamentals

Student Lab 1.4

L E S S O N 1 . 4

98-361 Software Development Fundamentals

Complete the QUIA Test

MTA Software Fundamentals 1 Test

98-361 Software Development Fundamentals

L E S S O N 1