Chapter #2 Exercises Exercises 2.9 1) My Favorite Month

advertisement
Chapter #2 Exercises
Exercises 2.9
SAVE ALL EXERCISES IN THE "CHAPTER 2" FOLDER
1) My Favorite Month
Save Project as: Month.vbp
Write a program that accepts what month of the year the user enjoys the most.
Have the user enter in their full name and display their favorite month.
Use the following Program Specifications:
1. Make a sketch of the interface before you begin the program. (5 points)
2. Complete the interface with the following properties table: (10 points)
Object
Name
Property
Text
Caption Property
Propert
y
First Name Label
lblFName
First Name
Middle Name
Label
lblMName
Middle Name
Last Name Label
lblLName
Last Name
Title of the
Program
lblTitle
Welcome to my
first Visual Basic
Program .....
First Name
txtFName
empty
Please Enter your
First Name
f_FName
Middle Name
txtMName
empty
Middle Name
f_MName
Last Name
txtLName
empty
Last Name
f_LName
Favorite Month
txtFMonth
empty
Favorite Month
f_Month
Process Display
Button
cmdDisplay
DISPLAY THE
MONTH
Variable Name
Clear Button
cmdClear
CLEAR
Exit Button
cmdExit
E&XIT
1. In order to display the information you should use the message box. Make sure that when
you display the users information that it has the proper spacing.
2. Change the form colour and title the form.
3. Change the text boxes to the same colour.
4. Declare the variables in general declarations.
5. Assign the text boxes to the proper variable names.
(2 points each = 10 points)
6. The program runs correctly (5 points)
TOTAL = 30 points
2) The Pet Purchasing Program
Independant Research: Find out the cost of your favorite pet, the veterinary bills per month,
food costs and accessories. Make a list of at least 5 accessories (Ie: leash, collar etc) and their
costs.
Save Project as: Pet.vbp
Write a program that allows the user to enter in the type of pet, name of the pet, cost of a pet,
veterinary costs per month, food costs and accessory costs for the pet (minimum 5 accesories).
The tax must be calculated on each item spent.
Sketch the interface of your pet purchasing program.
Program Specifications: Use val and constants, declare the appropriate variables and assign
them to any text boxes.
Use the format currency function to display the final cost a pet.
Properties Table
Object
Name
Property
Text
Caption Property
Propert
y
Title of the
Program
lblTitle
THE PET ORDER
FORM
Type of Pet Label
lblPetType
Type of Pet
Variable Name
Name of Pet Label lblPetName
Pet Name
Enter Information
Label
lblName
Enter the
information below.
Type of Pet Text
Box
txtPetType
empty
Pet Type
f_PetType
Name of Pet Text
Box
txtPetName
empty
Pet Name
f_PetName
Price of Pet Text
Box
txtPrice
empty
Price of Pet
f_Price
Veterinary Cost
Text Box
txtVetCost
empty
Veterinary Cost
f_VetCost
Food Cost Text
Box
txtFoodCost
empty
Food Cost Per
Month
f_FoodCost
Accessory 1 Text
Box
txtAccess1
empty
Accesory Group 1
f_Access1
Accessory 2 Text
Box
txtAccess2
empty
Acessory Group 2
f_Access2
Accessory 3 or
more
programmers
decision
Subtotal Text Box txtSubTotal
empty
SubTotal
f_SubTotal
Tax Text Box
txtTax
empty
Tax
f_Tax
Total Text Box
txtTotal
empty
Total
f_Total
Process Display
Button
cmdDisplay
THE GRAND
TOTAL
Clear Button
cmdClear
CLEAR
Exit Button
cmdExit
E&XIT
Text Boxes
3) Happy Birthday
Write a program that asks for a person's Birth date as month-day-year. Your program should
output the number of days that they have been alive. The current year represents the year that
you are writing this program in.
Save Project as: Birthday.vbp
Program Specifications: variable declaration, calculations and correct age output.
File Name: Birthday
4) I Love Mathematics
Most of us enjoy a good challenge from calculations in math. Write a program that accepts 4
numbers from a user and performs calculations written below.
Save Project as: Math.vbp
Make a Sketch of the interface.
Complete the program using the Properties Table below.
Object
Name
Property
Text
Caption Property
Propert
y
First Number
Label
lblNum1
First Number
Second Number
Label
lblNum2
Second Number
Third Number
Label
lblNum3
Third Number
Fourth Number
Label
lblNum4
Fourth Number
Answer Label
lblAnswer
Answer
Variable Name
First Number Text txtNum1
Box
empty
f_Num1
Second Number
Text Box
txtNum2
empty
f_Num2
Third Number
Text Box
txtNum3
empty
f_Num3
Fourth Number
Text Box
txtNum34
empty
f_Num4
Answer Text Box
txtAnswer
empty
f_Answer
Display Button
cmdProcess
PROCESS THE
EQUATION
Clear Button
cmdClear
CLEAR
Exit Button
cmdExit
E&XIT
The code to sum all four numbers entered in the text boxes would be attached to the PROCESS
THE EQUATION button. The code would appear as follows.
Private Sub cmdProcess_Click()
Dim f_Num1 as Integer, f_Num2 as Integer, f_Num3 as Integer, f_Num4 as Integer
Dim f_Answer as Integer
f_num1 = Val(txtNum1.Text)
f_num2 = Val(txtNum2.Text)
f_num3 = Val(txtNum3.Text)
f_num4 = Val(txtNum4.Text)
txtAnswer.Text = f_Num1 + f_Num2 +f_Num3 + f_Num4
End Sub
Why is Val used in the assignment statements?
Write the expression in your notebooks for the following equations. Then write the code for
the following equations. You may need to have 9 more command buttons.
Description
Expressions
1
the sum of the first two numbers times the sum
of the last two numbers
(num1+num2) *(num3
+num4)
2
the product of all the numbers
3
the average of all the numbers
4
the sum of the squares of the numbers
5
four times the first number minus two times the
last number
6
the first number minus the second number then
times the third number
7
the sum of the halves of each number
8
the average of the cubes of each number
9
the sum of the square root of each number
5) Post Office Program
Suppose you were going to send a large 81/2 by 11 envelope from Canada to anywhere in the
world. Before you send the envelope you need to calculate the cost of sending the envelope
which is dependant on the weight of the envelope. Write a program that allows the user to enter
in the weight of any envelope and displays the cost to send any mail from Canada the United
States.
HOMEWORK: Go to the post office and find out the cost of sending larger envelopes, heavier
envelopes and packages to the United States. Pick a European location and find the code to send
the envelope. Remember it is all determined by mass/weight of the envelope.
Program Specifications: use variables, use constants, the val and format functions.
Save Project as: Post.vbp
6) The Tax Program
Extension of Activity 2.6
Save Project as: Taxes.vbp
This is the year you have decided to work. You have realized that your first pay check is less
than expected. The reason for this is because your employer and the government have deducted
monies from your gross pay. Allow the user to enter in the following;
1. Gross pay for 2 weeks
2. Vacation pay
3. Federal tax
4. Unemplyment Insurance Premium
5. Canada Pension Plan Premium
6. Benefits
7. Net Pay
Using a word processor create a properties table. Fill in the the Properties Table for this program.
This is good practice for programmers, it helps to keep track of variable names and names of all
the objects in your interface. Always include a Process Button, Clear Button and an Exit Button.
You will need more rows than shown below.
Object
Name
Property
Text
Caption Property
Propert
y
Variable Name
fill in yourself!!
Process Display
Button
cmdDisplay
THE GRAND
TOTAL
Clear Button
cmdClear
CLEAR
Exit Button
cmdExit
E&XIT
Program Specifications: include use of variables, constants, calculations of the above. Display
the information in separate text boxes with the net pay in a larger text box.
7. Metric to Imperial Conversions
Write a program that will allow a user to convert from imperial to metric or from metric to
imperial for distance, capacity or weight. Save Project as: Conversions.vbp
Distance - leave it in
Capacity
pint to liter
quart to liter
gallon to liter
Weight/Mass
ounces to grams
pounds to grams
kilogram to ounce
tonnes to kilogram
Complete your own properties table for the conversion sets.
Program Specifications: The user must use val, declare all appropriate variables, include at least
4 conversions. Include an introductory form and three subsequent forms, one per conversion set.
Download