programming
@Saint ….Blessing Nyandoro
Part 1 free treat
Requirements of the syllabus
 DESCRIBE THE FEATURES OF HIGH LEVEL LANGUAGE
 LANGUAGE FEATURES OF PROGRAMMING
 PROGRAMMING CONSTRUCTS
 MANIPULATE AN ARRAY
 READ/WRITE TO FILE
 DESIGN INPUT OR OUTPUT INTERFACE
 FEATURES OF OOP (OBJECT ORIENTED PROGRAMMING)
 DESIGN GAMES OR MOBILE APPLICATIONS
TYPES OF HIGH LEVEL LANGUAGES
 GENERAL PURPOSE
 SPECIAL PURPOSE
 OBJECT ORIENTED PROGRAMMING
 DECLARATIVE
 IMPERATIVE / PROCEDURAL
OBJECT ORIENTED PROGRAMMING
 These languages use object-oriented programming paradigms as the design
approach for solving a given problem. In this programming language, a problem is
divided into a number of objects which can interact by passing messages to each
other. C++ and C# are the examples of object-oriented programming languages
 Class
 Instance
 Object
 Abstraction
 Encalpsulation
 Inheritance
 Polymprphism
FEATURES
 CONSTANTS – RESERVED STORAGE
 VARIABLES (LOCAL AND GLOBAL VARIABLES)
 EXPRESSIONS
 STATEMENTS
 CONTROL STRUCTURE
 BLOCK STRUCTURE
PROGRAMMING
QUESTIONS
Looping structures
 Question: find the average of ten numbers
FUNTIONS
 The Function procedure performs a task and then returns control to the calling code. When it returns
control, it also returns a value to the calling code.
 Each time the procedure is called, its statements run.
 [Modifiers] Function FunctionName [(ParameterList)] As ReturnType
 [Statements]
 End Function
 Example:
 Public Function FindSqrt(radicand As Single) As Single
 End Function
Calling the function
 lvalue = functionname [( parameterlist )]
 CREATING A FUNCTION HYPOTENUSE
 Function Hypotenuse(side1 As Double, side2 As Double) As Double
 Return Math.Sqrt((side1 ^ 2) + (side2 ^ 2))
 End Function
 CALLING THE FUNCTION HYPOTENUSE
 Private Sub
 Dim testLength, testHypotenuse As Double
 testHypotenuse = Hypotenuse(4, 10.7)
inputbox
 Syntax::
 InputBox(prompt[,title][,default][,xpos][,ypos][,helpfile,context])
 InputBox("What is your message?", "Message Entry Form", "Enter your messge here", 500, 700)
 Output:
QUESTION INVOLVING INPUTBOX AND
FUNCTION
 Write a program to grade marks for 12 courses for a college student. Use any
selection statement of your choice. The grades are as follows

 >= 75 Distinction
 60-74 Merit
 50-59 Pass
 40-49 Supplementary
 <=39 Fail
[12]
PROPERTIES
Control Property
InputBox "Enter mark for Course" & i
Label Name
Text
LblOutput
Answer
Button Name
Text
BtnEnter
Enter Marks
INPUT DESIGN
CODE
 Public Class Form2

 Private Sub BtnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles BtnEnter.Click
 Dim output As String = ""

 ‘Loop for entering marks for 12 courses
 For i = 1 To 12
 Dim c As String = InputBox("Enter mark for Course" & i)
 output += "Course" & i & " " & c & " " & Grade(c) + vbNewLine
 Next
 LblOutput.Text = output
 End Sub

 ‘function for calculating grades and outputting grade
 Private Function Grade(ByVal mark As Integer)
 Dim grd As String = ""

If mark > 100 Then
grd = "invalïd mark"
ElseIf (mark >= 75) Then
grd = "Distinction"
ElseIf mark >= 60 Then
grd = "Merit"
ElseIf mark >= 50 Then
grd = "Pass"
ElseIf mark >= 40 Then
grd = "Supplementary"
ElseIf mark >= 0 Then
grd = "Fail"
Else
grd = "invalïd mark"
End If
Return grd
End Function
End Class
OUTPUT
Message box
QUESTION involving a message box
b A person invests $1000.00 in a savings account that yields 5% interest. Assuming that all interest is left on deposit, calculate and print the
amount of money in the account at the end of each year over a period of 10 years. To determine these amounts, use the following formula:
a = p (1 + r) n
where
p is the original amount invested (i.e., the principal)
r is the annual interest rate (e.g., .05 stands for 5%)
n is the number of years
a is the amount on deposit at the end of the nth year.
The interface is as below
[20]
INPUT DESIGN
CODE
OUTPUT
Palindrome test code
 Interface
 CODE:
