62
Introduction to Computer Programming MELJUN CORTES MELJUN CORTES Visual Basic .Net Programming Using Visual Studio .Net

MELJUN CORTES VB.NET Technical faculty module part2 wsd

Embed Size (px)

DESCRIPTION

MELJUN CORTES VB.NET Technical faculty module part2 wsd

Citation preview

Page 1: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Introduction to Computer Programming

MELJUN CORTESMELJUN CORTES

Visual Basic .Net Programming Using Visual Studio .Net

Page 2: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Objectives

This module aims:• To introduce the software development process;• To know the different programming constructs of

Visual Basic .NET; and• To enhance the programming skills of the

participants.

Page 3: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Topics

• The software development process

• Different Control Structures in programming

• Visual Basic .NET examples

• Lab Exercise

Page 4: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

The Software Development Process

1. Problem Analysis

2. Setting up an algorithm

3. Implementing your algorithm

4. Running, Testing, & Debugging

5. Documentation

Page 5: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Problem!

Mr. De la Cruz needs a program to compute grades of 50 students. Grade computation is:

40% Average of 3 Exams

+ 40% Final Exam

20% Class participation

100%

Once grade of a student is computed the program also displays if student passed or failed. Passing grade is 70%.

Page 6: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

1. What are we asked to do?

Compute the final grade, and

determine if student is passing or not

1. Problem analysis

Page 7: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

2. What are the given data (inputs) and results that are to be produced (outputs)?

Input: Quiz No. 1 (Q1)

Quiz No. 2 (Q2) Quiz No. 3 (Q3) Final Exam (FE) Class Participation (CP)

Output: Final Grade (FG)

1. Problem analysis

Page 8: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

3. How do we accomplish our task?

FG = 40%

+ 30% (FE)

+ 15% (CP)

Q1 + Q2 + Q3

3

1. Problem analysis

Page 9: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Algorithm a list or sequence of steps that solves the

given problemNote: The steps should be as detailed as

possible.

2. Set-up Algorithm

Page 10: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Example:Step 1. Get Q1, Q2, and Q3 gradeStep 2. Get FE gradeStep 3. Get CP gradeStep 4. Calculate the FG using the formulaStep 5. Display the final grade FG

Step 6. Determine if student passed or failedStep 7. If students processed < 50,

go back to step 1 and process next student.

2. Set-up Algorithm

Page 11: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Refining the algorithm

Step 6. Determine if student passed or failed

Step 6. Determine if student passed or failed

Step 6.1. If FG < 70%,

display student failed

otherwise,

display student passed

Page 12: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

3. Implementing the algorithm

Basic Questions:1. How do you get input and display output?2. How do you store information?3. How do you write formula?4. How do you make a program perform statements

depending on different situations?5. How do you make a program repeat parts of itself?

Page 13: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Getting input and output

Using consoleImports System

Public Module Hello

Public Sub Main( )

Console.WriteLine("hello, world")

End Sub

End Module

Page 14: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Screen output

Page 15: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Getting input and output

Demo using GUI

Page 16: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

How do we store data?

Answer: Use variables

Declaring variables:

Dim variable As datatype

Rules:1. Must begin with a character or underscore2. Cannot have special characters3. Cannot be a reserved word

Page 17: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Basic data types in VB .NET

Numerical:

Integer (whole numbers)

Float (floating point)

Non-numerical:

Boolean (true or false values)

Char (a single character)

String (many characters)

Page 18: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Sample Declaration

Dim finalGrade As Float

Dim quiz1 As Integer

Dim name As String

Programming Tips…

1. Use descriptive variable names2. Don’t use names that differ by only one

characterex. Dim final As Float

Dim finals As Float3. Use names that are not too long and

not too short

Page 19: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Branching constructs

The If statement

Syntax 1: If expression Then

statements

End If

Page 20: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Example

For the problem of Mr. dela Cruz, to determine if a student passed or failed, we use:

If finalGrade >= 70 Then

Console.WriteLine(“Passed”)

End If

If finalGrade < 70 Then

Console.WriteLine(“Failed”)

End If

Page 21: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Branching constructs

The If statement

Syntax 2: If expression Then

statements

Else

statements

End If

Page 22: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Example

For the problem of Mr. dela Cruz, to determine if a student passed or failed, we use:

If finalGrade >= 70 Then

Console.WriteLine(“Passed”)

Else

Console.WriteLine(“Failed”)

End If

Page 23: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Branching constructs

The If statement

Syntax 3: If expression Thenstatements

