[go: up one dir, main page]

0% found this document useful (0 votes)
11 views15 pages

Multimedia Engineering Lab Problem

Multimedia-Engineering-Lab-Problem

Uploaded by

Noor Alamin 99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views15 pages

Multimedia Engineering Lab Problem

Multimedia-Engineering-Lab-Problem

Uploaded by

Noor Alamin 99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Example program 1 - Water Tank Capacity Program

The code for the program below will allow the user to enter the height, width and depth of a water tank,
then calculate and output the capacity.
Interface

Code when btnCapacity is clicked


'three variables that store the text box inputs from the interface as a decimal
Dim height As Decimal = txtHeight.Text
Dim width As Decimal = txtWidth.Text
Dim depth As Decimal = txtDepth.Text
'calculation to work out the capacity
Dim capacity As Decimal = (height * width * depth) / 1000
'outputs the capacity of the water tank
MessageBox.Show("The tank holds " & Decimal.Round(capacity, 2).ToString & " litres of water")
This is what happens when the button is clicked:

The code above rounds the variable capacity, to round a variable you use the Decimal.Round() function. You
write the name of the variable followed by the number of decimal places e.g. Decimal.Round(capacity,2).
Also note that it has .ToString after it, this is because any variable that is not a string data type much be
converted back to a string before it can be displayed in a message or list box.
Example program 2 - Cylinder Volume Program
The code for the program below will allow the radius and height of a circle, then calculate and output the
volume and surface area.
Interface

Code when btnCalculate is clicked