OUTPUTS
FILE HANDLING
Text files created
 Text Files created
 Filee 1(writing file)
 File 2(read file)
 File 1:
output
 Output:
Result in file 2:
QUESTIONS TO DISCUSS
Csv files
 When reading data from a file, the easiest file type to use is a CSV file, this
stands for comma separated values.
 Each piece of data is separated by a comma. This means that when you split
the information up you can say to the program split the information into the
list every time you find a comma.
 This means you can then refer to elements of the list to extract single pieces
of information. Imagine the array you have is called games, the second image
above shows how you would reference the information. If you wanted the
name of the game it would be games(1), if you wanted to the rating it would
be games(3)
Example:
THE QUESTION REQUIRES US TO CREATE A
CSV FILE WITH CARS AND THEIR SPEEDS AND
WITH A SPEED LIMIT ENTERED THE CARS
OVERSPEEDING WILL BE DETERMINED AND
OUTPUT ON A WINDOWS APPLICATION FORM
DESIGNING THE
CINTROLS
 COMBOBOX: CmbLimit
 LIST BOX: lstOutput
 BUTTON: BtnCheck
INTERFACE
Code
Output:
Csv file created
DESIGN GAMES OR MOBILE
APPLICATIONS
TOWER OF HANOI
MANIPULATING AN ARRAY
• FIRST WE POPULATE THE ARRAY
• THE WE SORT THE ARRAY
 BUBBLE SORT
 QUICK SORT
• SEARCH FOR AN ITEM
 LENEAR SEARCH
 BINARY SEARCH
POPULATING AN AREA
 An array in Visual Basic is created in the following way:
 Dim sentence As Array = {"The","quick","grey","fox","jumps"}
 This example creates an array called sentence that has 5 elements to it.
 If you wanted to display the word grey in a message box you would type:
 MessageBox.Show(sentence(2))
If you wanted to output the entire contents of the array, you could use a list box and use the
following code:
Dim sentence As Array = {"The","quick","grey","fox","jumps"}
lstOutput.Items.Add(sentence(0))
lstOutput.Items.Add(sentence(1))
lstOutput.Items.Add(sentence(2))
lstOutput.Items.Add(sentence(3))
lstOutput.Items.Add(sentence(4))
You could also use a loop to do the same thing:
Dim sentence As Array = {"The","quick","grey","fox","jumps"}
For x = 0 to sentence.length-1
 lstOutput.Items.Add(sentence(x))
Next

