Multimedia Engineering Lab Problem
Multimedia Engineering Lab Problem
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
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
Number
entered
Text entered
Nothing entered
Something entered
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:
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
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: