CST2601 Visual Basic I
Assignments


Click on this link to turn in your assignments:
\\Rc-hutch-ap\VOL1\HOME\allen_b\CST2601Labs
  • Turn in the complete folder for each exercise.  The folder should contain the .vbp and .frm files.
  • Your name and exercise number should be in the comments at the top of the program.

Note:  This link only works if you are connected to the Ridgewater campus network and using Microsoft Internet Explorer. (Netscape cannot be used).

If you do not have network access, you may turn your homework in on a floppy disk.  You must label the diskette with your name, assignment number, and course number.  The diskette must be turned in at the start of class.


Note:  Most of the programming examples in the textbook can be found on the CD-ROM included with the book.

 

Lab 3

Read these notes on coding conventions.  Try to apply coding conventions to the programs that you write for this class.  In particular note the coding conventions for variable names the suggested prefixes for controls.

Notes on variables.

Quick reference on data types.

It is a good idea to use the Option Explicit statement in your program.  This greatly reduces the risk of variable naming errors.

 

Exercises

Exercise 3.21

Write a temperature conversion program that converts a Fahrenheit temperature to a Celsius temperature.  Provide a TextBox for user input and a Label for displaying the converted temperature.  Provide a Input button to read the value from the TextBox.  Also provide the user with an Exit button to end program execution.

Use the following formula:  Celsius = 5/9 * (Fahrenheit - 32)

FahrCel 

Notice that the example program crashes when the input textbox is left blank or has something other than a number in it.  In later chapters we will do error checking to take into account these sort of situations.

Exercise 3.22

Enhance Exercise 3.21 to provide a conversion from Fahrenheit to Kelvin.  Display the converted Kelvin temperature in a second Label.

Use the formula:  Kelvin = Celsius + 273


Lab 4

If...Then...Else statement notes.

While... Wend statement notes.

Do...Loop statement notes.

 

Exercises

Exercise 4.21

The process of finding the largest number (i.e., the maximum of a group of numbers) is used frequently in computer applications.  For example, a program that determines the winner of a sales contest would input the number of units sold by each salesperson.  The salesperson who sells the most units wins the contest.  Write a program that inputs a series of 10 numbers, and determines and prints the largest of the numbers.  Hint: Your program should use three variables as follows:

1. counter:  A counter to count 10 (i.e. to keep track of how many numbers have been input, and to determine when all 10 numbers have been processed).

2. number:  The current number to input to the program.

3. largest:  The largest number found so far.

Exercise 4.26

A company wants to transmit data over the telephone, but they are concerned that their phones may be tapped.  All of their data is transmitted as four-digit Integers.  They have asked you to write a program that will encrypt their data so that it may be transmitted more securely.  Your program should read a four-digit Integer and encrypt it as follows:  Replace each digit by (the sum of that digit plus 7) modulus 10.  Then, swap the first digit with the third, as swap the second digit with the fourth.  Then print the encrypted Integer.

Exercise 4.27

Write a program that takes an encrypted Integer from Exercise 4.26 and decrypts it.


Lab 5

For... Next statement notes.

Select... Case statement notes.

Format function notes.

Code basics.

Programming example.

 

Exercises

Exercise 5.16

Write a program that prints the following patterns separately on the form each time a button is pressed.  Provide four buttons A, B, C, and D.  When button A is pressed, the triangle shown in part (A) is printed, etc.  Use For/Next loops to generate the patterns.  Each triangle's asterisks(*) should be printed by a single statement of the form Print "*"; (this causes the asterisks to print side by side.)  Hint: The last two patterns C and D require that each line begin with an appropriate number of blanks.  Set the form's Font to Courier Bold.

 

Pattern A
*
**
***
****
*****
******
*******
********
*********
**********
Pattern B
**********
*********
********
*******
******
*****
****
***
**
*
Pattern C
**********
 *********
  ********
   *******
    ******
     *****
      ****
       ***
        **
         *
Pattern D
         *
        **
       ***
      ****
     *****
    ******
   *******
  ********
 *********
**********

 

Exercise 5.19

Using the series

sin(x) = x - x3/3! + x5/5! - x7/7! + ...

calculate the sine x to n terms.  The GUI should consist of two TextBoxes, three Labels and a button.  One TextBox should allow the user to input the value of x in radians. (2p radians in a circle).  The second TextBox should allow the user to input the the number of terms n.  The larger terms, the more accurate the value.  Display the results in a Label.  The other two Labels should be used as prompts.  The calculations should be performed and displayed when the button is pressed.

Hint: "!" in mathematics means factorial.
3! = 1*2*3   5! = 1*2*3*4*5   7!= 1*2*3*4*5*6*7   and so on...

