0% found this document useful (0 votes)
7 views

Lab 4

This document contains instructions for two programming activities using variables in Visual Basic. Activity 1 involves creating an application to track ticket sales and spectators at the Olympic Games. It includes input fields, calculation buttons, and labels to display running totals. Activity 2 involves creating a cylinder volume calculator that takes radius and height as input and displays the calculated volume using a constant PI variable. Students are tasked with implementing these applications during their lab time.

Uploaded by

Tiulipe Penisoni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lab 4

This document contains instructions for two programming activities using variables in Visual Basic. Activity 1 involves creating an application to track ticket sales and spectators at the Olympic Games. It includes input fields, calculation buttons, and labels to display running totals. Activity 2 involves creating a cylinder volume calculator that takes radius and height as input and displays the calculated volume using a constant PI variable. Students are tasked with implementing these applications during their lab time.

Uploaded by

Tiulipe Penisoni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

IS122 Application Programming

IS122 INFORMATION SYSTEMS II

LAB 4

PROGRAMMING WITH VARIABLES

TABLE OF CONTENTS
Objectives ..................................................................................................................................................... 2
Introduction .................................................................................................................................................. 2
Activity 1: Gate Takings Application ..............................................................................................................2
Activity 2: More on Variables ........................................................................................................................ 5

1
Objectives
At the end of this lab activity you should be able to:

Design forms.
Program with variables.
Use the Convert class methods or procedures to convert data from one data type to another.

Introduction
In this lab, you will become acquainted declaring and using variables in simple Visual Basic Windows
applications.

Activity 1: Gate Takings Application


1. Scenario:

The Olympic Games Organizing committee wants an application that they can use at the end of the day to add
their cash takings from the sale of tickets from the various games that are being played on a particular day. The
application should also be able to sum up the number of spectators from the various games.

The application should have the following elements:

a) A place to type the name of the sport - txtSport


b) A place to type the number of spectators for the sport - txtSpectator
c) A label to display the cash takings for that particular sport - lblCashTakings
d) A command button called “Calculate” to multiply the number of spectators entered with a constant
variable which contains the price of the ticket and display it in the label above. You can have any value
for this variable. In the click event of this button, implement the following algorithm:

1. Declare a constant variable called dblTICKET_PRICE of type Double to store ticket price.
Assign it any value.
2. Declare a variable of type integer called intSpectators to store number of spectators.
3. Declare a variable of type double to store gate takings called dblGateTakings.
4. Use the Convert class method .ToInt32() to convert the number of spectators entered in the
textbox to an Integer value and store it in the variable declared in step 2.

intSpectators = Convert.ToInt32(txtSpectator.Text)

5. Calculate gate takings by multiplying ticket price (declared in step 1) by the number of spectators
and store it in the variable declared in step 3.

dblGateTakings = dblTICKET_PRICE * intSpectators

6. Display gate takings in the label.

lblCashTakings.Text = Convert.ToString(dblGateTakings)

e) A command button or simply a button called “Add” to capture the cash taking displayed in the label and
the number of spectators entered to the running total.
2
The following hints may be used:

1. Declare the following variables to have Module level scope.

Dim mdblTotal_GateTakings as Double


Dim mintTotal_Spectators as Integer

2. In the click event of this button, implement the following code:

mdblTotal_GateTakings = mdblTotal_GateTakings + Convert.ToDouble(lblCashTakings.Text)


mintTotal_Spectators = mintTotal_Spectators + Convert.ToInt32(txtSpectator.Text)

3. And then clear txtSport, txtSpectator and lblCashTakings and set focus to
txtSport.

f) A label to display the grand total of the cash takings (lblTotal_CashTakings ) and another label
(lblTotal_Spectators ) to display the grand total of the number of spectators at the games.

g) A command button called “Display Totals” to show the grand totals of the cash takings and the
number of spectators in the labels.

In the click event of this button, implement the following code:

lblTotal_CashTakings.Text = mdblTotal_GateTakings.ToString("C")
lblTotal_Spectators.Text = Convert.ToString(mintTotal_Spectators)

h) A command button (btnExit) to exit the program.

In the click event of this button, implement the following code:

Me.Close()

i) Set Option Explicit and Strict to On

2. Application Behaviour:

i. User enters details of a particular sport – name and number of spectators for that sport.
ii. Hits the “Calculate” button to reveal the gate takings for that sport.
iii. Then hits the “Add” button to accumulate the gate takings and number of spectators to running total
variables for gate takings and number of spectators respectively.
iv. Repeats steps (i) to (iii) for as many sports as you like.
v. Click the “Display Totals” button to reveal the day’s gate takings and total spectators from all the sports
that were entered.

3
3. Pre-Lab (before coming to the lab)

Design the form for Activity 1. You should draw a sketch of the user interface and all the controls on the
form. Think of the variables and their type that you are going to use. Decide on the scope you need to give to
the variables (block, procedure or class/module level).

4. Lab: During the lab time...

Implement the application designed using Microsoft Visual Basic.

5. If you have time:

Add a label control in which you can display all the sports that were played on that particular day. Remember
the user types in the name of the sport, e.g. Golf, Lawn Bowling, Tennis etc. You will need to concatenate the
sport names entered in a string variable so that when the user clicks “Display Totals”, the text property of
the label reads the names of the sports that were entered e.g. “Golf, Lawn, Bowling, Tennis,”. i.e.
Assign the value of the string variable to the text property of the label. The concatenation can be done each
time the “Add” button is clicked. This string variable needs to have class or module level scope. For example:

Dim strSports as String

And code to concatenate sport names:

strSports = strSports & txtSport.Text & ", "

4
Activity 2: More on Variables

Your friend, a science teacher, ask you to write an application that will allow his students to find the volume
of a cylinder by entering certain measurements such as the radius and height of the cylinder. The application
should have the following elements:

1. A place to enter the value for the radius


2. A place to enter the value for the height
3. A label to display the calculated volume
4. A command button called “Calculate Volume” to calculate the volume given the measurements
entered for the radius (step 1) and height (step 2), and display the result in the label (step 3).
5. A command button to exit the application

Your program should have a constant variable to hold the value for the PI. Set its value to 3.14159.

What scope will this variable have?

The formula to find the volume of a regular cylinder is:

Volume = PI * radius2 * height

Lab: During the lab time...

1. In addition to solving this problem, improve the interface by adding appropriate Access Keys to the
command buttons, setting the TabIndex and designate a default and cancel button.
2. Show the finished program to your tutor if there is one.

If you have time:

Modify your application so that it also calculates the surface area of the cylinder, with one circular end
open.

5
Pre-Lab (before coming to the lab)
Design the form for Activity 2.

You might also like