CS 9608 VB Console Student Booklet
CS 9608 VB Console Student Booklet
CS 9608 VB Console Student Booklet
Net Console
Student Booklet
by Doug Semple
Tasks and sample programs to teach the AQA GCE Computing COMP1 syllabus.
AQA COMP1
VB Console
Contents
Download and Setup............................................................................................................................... 3
Your first program ................................................................................................................................... 3
Example Program 1 Sequence ............................................................................................................. 4
Example Program 2 - Assignment ........................................................................................................... 5
Example Program 3 - Arithmetic ............................................................................................................. 6
Example Program 4 Selection .............................................................................................................. 7
Example Program 5 - Relational operators ............................................................................................. 8
Example Program 6 - Boolean operators ................................................................................................ 9
Moderation Exercise 1 Central Heating ............................................................................................. 10
Example Program 7 - Logical bitwise operators.................................................................................... 11
Example Program 8 - Built-in functions ................................................................................................ 12
Example Program 9 ............................................................................................................................... 13
Example Program 10 ............................................................................................................................. 14
Example Program 11 - Repetition ......................................................................................................... 15
Section 12 - Flowcharts ......................................................................................................................... 16
Example Program 13 Procedures & Functions................................................................................... 18
Section 14 By Ref vs. By Val ............................................................................................................... 19
Moderation Exercise 2 Car Hire ......................................................................................................... 20
Example Program 15 - Constants .......................................................................................................... 21
Example Program 16 - Data Structures ................................................................................................. 22
Other Built-in Data Types...................................................................................................................... 23
Byte ............................................................................................................................................... 23
Enumerated .................................................................................................................................. 23
Records.......................................................................................................................................... 24
Example Program 17 1 dimensional arrays........................................................................................ 26
Example Program 18a Read from a text file ...................................................................................... 27
Example Program 18b Write to a text file ......................................................................................... 28
Example Program 19 ............................................................................................................................. 29
Example Program 20 - Validation ......................................................................................................... 30
Example Program 21 2 dimensional arrays........................................................................................ 31
Example Program 22 Enumerated ..................................................................................................... 32
Section 23 - Set operators..................................................................................................................... 33
Union................................................................................................................................................. 33
Doug Semple 2013
AQA COMP1
VB Console
Range ................................................................................................................................................ 33
Intersection ....................................................................................................................................... 33
Difference (Distinct) .......................................................................................................................... 34
Membership (Contains) .................................................................................................................... 36
Example Program 24 - Linear search .................................................................................................... 37
Example Program 25 - Bubble sort ....................................................................................................... 38
Extension Set A: Form Applications ...................................................................................................... 39
Extension Set B: COMP1 Revision ......................................................................................................... 40
AQA COMP1
VB Console
Tasks
0.1 Write a program that displays the message Hello Dave
0.2 Write a program that displays the message My name is Dave and I live in Brussels (replace
Dave and Brussels with your own information)
0.3 Write a program that displays the lyrics to your national anthem. Each line of the song should
be on a fresh line.
AQA COMP1
VB Console
Tasks
1.1) Display the words red, amber, green on a fresh line each in order on the screen. Colour the
words appropriately.
1.2) Displaythe names of the colours of the rainbow. Each on a separate line, coloured and in order.
AQA COMP1
VB Console
Example Program 2 - Assignment - Integer, byte, real, boolean, character, string, date/time.
Module Module1
Sub Main()
Dim theNumber As Integer
Dim theWord As String
theWord = "Bird"
theNumber = 9
Console.WriteLine(theWord & " is the word")
Console.WriteLine("And the number is " & theNumber)
Console.ReadKey()
theWord = "Cat"
Console.WriteLine("Enter a number>")
theNumber = Int(Console.ReadLine())
Console.WriteLine("Now " & theWord & " is the word and the number is " &
theNumber)
Console.ReadKey()
End Sub
End Module
AQA COMP1
VB Console
Tasks
3.1) Write a program that divides a number entered by the user by 2
3.2) Write a program that displays the 7 times table
3.3) Write a program that displays any times table the user requests
AQA COMP1
VB Console
1. Write a program to ask the user what 24+9 is. Say Excellent if they get it right.
2. Write a program to ask the user how many in a bakers dozen? and say most
excellent if they get it right.
3. Write a program to ask the user to enter their age. If their age is under 18 then say
Sorry, this film is not for you.
4. Write a program to ask the user for two numbers. Compare the first with the second
and then print out one of three messages. Either the numbers are equal, the first is
bigger, or the second is bigger. You will need more than one IF to solve this one.
5. Write a program which asks the user to enter their password. If they enter the word
PASSWORD then display the message Welcome to the treasure, otherwise
display a message which says go away, its all mine.
6. Write a program which asks the user to enter a number between 1 and 10. If the
number entered is out with this range then display a message Sorryout of range.
AQA COMP1
VB Console
AQA COMP1
VB Console
AQA COMP1
VB Console
You may practice this task at home, but wont be able to refer to any notes or files when assessed.
Submit the following for marking in 1 word document:1. Your code listing
2. Screenshots for suitable test data
10
AQA COMP1
VB Console
Dim a, b, c, d, e, f, g As Boolean
a = 23 > 14 And 11 > 8
b = 14 > 23 And 11 > 8
' The preceding statements set a to True and b to False.
c = 23 > 14 Or 8 > 11
d = 23 > 67 Or 8 > 11
' The preceding statements set c to True and d to False.
e
f
g
'
= 23 > 67 Xor
= 23 > 14 Xor
= 14 > 23 Xor
The preceding
11 > 8
11 > 8
8 > 11
statements set e to True, f to False, and g to False.
Dim x, y As Boolean
x = Not 23 > 14
y = Not 23 > 67
' The preceding statements set x to False and y to True.
Tasks
1) Adapt the example program so that there is output of the result of
each comparison to the screen
11
AQA COMP1
VB Console
num As Double
rounded As Integer
squarert As Double
trunc As Integer
Tasks
1) Write a program that asks for 5 numbers, calculates the mean average and then rounds it
down. Display the result on screen.
12
AQA COMP1
VB Console
concatenation.
Tasks
1.
Write a program that checks a username against a stored value. How the user enters the
username should NOT be case sensitive.
2.
Adapt program 1 so that it also takes in a password. If the user enters spaces after the
password the computer will trim them out automatically.
3.
Write a program that will check a phone number is of the correct length.
4.
Write a program that asks for a users full name in one inputbox/textbox but then stores the
first and second names in different variables.
13
AQA COMP1
VB Console
date/time.
Tasks
1) Using the toString function, recreate the example program in reverse
14
AQA COMP1
VB Console
Tasks
1.
Write a program which asks for your name and then displays it 5 times on the
screen.
2.
Write a program to display the name of the town you live in 10 times.
3.
Write a program to ask for a persons favourite CD and the artist. Both should
be displayed on the same line 5 times.
4.
Write a program to ask for a number and display its multiplication table 1 to
100
5.
Write a program that asks the user for a number 5 times and adds them all up
to give a total.
15
AQA COMP1
VB Console
Section 12 - Flowcharts
A flowchart is another way of breaking down a program in the form of a
diagram.
The following are recognised flowchart symbols:
16
AQA COMP1
VB Console
17
AQA COMP1
VB Console
Tasks
1) Write a function for calculating the volume of a cuboid
2) Write a function for calculating the volume of a sphere
3) Write a procedure for checking a users password
18
AQA COMP1
VB Console
19
AQA COMP1
VB Console
An example of output is shown below. The output from your program may look different but
must meet the specification.
Charge ()
22
44
66
88
110
132
154
176
198
220
242
264
286
308
You may practice this task at home, but wont be able to refer to any notes or files when assessed.
Submit the following for marking in 1 word document:1. Your code listing
2. Screenshots for suitable test data
20
AQA COMP1
VB Console
created by VB
conPi)
vbCr & conPi)
vbTab & conPi)
Console.ReadKey()
21
AQA COMP1
VB Console
22
AQA COMP1
VB Console
23
AQA COMP1
VB Console
Records
In VB, Records are known as structures. They allow you to combine several data items (or fields) into
one variable. For example in college instead of having a separate variable to store information about
each fact about a student, you would create a student record with fields to store each fact (or field!):
Sub Main()
dim newStudent as student
console.writeline("insert the id")
newStudent.id = console.readline()
console.writeline("insert the name")
newStudent.name = console.readline()
console.writeline("insert the Date of Birth")
newStudent.DoB = console.readline()
console.writeline("new record created: " & newStudent.id & " " & newStudent.name & " " &
newStudent.DoB)
End Sub
But why should we use Records? Well it makes for much neater code, without having to declare
dozens of variables, we could declare a couple of students. You'll also see how neat your code can
become when using arrays of records.
24
AQA COMP1
VB Console
Tasks
1)
2)
3)
4)
25
AQA COMP1
VB Console
=
=
=
=
=
"Scotland"
"Belgium"
"Netherlands"
"Germany"
"France"
Randomize()
randomNum = Int(Int((5 * Rnd()) + 1))
Console.WriteLine("You should go to " & countries(randomNum) & " on holiday.")
Console.ReadKey()
Tasks
1) Write a program which will set up an array to hold 50 numbers. Call the array numbers. Display
the array's contents across the screen. They should all be 0.
2) Create a program that stores an array of car records. At least 5 cars and 4 fields per record.
3) Create a program that stores an array of 5 people records. The information should be entered by
the user.
4) Adapt program 2 to now do a linear search for a certain car and display its details.
26
AQA COMP1
VB Console
Loop
Tasks
1) Write a program that reads the students names from a txt file and displays them on the
screen
2) Write a program that reads 10 team names from a txt file and stores them in an array
3) Write a program that reads 5 song titles from a csv file and displays them on the screen
4) Write a program that reads 20 team names from a csv file into an array, then displays the
array on screen
27
AQA COMP1
VB Console
Tasks
1) Write a program that asks for 5 names and then writes then to a file
2) Write a program that writes the colours of the rainbow to a csv file
3) Write a program that reads a hiscore table from a file, asks the user to add a new hiscore,
then writes this new hiscore table to the file.
28
AQA COMP1
VB Console
Tasks
1) Amend the program so that there is a write procedure and a read procedure which are
called
2) Amend the program so that the 2 student are stored in an array of people, then write and
read the array to the file
Doug Semple 2013
29
AQA COMP1
VB Console
mark")
Loop Until (mark >= 0) And (mark <= 10) ' keeps going until a valid mark is entered
Console.WriteLine("Well done!")
Console.ReadKey()
Tasks
1) Write a program that validates a user is old enough to drive (older than 17, younger than 80)
2) Write a program that checks that a telephone number entered is long enough (string length)
3) Write a program that checks that both a username and password is correct before allowing
you to proceed.
30
AQA COMP1
VB Console
Tasks
1) Create a 2d array which stores and displays a grid for noughts and crosses. Allow users to
pick a location and set it to an X or a O.
2) 2d array which stores a grid for Battleships. Allows the user to place their 5 ships and isplay
on screen.
31
AQA COMP1
VB Console
The list of valid options will be shown in a pop-up list when you are writing code (see the
picture below, after the first Code snippet), so you don't need to remember them or even
type them - instead you can just select them with your mouse or keyboard.
It is harder to make a typo that causes bugs, as not only are the values shown in the list for
you to pick from, but also valid entries you type will be changed to the case you declared
them - so you can see when the case is not automatically corrected for you.
If you use Option Explicit (which is highly recommended anyway), any typo's will cause a
message warning you about them.
They take less memory than Strings, and code that uses them runs faster than the String
equivalent.
Enum Importance
Critical = 4
Important = 3
None = 0
Regular = 2
Trivial = 1
End Enum
Sub Main()
Dim value As Importance = Importance.Critical
' Select the enum and print a value.
Select Case value
Case Importance.Trivial
Console.WriteLine("Not true")
Return
Case Importance.Critical
Console.WriteLine("True")
Exit Select
End Select
End Sub
32
AQA COMP1
VB Console
Range
' Generate a sequence of integers from 1 to 10
' and project their squares.
Dim squares As IEnumerable(Of Integer) = Enumerable.Range(1,
10).Select(Function(x) x * x)
Dim output As New System.Text.StringBuilder
For Each num As Integer In squares
output.AppendLine(num)
Next
' Display the output.
Console.WriteLine (output.ToString())
Console.ReadKey()
Intersection
' Create two integer arrays.
Dim id1() As Integer = {44, 26, 92, 30, 71, 38}
Dim id2() As Integer = {39, 59, 83, 47, 26, 4, 30}
' Find the set intersection of the two arrays.
Dim intersection As IEnumerable(Of Integer) = id1.Intersect(id2)
Dim output As New System.Text.StringBuilder
For Each id As Integer In intersection
output.AppendLine(id)
Next
' Display the output.
Console.WriteLine (output.ToString)
Console.ReadKey()
33
AQA COMP1
VB Console
Difference (Distinct)
Module Module1
Sub Main()
Dim products() As Product =
{New Product With {.Name = "apple", .Code = 9},
New Product With {.Name = "orange", .Code = 4},
New Product With {.Name = "apple", .Code = 9},
New Product With {.Name = "lemon", .Code = 12}}
' Exclude duplicates.
Dim noduplicates = products.Distinct(New ProductComparer())
For Each product In noduplicates
Console.WriteLine(product.Name & " " & product.Code)
Next
Console.ReadKey()
End Sub
End Module
Public Class Product
Public Property Name As String
Public Property Code As Integer
End Class
' Custom comparer for the Product class
Public Class ProductComparer
Implements IEqualityComparer(Of Product)
Public Function Equals1(
ByVal x As Product,
ByVal y As Product
) As Boolean Implements IEqualityComparer(Of Product).Equals
' Check whether the compared objects reference the same data.
If x Is y Then Return True
'Check whether any of the compared objects is null.
If x Is Nothing OrElse y Is Nothing Then Return False
' Check whether the products' properties are equal.
Return (x.Code = y.Code) AndAlso (x.Name = y.Name)
End Function
Public Function GetHashCode1(
ByVal product As Product
) As Integer Implements IEqualityComparer(Of Product).GetHashCode
' Check whether the object is null.
If product Is Nothing Then Return 0
' Get hash code for the Name field if it is not null.
Dim hashProductName =
If(product.Name Is Nothing, 0, product.Name.GetHashCode())
' Get hash code for the Code field.
Dim hashProductCode = product.Code.GetHashCode()
34
AQA COMP1
VB Console
35
AQA COMP1
VB Console
Membership (Contains)
' Create an array of strings.
Dim fruits() As String = {"apple", "banana", "mango", "orange",
"passionfruit", "grape"}
' This is the string to search the array for.
Dim fruit As String = "mango"
' Determine if the array contains the specified string.
Dim hasMango As Boolean = fruits.Contains(fruit)
Dim text As String = IIf(hasMango, "does", "does not")
' Display the output.
Console.WriteLine ("The array " & text & " contain " & fruit)
Console.ReadKey()
36
AQA COMP1
VB Console
Tasks
1) Search for the position of a player in a teamsheet (array) by surname
2) Search for the position of a song in a top 10 chart by title and return its position
3) Using 2 arrays, create an address book of friends. 1 array for names, 1 for addresses.
Prompt the user for a name and then return that persons address.
4) Add mobile telephone numbers to the previous program (Hint: 3rd array needed)
37
AQA COMP1
VB Console
Next
Tasks
1) Write a program that sorts an array of ages.
2) Write a program that reads in scores from a file, sorts them and displays them
3) Add to this program so that there is a corresponding list of names which is also sorted to go
with the score.
4) Add to this program so that it is possible for a user to add an additional hiscore and it
appears in the correct position when displayed.
38
AQA COMP1
VB Console
39
AQA COMP1
VB Console
40
AQA COMP1
VB Console
41