'three variables that store the two inputs from the interface and the value of pie
Dim radius As Decimal = txtRadius.Text
Dim height As Decimal = txtHeight.Text
Dim pie As Decimal = 3.14159
'calculations to work out the volume and surface area
Dim volume As Decimal = pie * (radius * radius) * height
Dim surfaceArea As Decimal= 2 * (pie * (radius * radius)) + 2 * (pie * radius * height)
#outputs the volume and surface area of the cylinder in a message box.
MessageBox.Show("The volume of your cylinder is: " & Decimal.Round(volume, 2).ToString & " to 2 decimal
places" & vbNewLine & "The surface area of the cylinder is: " & Decimal.Round(surfaceArea, 2).ToString & "
to 2 decimal places.")
This is what happens when the button is clicked:

Example program 1 - Capital City


The code for the program below ask the user the capital city of England and display a suitable message
depending on whether they enter London.
Interface
Code when btnCheck is clicked
'stores the user selection from the combo box in the variable city
Dim city As String = cmbCity.Text
'checks if the city variable has London stored in it
If city = "London" Then
'displays correct if the condition is true
MessageBox.Show("Correct, the capital city of England is: " & city)
Else
'displays wrong if they have entered something else
MessageBox.Show("Wrong, try again!")
End If
This is what happens when the button is clicked:

Example program 2 - Grade Calculator Program


The code for the program below will ask the user to enter a score on a test and then decide what grade they
got.
Interface

Code when btnGradeCheck is clicked


Dim score As Integer = txtScore.Text
If score >= 70 Then
MessageBox.Show("That test score is an A grade")
ElseIf score >= 60 Then
MessageBox.Show("That test score is a B grade")
ElseIf score >= 50 Then
MessageBox.Show("That test score is a C grade")
ElseIf score >= 40 Then
MessageBox.Show("That test score is a D grade")
Else
MessageBox.Show("That test score is a U grade")
End If
NOTE: When ElseIf is used it will only check the next condition If the previous condition is false.
This is what happens when the button is clicked:

Example program 3 - Taxi Fare Calculator Program


The code for the program below will calculate the fare for a taxi ride depending on the distance and number
of passengers. It should add 50% onto the total if there are more than 5 passengers.
Interface

Code when btnFare is clicked


'declares a variable called distance to store the distance entered
Dim distance As Integer = txtDistance.Text
'declares a variable called passengers to store the number of passengers entered
Dim passengers As Integer = txtPassengers.Text
'declares a variable called cost and then uses a calculation to work out the cost
Dim cost As Decimal = 3 + ((distance - 1) * 2)
'checks to see if the number of passengers is more than 5 so that the additional 50% cost can be added
If passengers > 5 Then
'calculates the extra cost if the condition is true
cost = cost * 1.5
End If
'a message box to display the total cost of the journey
MessageBox.Show("The cost of the journey is: " & FormatCurrency(cost).ToString)
Important Points to Note
• there is no Else on this If, you do not need to have one. An if can just check a condition and do
something if it is True and nothing if it is False
• in the MessageBox.Show() command the cost is formatted to currency, this is done by
writing FormatCurrency(variable)
This is what happens when the button is clicked:

Type Check Example


In Visual Basic you can easily do a type check to check whether a value is a number or not a number, see
the example below:
Interface

Code when btnRun is clicked


Dim IsNumbers As Boolean = IsNumeric(txtNumber.Text)
If IsNumbers = True Then
MessageBox.Show("What is entered in txtNumber is a number")
Else
MessageBox.Show("What is entered in txtNumber is not a number")
End If
IsNumeric will return a value either True or False after checking to see if a variable or form control is a
number. You can then use an If to determine what to do.
This is what happens when the button is clicked:

Number
entered

Text entered

Presence Check Example


In Visual Basic you can use the len() function to find the length of a variable or form control. You can then
check to if the length is 0 and if it is this means nothing has been entered, see the example below:
Interface

Code when btnCheck is clicked


If Len(txtNumber.Text) = 0 Then
MessageBox.Show("Please enter a number")
Return
Else
MessageBox.Show("Welcome to the program")
End If
The len() function will return the length of the data stored in a variable/control. If the length is 0 this means
nothing has been entered. Return can be used in a program as it will prevent any further code being
executed. It stops the program running.
This is what happens when the button is clicked:

Nothing entered

Something entered

Range Check Example


You can also validate data by making sure it is within a certain range. For example, entering a test score you
could say that it must be between 0 and 100. An example of how you could use a range check is shown
below:
Interface

Code when btnCheck is clicked


If txtNumber.Text > 0 And txtNumber.Text <= 100 Then
MessageBox.Show("You entered a valid number")
Else
MessageBox.Show("You must enter a number between 1 and 100")
End If
To perform a range check you can simply use an If and then use And to connect two conditions together.
This is what happens when the button is clicked:
Entered out of
range

Iteration - Count Controlled


Iteration is the process of repeating a process over and over. Often in programming you need to repeat a
block of code several times.
FOR Loops
A for loop is known as a count controlled loop, you should use it when you want to repeat a block of code
for a set number of times.

Code without a loop Code using a loop


How the for loop works
• a for loop will repeat for a set number of times and it will repeat between two ranges. for x = 0 to
4 will repeat between the bottom and the top value.
• for x = 0 to 4 will repeat 5 times, 0,1,2,3 and 4.
• x is just a variable that is a counter and stores the number in the loop, this is useful if you need to
use it in calculations.
Example Program 1 - Name Repeater Program
This program asks the user to enter their name and their age. It will then add their name to the list box for
the number of times their age.

Interface
Code when btnGo is clicked
Dim name As String = txtName.Text
Dim age As Integer = txtAge.Text
For x = 0 To age - 1 Step 1
lstOutput.Items.Add("Your name is: " + name)
Next
This is what happens when the button is clicked:

Example Program 2 - Timestable Program


This program will ask the user what timestable they want to learn, it will then calculate the timestables for
that number. It makes use of the counter (x) in the loop to calculate the answer.
Interface
Code when btnCalculate is clicked
Dim number As Integer = txtNumber.Text
Dim total As Integer
'loop that will repeat 13 times (0-12)
For i = 0 To 12 Step 1
'the answer uses x which increases each time to work out the answer
total = number * i
'x is used to display what it is multiplying the number entered by and it is added to the list box
lstOutput.Items.Add(number.ToString & " x " & i.ToString & " = " & total.ToString)
Next
This is what happens when the button is clicked:
Example Program 3 - Total Calculator Program
This program asks the user to enter a five digit number. It then uses sub-strings to add digits 1,3 and 5
together and subtract digits 2 and 4. This program combines using an if with a for loop.
Interface

Code when btnAdd is clicked


Dim total As Integer = 0
Dim number As String = txtNumber.Text
'an array which splits the number variable into characters and stores separately in the array
Dim numbers As Array = number.ToCharArray
'loop to repeat five times
For x = 0 To 4
'x is used to see how many digits have been checked
'if x is 0 this is the first time through the loop, therefore digit 1 is being checked, 2 is digit 3 and 4 is
digit 5.
If x = 0 Or x = 2 Or x = 4 Then
'the number is retrieved from the variable number by using x to get the
'correct digit. As it is a character array, it needs to be converted to a
'string and then converted to an integer so calculations can be carried out
total = total + CInt(numbers(x).ToString)
Else
'if it is digit 2 or 4 then it will subtract the number from the total
total = total - CInt(numbers(x).ToString)
End If
Next
'the final total will be displayed in a message box, this is after the loop has finished
MessageBox.Show("The final total is " & total.ToString)
This is what happens when the button is clicked:

The answer on this example is 15. This is because it will add the first, third and fifth digit to the total (5 + 6
+ 9) which gives 20. It then subtracts digits 2 and 4 (3 + 2) from the total, therefore 20 - 5 to give 15.
Example 4 - Finding the average of numbers in a list
This program has a list that contains test scores. The program will go through the list and add together all
the numbers to get a total. It then works out the average of the test scores.
Interface

Code when btnFind is clicked


'an array called scores, that contains 5 test scores
Dim scores As Array = {10, 12, 7, 6, 10}
'the size of the array if found by using .Length, it will return 5
Dim size As Integer = scores.Length
Dim total As Integer = 0
'a loop that will repeat between 0 and the value of size - 1 (so it doesn't loop one too many times)
For x = 0 To size - 1
'it will use x from the loop to add the correct element from the list to the total
total = total + scores(x)
Next
'the loop has now finished, the average is calculated by dividing the total by the size
Dim average As Decimal = total / size
MessageBox.Show("The average of the numbers is: " & average.ToString)
This is what happens when the button is clicked:

