Copyright © 2008 Department of Education - Introduction to Visual Basic – VB.Net Page 75
Food for thought: Exercise 4 1. What is the difference between a class and an object? Why do we make classes?
2. How are fields and properties different from each other? 3. Isnt it better to make a field Public than providing its property with both Set...End Set
and Get...End Get blocks? After all, the property will allow the user to both read and modify the field, so why not use a Public field instead? Motivate your answer
4. Write a class to simulate a Circle Shape. Each circle has its radius and center point
• a Provide 2 constructors: a parameterless and one which takes a center point and a radius as parameters.
• b Provide a mechanism to find the diameter twice the radius and the area PI square of radius.
• c Provide a mechanism to move a circle from its starting location to somewhere else.
• d Apply proper access modifiers and use proper fields, method and properties 5. Write a program containing a method that will swap interchange the value of two
integer type variables. 6. Can we use Me in a Shared context? Why or Why not? Can Me ever be equal to Nothing?
Solution of Last Issues Exercise Exercise 3 1. How was we able to call the Parse method on an Integer. The Integer is a data
type and not a class? All the primitive data types in VB.Net or any other .Net compliant language are mapped to
types defined in the CTS Common Type System. These CTS types are implemented as Structures something like classes, we will see the details of structures in coming lessons
and contain different useful methods and properties. An Integer in VB.Net is mapped to the CTS type Int32, which contains a method called Parse. When we tried to call the Parse
method on an Integer, the CLR automatically boxed it into a Int32 and called the Parse method. We will see boxing and un-boxing in the next lesson Lesson 5.
2. Write a program that asks a user for 5 numbers and then print the average of these numbers.
Imports System Program that input and calculate the average of 5 numbers
Module NumberAverage Sub Main
Dim numbers4 As Integer Dim sum As Integer = 0
Dim average As Double = 0 Dim i As Integer
For i = 0 To numbers.Length - 1 Console.WritePlz, write the {0}th number: , i + 1
numbersi = Integer.ParseConsole.ReadLine sum += numbersi
Next average = sum numbers.Length
Console.WriteLinevbCrLf The average of given {0} numbers is: {1},
Copyright © 2008 Department of Education - Introduction to Visual Basic – VB.Net Page 76
numbers.Length, average End Sub
End Module
3. Write a program that asks for 5 names. Find and print the name which has most number of characters in it.
Imports System Program that input and find the biggest of the 5 names
Module BiggestName Sub Main
Dim names2 As String Dim biggestLength As Integer = 0
Dim biggestName As String = Dim i As Integer
For i = 0 To names.Length - 1 Console.WritePlz, write the {0}th name: , i + 1
namesi = Console.ReadLine If namesi.Length biggestLength Then
biggestName = namesi biggestLength = namesi.Length
End If Next
Console.WriteLinevbCrLf The biggest of the given {0} names is: {1} with {2} characters., names.Length, biggestName, biggestLength
End Sub End Module
4. Write a program that processes the following string:
Dim sentence As String = Learning VB.Net is extremely easy fun, _ specially at Programmers Heaven VB.Net has a data type Byte, _
which occupies 8 bits and can store integers values from -128 to 127.
Iterate through the string and find the number of letters, digits and punctuation characters in it and print these to the console.
Imports System Program that finds the number of letters, digits
and punctuation characters in string Module DigitLetter
Sub Main Dim sentence As String = Learning VB.Net is extremely easy fun, _
specially at Programmers Heaven VB.Net has a data type Byte, _ which occupies 8 bits and can store integers values from -128 to 127.
Dim letters As Integer = 0 Dim digits As Integer = 0
Dim punctuators As Integer = 0 Dim ch As Char
For Each ch In sentence If Char.IsLetterch Then
letters += 1
Copyright © 2008 Department of Education - Introduction to Visual Basic – VB.Net Page 77
ElseIf Char.IsDigitch Then digits += 1
ElseIf Char.IsPunctuationch Then punctuators += 1
End If Next
Console.WriteLineInt the sentence: vbCrLf sentence vbCrLf Console.WriteLineThe number of charachters = {0}, sentence.Length
Console.WriteLineThe number of letters = {0}, letters Console.WriteLineThe number of digits = {0}, digits
Console.WriteLineThe number of punctuators = {0}, punctuators End Sub
End Module
5. Write a program that prints all the command line arguments.