Fundamentals of Programming in Visual basic

  • 1.
  • 2.
    Requirements of thesyllabus  DESCRIBE THE FEATURES OF HIGH LEVEL LANGUAGE  LANGUAGE FEATURES OF PROGRAMMING  PROGRAMMING CONSTRUCTS  MANIPULATE AN ARRAY  READ/WRITE TO FILE  DESIGN INPUT OR OUTPUT INTERFACE  FEATURES OF OOP (OBJECT ORIENTED PROGRAMMING)  DESIGN GAMES OR MOBILE APPLICATIONS
  • 3.
    TYPES OF HIGHLEVEL LANGUAGES  GENERAL PURPOSE  SPECIAL PURPOSE  OBJECT ORIENTED PROGRAMMING  DECLARATIVE  IMPERATIVE / PROCEDURAL
  • 5.
    OBJECT ORIENTED PROGRAMMING These languages use object-oriented programming paradigms as the design approach for solving a given problem. In this programming language, a problem is divided into a number of objects which can interact by passing messages to each other. C++ and C# are the examples of object-oriented programming languages  Class  Instance  Object  Abstraction  Encalpsulation  Inheritance  Polymprphism
  • 6.
    FEATURES  CONSTANTS –RESERVED STORAGE  VARIABLES (LOCAL AND GLOBAL VARIABLES)  EXPRESSIONS  STATEMENTS  CONTROL STRUCTURE  BLOCK STRUCTURE
  • 7.
  • 8.
    Looping structures  Question:find the average of ten numbers
  • 10.
    FUNTIONS  The Functionprocedure performs a task and then returns control to the calling code. When it returns control, it also returns a value to the calling code.  Each time the procedure is called, its statements run.  [Modifiers] Function FunctionName [(ParameterList)] As ReturnType  [Statements]  End Function  Example:  Public Function FindSqrt(radicand As Single) As Single  End Function
  • 11.
    Calling the function lvalue = functionname [( parameterlist )]  CREATING A FUNCTION HYPOTENUSE  Function Hypotenuse(side1 As Double, side2 As Double) As Double  Return Math.Sqrt((side1 ^ 2) + (side2 ^ 2))  End Function  CALLING THE FUNCTION HYPOTENUSE  Private Sub  Dim testLength, testHypotenuse As Double  testHypotenuse = Hypotenuse(4, 10.7)
  • 12.
    inputbox  Syntax::  InputBox(prompt[,title][,default][,xpos][,ypos][,helpfile,context]) InputBox("What is your message?", "Message Entry Form", "Enter your messge here", 500, 700)  Output:
  • 13.
    QUESTION INVOLVING INPUTBOXAND FUNCTION  Write a program to grade marks for 12 courses for a college student. Use any selection statement of your choice. The grades are as follows   >= 75 Distinction  60-74 Merit  50-59 Pass  40-49 Supplementary  <=39 Fail [12]
  • 14.
    PROPERTIES Control Property InputBox "Entermark for Course" & i Label Name Text LblOutput Answer Button Name Text BtnEnter Enter Marks
  • 15.
  • 16.
    CODE  Public ClassForm2   Private Sub BtnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEnter.Click  Dim output As String = ""   ‘Loop for entering marks for 12 courses  For i = 1 To 12  Dim c As String = InputBox("Enter mark for Course" & i)  output += "Course" & i & " " & c & " " & Grade(c) + vbNewLine  Next  LblOutput.Text = output  End Sub   ‘function for calculating grades and outputting grade  Private Function Grade(ByVal mark As Integer)  Dim grd As String = "" 
  • 17.
    If mark >100 Then grd = "invalïd mark" ElseIf (mark >= 75) Then grd = "Distinction" ElseIf mark >= 60 Then grd = "Merit" ElseIf mark >= 50 Then grd = "Pass" ElseIf mark >= 40 Then grd = "Supplementary" ElseIf mark >= 0 Then grd = "Fail" Else grd = "invalïd mark" End If Return grd End Function End Class
  • 18.
  • 19.
  • 20.
    QUESTION involving amessage box b A person invests $1000.00 in a savings account that yields 5% interest. Assuming that all interest is left on deposit, calculate and print the amount of money in the account at the end of each year over a period of 10 years. To determine these amounts, use the following formula: a = p (1 + r) n where p is the original amount invested (i.e., the principal) r is the annual interest rate (e.g., .05 stands for 5%) n is the number of years a is the amount on deposit at the end of the nth year. The interface is as below [20]
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 28.
  • 30.
    Text files created Text Files created  Filee 1(writing file)  File 2(read file)  File 1:
  • 32.
  • 33.
  • 34.
    Csv files  Whenreading data from a file, the easiest file type to use is a CSV file, this stands for comma separated values.  Each piece of data is separated by a comma. This means that when you split the information up you can say to the program split the information into the list every time you find a comma.  This means you can then refer to elements of the list to extract single pieces of information. Imagine the array you have is called games, the second image above shows how you would reference the information. If you wanted the name of the game it would be games(1), if you wanted to the rating it would be games(3)
  • 35.
  • 36.
    THE QUESTION REQUIRESUS TO CREATE A CSV FILE WITH CARS AND THEIR SPEEDS AND WITH A SPEED LIMIT ENTERED THE CARS OVERSPEEDING WILL BE DETERMINED AND OUTPUT ON A WINDOWS APPLICATION FORM
  • 37.
    DESIGNING THE CINTROLS  COMBOBOX:CmbLimit  LIST BOX: lstOutput  BUTTON: BtnCheck
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
    DESIGN GAMES ORMOBILE APPLICATIONS TOWER OF HANOI
  • 44.
    MANIPULATING AN ARRAY •FIRST WE POPULATE THE ARRAY • THE WE SORT THE ARRAY  BUBBLE SORT  QUICK SORT • SEARCH FOR AN ITEM  LENEAR SEARCH  BINARY SEARCH
  • 45.
    POPULATING AN AREA An array in Visual Basic is created in the following way:  Dim sentence As Array = {"The","quick","grey","fox","jumps"}  This example creates an array called sentence that has 5 elements to it.  If you wanted to display the word grey in a message box you would type:  MessageBox.Show(sentence(2))
  • 46.
    If you wantedto output the entire contents of the array, you could use a list box and use the following code: Dim sentence As Array = {"The","quick","grey","fox","jumps"} lstOutput.Items.Add(sentence(0)) lstOutput.Items.Add(sentence(1)) lstOutput.Items.Add(sentence(2)) lstOutput.Items.Add(sentence(3)) lstOutput.Items.Add(sentence(4)) You could also use a loop to do the same thing: Dim sentence As Array = {"The","quick","grey","fox","jumps"} For x = 0 to sentence.length-1  lstOutput.Items.Add(sentence(x)) Next