This program gives the average of 9 based on the values in the scores array. This is because 10 + 12 + 7 +
6 + 10 = 45. This is then divided by the size of the list (5) to give 9.
Iteration - Condition Controlled
Iteration is the process of repeating a process over and over. Often in programming you need to repeat a
block of code several times.
WHILE Loops
A while loop is known as a condition controlled loop, you should use it when you do not know how many
times the code needs to repeat as you can say repeat while a condition is True.
Dim userentry As String = "y"
While userentry <> "n"
userentry = InputBox("Play again? y/n")
End While
MessageBox.Show("Game over")
This is what happens when the button is clicked:

How the while loop works


• there is a condition after the word while, it works like an if condition. while the variable userentry is
not equal to n the code inside the loop (that is indented) will repeat
• when n is entered by the user, the loop will end and it will continue with the code after the loop. In
this case it will display a message box saying “Game Over”.
Example Program 1 - Guess the Number
This program asks the user to guess the number, it will keep asking them to guess the number until they
guess it correctly. Once they have guessed it correctly it will tell them how many attempts it took.
Code when btnGuess is clicked
Dim answer As Integer = 15
Dim attempts As Integer = 0
Dim userentry As String = ""
'a loop that repeats while the users guess is not the same as the answer
While answer.ToString <> userentry
userentry = InputBox("Enter a number between 1 and 20")
'each time through the loop 1 is added to the number of attempts
attempts = attempts + 1
End While
'after the loop it will say how many attempts it took
MessageBox.Show("Well done you correctly guessed the number, it took you " & attempts.ToString & "
attempts")
This is what happens when the button is clicked:

Example Program 2 - Adding User Numbers Program


This program asks the user to enter a number. It then asks them whether they want to enter another. If they
do it will ask them another and add it to the previous number, it will keep doing this until they say they do
not want to enter any more numbers. Finally it will output the total.
Code when btnAdd is clicked
Dim total As Integer = 0
Dim another As String = "Y"
'the loop will repeat while the user types Y when asked if they want to enter another number
While another = "Y"
#asks the user to enter a number
Dim number As Integer = InputBox("Enter a number")
'adds the number entered to the total
total = total + number
'asks the user if they want to enter another number
another = InputBox("Do you want to enter another number? Y/N ")
End While
'after the loop ends it outputs the total
MessageBox.Show("The total of your numbers was " & total.ToString)
This is what happens when the button is clicked:

You might also like