Exercise: Use classes to generate consonants and vowels for Countdown

This exercise is provided to allow potential course delegates to choose the correct Wise Owl Microsoft training course, and may not be reproduced in whole or in part in any format without the prior written consent of Wise Owl.

The answer to the exercise will be included and explained if you attend the course listed below!

Category ==> Visual Basic  (45 exercises)
Topic ==> Creating classes  (4 exercises)
Level ==> Average difficulty
Course ==> Adv VB | Classes & LINQ
Before you can do this exercise, you'll need to download and unzip this file (if you have any problems doing this, click here for help).

You need a minimum screen resolution of about 700 pixels width to see our exercises. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

Right-click on your project in Solution Explorer and choose to add the form from the above folder (you can alternatively press SHIFT + ALT + A ).

The idea behind this exercise is that you should be able to choose your Countdown letters by clicking on the vowel and consonant buttons as required:

Final grid filled in

This is the sort of thing you should see when everything's working (this one's a bit easy - there's an obvious 9-letter word in FWORJAAMI).

Lots of help now follows, but before turning the page, have a think about how you might design a Letter class to hold each letter chosen.

Every time you click on one of the two buttons, your code should generate a new letter of the appropriate type (vowel or consonant) and add this as a textbox to the form.  Here's one suggestion of what this code could look like, for adding a vowel:

Private Sub btnVowel_Click(sender As System.Object, _

e As System.EventArgs) Handles btnVowel.Click

'create a new letter

Dim NextLetter As New Letter

'this is a vowel

NextLetter.IfVowel = True

'add it to the form

NextLetter.AddForm(groupBox1)

'check how many letters there are

CheckNumberLetters()

End Sub

The CheckNumberLetters method should count how many letters have been added already, and if this is now 9 it should:

  • disable the two buttons by setting their Enabled properties to False; then
  • display a message that the countdown clock has begun!

The above shows just one way to solve this problem.  With classes there is rarely a right and a wrong way - just degrees of elegance.

To get the code above to work, create a Letter class and give it the following methods and properties:

Name Scope Type Notes
IfVowel Public Read-write property Whether this is a vowel or not
Choice Private Read-only property The randomly generated letter
AddForm Public Method Add a text box to the form

When you create a new instance of the Letter class you should increase the value of a shared variable called NumberLetters by 1.  When you first open the form, you should set the value of this shared variable to 0.

Create the following fields and constants in your Letter class:

'the number of letters so far

Public Shared NumberLetters As Integer = 0

'generate a random number seed

Private Shared r As New Random()

'the horizontal offset for the first letter

Private Const initialGap As Integer = 24

'the horizontal offset for all other letters

Private Const gapBetweenLetters As Integer = 54

Private Const Vowels As String = "AEIOU"

Private Const Consonants As String = "BCDFGHJKLMNPQRSTVWXYZ"

The random number seed has to be shared, as otherwise you'll get the same random number for each letter.

Create a read-write Boolean property called IfVowel, then a public read-only property called Choice which should return the letter generated.  You may find the following two lines of code useful (although you'll need to amend them):

'get a random number between 1 and N

Dim ChoicePosition As Integer = r.Next(1, SomeString.Length)

'return the letter at this position

Return SomeString.Substring(ChoicePosition - 1, 1)

Now create the AddForm method, which should begin like this:

'add the letter as a textbox to the form's groupbox

Public Sub AddForm(grp As GroupBox)

This method thus takes the group box as an argument, and adds one textbox to the group box for each letter found.  You should now add code to this method so that it:

  • Creates a new text box;
  • Adds it to the group box;
  • Sets various formatting properties; and
  • Sets the text in the box to equal the value of the Choice property.

The easiest way to get this code is to add a textbox to the group box within the form, then copy and edit the generated designer code.

When everything is working, generate a Countdown word using your system (but don't worry about the fact that it gives equal weight to common and obscure letters).

This page has 0 threads Add post