Hint: 2p radians in a circle = 6.2832 radians in a full circle = 360 degrees.

 

Exercise 5.20  (Pythagorean Triples)

A right triangle can have sides that are all Integers.  The set of three Integer values for the sides of a right triangle is called a Pythagorean triple.  These three sides must satisfy the relationship that the sum of the squares of two of the sides is equal to the square of the hypotenuse.  Write a program to find all Pythagorean triples for side1, side2, and the hypotenuse, all no larger than 500.  Use a triple-nested For/Next loop that tries all possibilities.  The is an example of "brute force" computing.

Hint: side a squared plus side b squared equals the hypotenuse squared in a right triangle.  So a2 + b2 = c2

 

Extra Credit

Modify the For/Next loops in Exercise 5.20 to conserve computer time.  Side1 + Side2 cannot be shorter than the hypotenuse side.


Lab 6

Variable Scope.

Static variables.

Procedure Arguments.

Notes on Procedures.

Sample program showing regular and recursive functions.

 

Exercises

Exercise 6.36

An Integer number is said to be a perfect number if its factors, including 1 (but not the number itself), sum to the number.  For example, 6 is a perfect number because 6=1+2+3.  Write a Function procedure Perfect that determines if parameter number is a perfect number.  Use this procedure in a program that determines if parameter number is a perfect number.  Use this procedure in a program that determines and prints all the perfect numbers between 1 and 1000.  Print the factors of each perfect number to confirm that the number is indeed perfect.

 

Exercise 6.42

Write a Function procedure that takes two String arguments representing a first name and a last name, concatenates the two Strings to form a new String representing the full name and returns the concatenated String.

Write a test program to test this procedure.


Lab 7

Notes on Arrays

Rnd function and Randomize

More notes on functions.

 

Exercises

Exercise 7.14 (Modified)

Use a one-dimensional array to solve the following problem.  Read in numbers.  Each of which is between 0 and 100, inclusive.  As each number is input, print it only if it is not a duplicate of a number already input.

Hint:  Since we cannot be certain how many numbers the user will enter, use an array that keeps increasing in size.  This is done with the ReDim statement.

 

Exercise 7.15

Write a program that simulates the rolling of two dice.  The program should use function Rnd to roll the first die, and should use Rnd again to roll the second die.  The sum of the two values should then be calculated.  Note:  Since each die can show an Integer value from 1 to 6, the sum of the two values will vary from 2 to 12, with 7 being the most frequent sum and 2 and and 12 being the least frequent sums.  Figure 7.20 shows the 36 possible combinations of the two dice.  Your program should roll the two dice 36,000 times.  Use a one-dimensional array to tally the number of times each possible sum appears.  Print the results in a tabular format.  Also determine if the totals are reasonable (i.e., there are six ways to roll a 7), so approximately one sixth of all the rolls should be 7.

 

Exercise 7.26

(Selection Sort) A selection sort searches an array looking for the smallest element in the array.  Then, the smallest element is swapped with the first element of the array.  The process is repeated for the subarray beginning with the second element of the array.  Each pass of the array results in at least one more element being placed into its proper location.  This sort performs comparably to the bubble sort -- for an array of n elements, n - 1 passes must be made, and for each subarray, n -1 comparisons must be made to find the smallest value.  When the subarray being processed contains one element, the array is sorted.  Write a program to perform this algorithm.

 

Exercise 7.32

The Fibonacci series

0, 1, 1, 2, 3, 5, 8, 13, 21, ...

begins with the terms 0 and 1 and has the property that each succeeding term is the sum of the two preceding terms.  (a) Write a nonrecursive procedure Fibonacci that calculates the nth Fibonacci number.  (b) Determine the largest Fibonacci number that can be calculated on your system.  Use data type Double.

Hint:  This exercise does not require an array at all.  However I am including it in the assignment because it is a good example of the use of a function.

Below is the start of the function.

Function Fibonacci(ByVal n As Integer) As Double
   'The sequence is:
   'Fibonacci(0) returns 0
   'Fibonacci(1) returns 1
   'Fibonacci(2) returns 1
   'Fibonacci(3) returns 2
   'Fibonacci(4) returns 3
   'Fibonacci(5) returns 5
   'Fibonacci(6) returns 8
   'Fibonacci(7) returns 13
   'Fibonacci(8) returns 21
  
   'Write your code here...
  

End Function


Lab 8

String keywords.

 

Exercises

Exercise 8.14

Write a program that uses random number generation to create sentences.  Use four arrays of Strings called article, noun, verb and preposition.  Create a sentence by selecting a word at random from each array in the following order: article, noun, verb, preposition, article and noun.  As each word is picked, concatenate it to the previous words in the the sentence.  The words should be separated by spaces.  When the sentence is output, it should start with a capital letter and end with a period.  The program should generate 20 sentences and output them to a List.