ElseIf expression Thenstatements

ElseIf expression Thenstatements

Elsestatements

End If

Page 24: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Example

If finalGrade >= 90 Then

Console.WriteLine(“Very Good”)

ElseIf finalGrade >= 80 Then

Console.WriteLine(“Good”)

ElseIf finalGrade >= 70 Then

Console.WriteLine(“Satisfactory”)

Else

Console.WriteLine(“Needs Improvement”)

End If

Page 25: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Looping constructs

• Allow a group of statements to be executed more than once

• Looping statements in VB .NET:– Do– For– For Each

Page 26: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Looping constructs

The Do statement

Body of the loop is executed either:

1. while a condition remains true; or

2. until the condition becomes true

Page 27: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Looping constructs

The Do statement examples

Do While count < 50' statements

Loop

Do Until count > 50' statements

Loop

Do ' statementsLoop While count <= 50

Do ' statementsLoop Until count >= 50

Do ' statementsLoop

Page 28: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Looping constructs

The For statement

• Body of the loop is executed a fixed number of times

• Looping is controlled by a loop control variable

Page 29: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Looping constructs

Syntax of For statement:

For var = expr To expr [ Step expr ]

statements

Next [ variable_list ]

Page 30: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Looping constructs

Example of For statement:

For i = 1 To 10

Next

For i = 1 To 10

For j = 1 To 10

Next i, j

For i = 1 To 10

For j = 1 To 10

Next

Next

Page 31: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Different bugs or errors encountered:

• Syntactical errors are caused by deviating from grammatical rules of the language.

• Logical errors are caused due to faulty algorithm formulation

4. Run, Test, debug

Page 32: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Reasons for doing documentation:1. Ease of understanding different parts of the

code.

2. Reduces efforts during program maintenance and problem reconstruction.

Internal Documentation in VB .NET:‘ This is a comment

5. Documentation

Page 33: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

.NET Hands On Exercises

Page 34: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Exercises

• Lab1: Flash Cards

• Lab2: XChange

• Lab3: Student Picker

• Lab4: Mine Sweeper

Page 35: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Flash Cards

• Objectives– Design a graphical user interface.– Modify the properties of the user controls.– Create and call functions.– Perform input and output operations.– Create and evaluate statements and

expressions.– Implement and analyze program branching.

Page 36: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Flash Cards

• Features– Generates random

algebra problems.– Checks user input if

correct or incorrect.– Optional: scoring– Optional: hints

Page 37: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Flash Cards

• Creating the GUI– Drag and drop controls from the toolbox.– Use the properties window to modify the windows

controls.– Change the names of the controls.

Page 38: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Flash Cards

• Declaring Variables

Page 39: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Flash Cards

• Creating Functions

Page 40: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Flash Cards

• Generating the problem

Page 41: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Flash Cards

Page 42: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Flash Cards

Page 43: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Flash Cards

• Checking the answer

Page 44: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Flash Cards

• Giving hints

Page 45: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

XChange

• Objectives– Modify the properties of the user controls.– Create and call functions.– Convert data types.– Create and evaluate statements and

expressions.– Handle Windows control events.

Page 46: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

XChange

• Features– Converts one value to

another based on a given formula.

– Customizable conversion functions.

Page 47: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

XChange

• Synchronize TrackBar and Label

Page 48: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

XChange

• Convertion Functions

Page 49: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

XChange

• Convert Left Value

Page 50: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

XChange

• Convert Right Value

Page 51: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

XChange

• Dollar to Peso Conversion

Page 52: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

XChange

• Grade Equivalent of 70 pt Exam

Page 53: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

XChange

• Other Applications– Celsius to Fahrenheit– Pounds to Kilos– Inches to Centimeters– Etc.

Page 54: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Teaching Assistant

• Objectives– Call functions and pass parameters– Practice looping constructs and conditions– Work with list controls– Use the features of collections– Handle file input and output

Page 55: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Teaching Assistant

• Features– Class attendance

checker– Random student

selection– Load/Save class lists

Page 56: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Teaching Assistant

• Adding new students

Page 57: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Teaching Assistant

• Deleting students

Page 58: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Teaching Assistant

• Checking class attendance

Page 59: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Teaching Assistant

• Checking class attendance

Page 60: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Teaching Assistant

• Checking class attendance

Page 61: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Teaching Assistant

• Create a random function

Page 62: MELJUN CORTES VB.NET Technical faculty module   part2 wsd

Teaching Assistant

• Randomly select a present student