The arrays should be filled as follows:

article noun verb preposition
the boy drove to
a girl jumped from
one dog ran over
some town walked under
any car skipped on

Hint:
Example sentences would be:

One car jumped on the dog.
The girl ran under some car.

 

Exercise 8.16

Write a program that inputs a telephone number as a string in the form
(555) 555-5555.  The program should use functions Mid$, Left$ and Right$ to extract the area code, the first three digits of the phone number and the last four digits of the phone number.  The seven digits of the phone number should be concatenated into one string.  The program should convert the area code string and the phone number string to Longs.  Both the area code and the phone number should be printed.


Lab 9

Notes on the VB coordinate system.

Notes on scale.  Additional notes on scale.

Twips explained.

How to draw a circle.

The timer control.

 

Exercises

Exercise 9.10

Write a program that reads four numbers from the user and graphs the numbers as a pie chart.

 

Exercise 9.16

Write a program that draws a grid over an image displayed in a PictureBox.  Provide a CheckBox that controls when the grid is visible.

 

Exercise Blinker

Write a program that alternately flashes a solid circle from green to red.  Green for 1 second, red for two seconds, then keep repeating.

Hint:  You need two timer controls.


Lab 10

 

Exercises

Exercise 10.6

Write a program that allows the user to understand the relationship between Fahrenheit temperatures and Celsius temperatures.  Use a vertical Slider to scroll through a range of Fahrenheit temperatures.  Use a Label to display the equivalent Celsius temperature.  The Label should be updated as the Slider's value changes.  Use the following formula:

Celsius = 5/9 * (Fahrenheit - 32)
 

Exercise MenuPopup

Write a program that utilizes menus that have both Alt hotkeys and shortcuts.  Also the program needs to pop up a message box.   The program should duplicate the sample program found below.

MenuPopup 

 

Exercise Phone Number

Modify the program that you did for Exercise 8.16 using a MaskEdit control so that the user must enter the phone number in the exact format:
(555) 555-5555


Original Exercise 8.16

Write a program that inputs a telephone number as a string in the form
(555) 555-5555.  The program should use functions Mid$, Left$ and Right$ to extract the area code, the first three digits of the phone number and the last four digits of the phone number.  The seven digits of the phone number should be concatenated into one string.  The program should convert the area code string and the phone number string to Longs.  Both the area code and the phone number should be printed.

 

Exercise Pie Chart

Modify the program that you did for Exercise 9.10 using the Microsoft Chart Control 6.0 ActiveX control.


Original Exercise 9.10

Write a program that reads four numbers from the user and graphs the numbers as a pie chart.

 


Lab 11

 

Exercises

Exercise Login

Modify the program MenuPopup that your did earlier.  On initial startup of the program, buttons two and three should be grayed out.  Also the corresponding menu items should be grayed out as well.  When the user presses the login button, the login dialog box should open.  The correct password should be "abc123".  Once the correct password has been entered, the buttons two and three should be enabled again.  Also the corresponding menu selections should be enabled as well.  The program should duplicate the sample program found below.

Login 

 


Lab 12

 

Exercises

Exercise 12.10

Write a program that uses the mouse to draw a square on the form.  The upper-left coordinate should be the location where the user first pressed the mouse button, and the lower-right coordinate should be the location where the user releases the mouse button.  Also display the area in twips in the Caption.

 

 

Exercises

Exercise 12.11

Modify your solution to Exercise 12.10 to allow the user to draw different shapes.  As a minimum, the user should be allowed to choose from an oval, circle, line and rectangle.  Allow the user to select the shape type from a menu.

 

 

Exercises

Exercise 12.12

Modify your solution to Exercise 12.11 to allow the user to specify the shape using the keyboard.  The shape drawn should be determined by the following keys: o for oval, c for circle, r for rectangle, O for a solid circle, R for a solid rectangle and L for line.  All drawing is done in the color blue.  The initial shape defaults to a circle.

 

 

Exercises

Exercise 12.14

Modify your solution to Exercise 12.10 or Exercise 12.11 to provide a "rubber-banding" effect.  As the user drags the mouse, the user should be able to see the current size of the rectangle to know exactly what the rectangle will look like when the mouse button is released.

 


Lab 13

 

Exercises

 Exercise ErrorHandle

Modify your the program found on page 543 (figure 13.3)  In such a way that the error handling presented in the problem is not required.  The program should accept any input from the user and deal with the input appropriately, whether that be popping up an error message, displaying a message on the form, or actually processing the data.