PROJECT 2:

Astrological Toys

CSE100/INFO100 Fluency with Information Technology

Autumn 2001


Part 1................................................................................................................................................................................................................ 3

Grading Criteria.......................................................................................................................................................................................... 8

Part II............................................................................................................................................................................................................... 9

Coding Binary Search............................................................................................................................................................................. 13

Detailed Logic.......................................................................................................................................................................................... 15

Overall Logic............................................................................................................................................................................................ 14

Grading Criteria........................................................................................................................................................................................ 17

 


 

Introduction

In this project (and the labs accompanying it) you will write a series of programs [applications] that look up and display astrological signs and dates. The programs that will make up the project are as follows:

 

This Time – a digital clock with the date on the window bar

Sign Finder – present your birthday and be told your astrological sign

Zodiac Range - give your sign and receive the interval containing your birthday

Day Finder – generalizing the Sign Finder to ask a series of questions to find the respondent's birthday using binary search

 

Two of these applications will be done in lab:

This Time (Lab 7) and Sign Finder (Lab 9)

 

Those two applications will help you prepare the two parts that will be turned in for Project 2:

Zodiac Range and Day Finder

 

The various projects are written in Visual Basic 6.0 and serve as your introduction to algorithmic thinking, program design and the characteristics of VB6.

 

It is suggested you create a new folder for each project piece of the assignment. This will assist you in keeping track of all of the various files produced as well as simplify the saving of your work to Dante or a floppy disk.

 

This Time: This is a simple digital clock program with the date on the window bar. The interface (GUI) that you will create has the following form and the solution will be covered in Lab 7.

 

 


Sign Finder:
The Sign Finder program will be discussed briefly in class and you will go through the solution in Lab 9. Sign Finder has the following graphic interface (adjust colors and font styles any way you wish to):

 

 

 

Part 1: Zodiac Range             Due October 31st Noon

 

The objectives of this project piece are:

·         Create your first VB application

·         Develop experience with form creation and refinement

·         Understand how an algorithm is expressed in a programming language

·         Understand the flow of information through variables

·         Understand the concept of “event programming”

·         Practice using the Visual Basic IDE and managing project development.

 

The application will present the user with a set of 12 radio buttons corresponding to the Zodiac signs, and when one radio button is pressed, followed by a click of OK, will print out the starting and ending dates of that sign. The main task is to construct the form to have the 12 radio buttons, one command button and five labels. Zodiac Range has the following graphic interface.  This is the initial window for the Zodiac application:

 

 

 

The next window results from clicking "Leo" and "OK"

 

         

 

The overall logic is as follows. The form contains all of the structures shown in both displays above. For the initial display, the "You were born" text, the dates and the "and" text are all hidden. When the command (OK) button is pushed, the command button is hidden, and the "You were born" text, the Go Again button, etc. is made visible.

 

Detailed Steps of What to Do:

 

(a)      Create a new project and a form template.  Name the Project Zodiac

(b)      Rename the form object frmZodiac

(c)      Save the form and the project, maintaining “frmZodiac” as the form name

and "Zodiac" as the project name.  The ".frm" and ".vbp" extensions are added automatically.

(d)      Select a new background color and make other changes to the form you think are appropriate to make it attractive and appealing.

(e)      Double click on the form to bring up the code window.

(f)      Add the terms Option Explicit line at the top of the code window if Option Explicit is not shown

(g)      Enter the following variable declarations: (see next page)

Dim loMonth As String

Dim hiMonth As String

Dim loDay As Integer

Dim hiDay As Integer

 

Notice how VB’s “word completion” tries to assist after the "As" is entered. The two string variables will represent the two months that the astrological sign spans, and the two integer variables will represent the starting and ending days. So, for Leo, the loMonth will be assigned the value "July " and hiMonth will be assigned "August ". The loDay variable will be assigned 23 and the hiDay will be assigned 22.

Aries

March 21            

April 19

Taurus

April 20                    

May 20

Gemini

May 21                     

June 20

Cancer

June 21                    

July 22

Leo

July 23                      

August 22

Virgo

August 23                     

September 22

Libra

September 23                       

October 22

Scorpio

October 23                    

November 21

Sagittarius

November 22                

December 21

Capricorn

December 22                  

January 19

Aquarius

January 20                                

February 18

Table 1:  List of Astrological Signs

 

 
Pisces

February 19                   

March 20

 

 

 

(h)      In the form, place the 12 option or radio buttons, naming each with a

new name. The name should begin with the letters "opt" (stands for option button) and be followed by the first three letters of the sign. The name for the Aries button will be optAri. Change the caption on the button to the intended Zodiac sign. Change the background color, font and any other features of the button that you need to make it aesthetically pleasing.

(i)       Once the buttons are defined, place labels for the text lines on the form:

"Enter your Zodiac sign"

"You were born between"

"starting date"

"and"

"ending date"

 

Name the labels lblEnter, lblBorn, lblStDate, lblAnd, lblEndDate, respectively. Change the fonts, background colors and any other needed properties. The last four labels should be hidden, i.e. have their visibility property set to "false".

 

(j)      Click on each radio button to bring up the code window with the "click event" handler for that button. Customize it by setting the loMonth to the starting month (followed by a space) in quotes, hiMonth to the ending month (followed by a space) in quotes, loDay to the starting day for the sign, and hiDay for the ending day for the sign. The extra space is included so the day doesn’t smash into the month when printed out. For example, the Capricorn "click event" handler would be:

Private Sub optCap_Click()

loMonth = "December "

hiMonth = "January "

loDay = 22

hiDay = 19

End Sub

 

There is one further trick that can be used to make the text appear more natural when it is printed. Assume that the start date precedes the " and ", which is followed by the end date. If these are all printed on the same line, and the start date is right justified, then the two dates will "hug" the "and" and make the text appear without spaces.

 

 

 

 

 

 

 


(k)      Add a command button, renamed cmdOK and captioned OK. Modify its visual properties appropriately. Click on the OK command button to bring up the code window with the cmdOK_Click procedure. Set the start and end dates by changing the captions as follows:

 

lblStDate.Caption = loMonth & loDay

lblEndDate.Caption = hiMonth & hiDay

 

Further, set the visibility of the lblBorn, lblStDate, lblAnd, lblEndDate, to True, and visibility of cmdOK to be False.

 

(j)      Finally, add one last command button, rename it cmdGoAgain and

make the caption Go Again.  Click on the Go Again command button to bring up the code window with the cmdGoAgain_Click procedure. Use this event to reset your form so that a user may play more than once.  Use what you know about properties of objects from your previous steps and think about the statements you must include in this event in order to clear the form and bring the OK button back.

 

The program should now run. Save it. Compile it. Try it out. Show it to your friends, your parents, your dog and notice their utter amazement that a computer could be made to do such an amazing feat, a skill once reserved only for astrologers!


Grading Criteria for Zodiac Range

The Zodiac Range program will be graded on the following criteria:

  • Structure and organization of the form, including how attractive the layout is.
  • Operation of the Application – does it work?
  • Completeness – finishing all aspects of the work

 

Part I is due on October 31st  by Noon through electronic submission on the Project Turn In page of the class website.  A hardcopy of your code and a screen shot of your form should be handed in at lecture or to the Information School office by noon.  Your electronic submission will include 3 files:  one with a .vbp extension (e.g. Zodiac.vbp), another with a .frm extension (e.g. frmZodiac.frm) and a third with a .exe extension (an executable of your program, e.g. Zodiac.exe).

Your hard copy (turned in by Noon on October 31st) should include:

          Your name

          Student ID

          Lab Section

          Your form code

          A screen capture of the form

 


Part II:  Day Finder       Due November 7th by Noon

 

The Day Finder is a small extension to the Zodiac program but very significant because it introduces a powerful algorithm, binary search.

 

Searching for a day. The Day Finder is an extension of the Zodiac application in which the user is asked questions that enable the program to say what day he or she was born on. 

 

Leos are known by the Zodiac application to be born between July 23 and August 22. (Remember this includes both the starting and ending dates.) The task is to ask the user questions to determine which day is the user’s birthday. One strategy would be to ask, "Were you born after July 23?" If the answer is "no", then the program can say, "You were born on July 23", but if the answer is "yes", the program has to try another question, like "Were you born after July 24?"

 

If the person is born on August 22, this strategy would take 30 questions to cover the 9 days in July (23-31) and the 22 days in August (1-22), which is actually 31 days, but we don’t have to ask about the last day, since a "no" to "Were you born after August 21" implies an August 21 birthday, while a "yes" implies an August 22 birthday.

 

There is a better way than the one mentioned above. The Day Finder will use an intelligent searching strategy, called the binary search algorithm. Binary search can be used on any ordered objects, like letters or the dates of a Zodiac sign. It uses a series of questions, sometimes called probes, to eliminate half of the remaining possibilities with each question.

 

For example, suppose I’m thinking of a letter. You may ask, "Is the letter between N and Z?", a question that is equivalent to asking if it is in the last half of the alphabet (“Is it AFTER M?”).

 

Regardless of which answer is given, half of the alphabet is eliminated. The binary search for the letter "L" goes as follows: (see next page)

 

Question

 

Is the letter after M?

 

Is the letter after G?

 

Is the letter after J?

 

Is the letter after L?

 

Is the letter after K?

The letter must be L

Answer

 

No

 

 

 

Yes

 

 

 

Yes

 

 

 

No

 

 

 

Yes

Letters Removed

 

A B C D E F G H I J K L M

N O P Q R S T U V W X Y Z

 

 

A B C D E F G H I J K L M

N O P Q R S T U V W X Y Z

 

 

A B C D E F G H I J K L M

N O P Q R S T U V W X Y Z

 

 

A B C D E F G H I J K L M

 N O P Q R S T U V W X Y Z

 

 

A B C D E F G H I J K L M

 N O P Q R S T U V W X Y Z

 

Notice two points about the example:

First, the questions always ask if the letter is after the probe letter. It is possible to ask before and after questions, but always asking the same type simplifies the algorithm.

 

Second, the intervals sometimes have an odd number of letters and sometimes an even number. When it is an odd number, then the middle letter is the probe. When it is an even number, e.g. A-Z, then the probe is the last element of the first half of the interval. This is because of the "after formulation" of the questions. If the questions had been formulated as before questions, then the first element in the second half should be used.  By the way, it takes only five questions to find any letter using binary search on the alphabet.

 

 

Finding a birthday works in the just the same way. The only complication is that the sequence of days changes months under the sign, e.g. Leo goes from July to August. This is not really a problem, however, if we change slightly how we think about the dates.  Imagine that the month didn’t change - then we would look for the birthday from July 23rd through July 53rd. Of course, there is no July 53; that’s what August 22 would be if the month didn’t change.

                   July                                          August                  

 

 

 

 


                                                Leo

 

 

 

 


                                      Leo as extended July Dates

 

The 53 is how far past the end of July the Leo sign goes, which is computed by adding the number of days in July, 31, to the last day of the sign, the 22nd, giving 31 + 22 = 53. This means that the search can look for the birthday between 23 and 53, and whenever a question must be asked about a date larger than 31, the day gets 31 subtracted from it and the month becomes August. Otherwise it is just a normal July date.

 

For example, to figure out the first question to ask, we find the midway point between July 23 and July 53 by subtracting the smaller endpoint from the larger:

 

53 – 23 = 30

 

and dividing by 2

 

30 \ 2 = 15

 

If the number had been odd, the 0.5 would have been truncated (or rounded down). Then this number is added to the start of the Leo sign, the 23rd

 

23 + 15 = 38

 

which is a day in August since it is larger than the largest day in July, the 31st. Thus, July 38 is really August 7th, since 38-31 = 7 (see diagram above). And, it can be checked that this is the middle of the Leo range, just by counting out the days. The result of this computation would be to ask the question:

 

Were you born after August 7?

 

To us, the programmers, this is July 38. If the answer is "no," then the new interval is 23 to 38. Remember, August 7 (= July 38) is still a possibility. If the answer is "yes" then the interval is 39 to 53. By repeatedly shrinking the interval we can reduce the interval size to one day, say 32 to 32. And that must be the person’s birthday.

The questions to find the birthday August 1 are as follows, where Lo is the lower end of the interval, Hi is the upper end of the interval, Mid is the computed midpoint and Size is the size of the interval.

 

 

 

 

 

 

 

 

 

 


Notice that when the endpoints are the same, then they must be for the birthday being sought.

 

 

Form window asking the first question for the Day Finder application.

 

 

Encoding the Binary Search.

 

The objectives of this part of the project are:

  • Create a non-trivial VB application
  • Develop experience with more advanced form manipulation
  • Understand how to control flow through conditional implementation of complex logic
  • Transfer an abstract algorithm (binary search) into a practical program
  • Increase familiarity with the details of binary search, including “end conditions”

 

The Day Finder will be an extension of the Zodiac program.  The first step is to make a copy of the Zodiac form.  Use Save As in the File menu.  This new form will include the features needed to implement a Yes/No dialog that generates probes to find the user’s birthday. To begin this next part of Project 2, add four more variables to the integer declarations at the start of your program:

Dim loMonth As String

Dim hiMonth As String

Dim loDay As Integer

Dim hiDay As Integer

Dim loEnd As Integer    New variable

Dim hiEnd As Integer    New variable

Dim midPt As Integer    New variable

Dim numDays As Integer  New variable

 

Once the declarations are in, each of  the twelve Zodiac option event handlers must be added to.  In each option click event the variable numDays must be assigned the number of days in the loMonth month. This tells the program on what day the month changes. For example, for Leo July is the loMonth, which has 31 days, so numDays = 31 must be added to Leo. 

 

The 2 variables, loEnd and hiEnd will hold the end values of the interval being searched.  MidPt will be used to represent the probe point in the center of the interval and numDays will be the number of days of the month in which the sign begins.  This value will be used as to implement the Extended Month Technique. 

 

 

Overall Logic

 

The Day Finder follows the work of the Zodiac program.  The program then asks a series of questions that implement a binary search over the range set by Zodiac Range until it gets so small as to span only a single day, which is the user’s birthday.  To program such a computation, break the task into a series of parts:

  • Add in additional variable specifications listed above
  • Make form modifications – new buttons and text
  • Program code to set up for the search and ask the first question [OK button click event]
  • Program code in the click events for the Yes/No buttons to ask additional questions [Y, N]
  • Program code for the OK/Yes/No buttons to print out the guess in the right form [OK, Y, N]
  • Program code to reduce the interval by half [Y, N]
  • Program code for the Yes/No buttons to recognize the end of the process and announce the result (the birthday month and day) [Y,N]

 

Notice that except for form modifications (the aesthetics of display) and variable declarations, the steps can all be associated with one or more events, shown in brackets.  This is a feature of Event Based Programming – the whole process is arranged so that when an event like a button click happens, the program takes the proper actions.  Thinking of what that action should be is an easy way to understand the logic.

 


Detailed Logic

 

The following discussion goes into each of the 5 steps in the Overall Logic.

 

a)       Additional Variable Specifications

The click event handler for each sign’s radio button, which already contains assignment statements for loMonth, hiMonth, loDay and hiDay, must have an assignment to numDays, the last day of the month given by loMonth.  For example, for the Leo event handler, the loMonth is “July ”, and the last day of July is the 31st.  So, numDays = 31 should be included in the Leo event handler.  The purpose of keeping track of the last day of the month is so that the lower month can be “extended” as explained above.

b)       Form modifications

The form for the Day Finder adds to the same code used in Zodiac Range.  Add the items (controls) to the form, remembering that they must be invisible initially, and become visible at the time of the OK click.  Setting them invisible to begin with is done by changing their properties; making them visible at the OK Click is done by adding program code statements that assign their visibility property a different value.

c)       Search setup

Once the form’s visibility has been set, the binary search must be set up and the first question asked.  The range will be bounded by loEnd and hiEnd, which should be assigned values based on loDay, hiDay and numDays for each sign and the Extended Month Technique.  Once the interval is initialized, the first question is asked.

d)       Compute the probe (the midpoint)

Questions will be asked in OK (the first question) and Yes and No events.  To find the probe requires the midPt to be calculated as noted above.  It is important to re-emphasize that when dividing by 2 to compute the midPt, the integer division symbol (\) should be used rather than the normal division symbol (/).  Why?  Because with the normal division symbol the results may include decimals.  Nobody notes their birth date on half a day, or .3 of a day, so we use the integer division symbol to make sure the result is always a whole number.

e)       Printing out the guess questions

The guess must be printed as “Were you born after ” month day ?  The midPt value, which ranges over the Extended Month, can be used to determine the proper values for month and day.  If midPt is less than or equal to numDays, then the month is the lower month (loMonth), otherwise it is the higher month.  Also, if midPt is less than numDays, then midPt is the proper value for day.  Otherwise, by the Extended Month Technique, midPt is too large by the amount of numDays, which must be subtracted off to get the right day value (and the month will be the higher month).  To hook together the month word and the day number to form the guess to be displayed, use the concatenate operation (&) which is used to put two letter strings together.  (The number will be converted to a letter string.)  Assigning the result to the caption property of the label control used for guessing (introduced in (b) above), prints out the guess and waits for the user to respond by clicking Yes or No.

f)       Reducing the interval by half

When the user clicks Yes or No, he or she has given birthday information and the interval must be reduced before the next question is asked.  Since the questions are “after” questions, a Y reply means that the lower half of the interval can be discarded.  So, the loEnd should be moved up to just beyond the midPt, i.e midPt + 1.  IF the reply is N, then the upper part of the interval is to be discarded, and hiEnd should be moved down to midPt. 

g)       Recognizing the birth date (termination of the binary search)

Any binary search ends when the interval has a size of 1, since that must be the item we are looking for.  At that point the label “Your birthday is ” should replace the “Were you born after “ label, and the day printed out.

h)       Explain how it works

Finally, write a paragraph explaining how the binary search works.  Write it as though you were explaining it to a friend who has not yet learned any programming, but with enough detail that he/she understands the entire process.  Include discussion of the main ideas of the algorithm and how it has been converted into the specific program for finding a birthday given the Zodiac sign.

 

Grading Criteria for Day Finder

The Day Finder program will be graded according to the following criteria:

  • The organization of the form revisions (aesthetics)
  • Operation of the Application – does it work and get the right answer?
  • Is the logic clearly specified?
  • Completeness – do all aspects of the computation work?
  • Have control (item) names been chosen thoughtfully?
  • Is the write-up clear and comprehensive?

 

Part II is due on November 7th by Noon through electronic submission on the Project Turn In page of the class website.  You will turn a hard copy of your write-up, a screen shot of your form and your code before Noon as well.

 

Your electronic submission will include 3 files:  one with a .vbp extension (e.g. DayFinder.vbp), another with a .frm extension (e.g. frmDayFinder.frm) and a third with a .exe extension (e.g. DayFinder.exe).  If you have added any images to your form and you want us to be able to see them, the executable is absolutely essential.

 

Your hard copy (turned in by Noon on November 7th) should include:

          Your name

          Student ID

          Lab Section

          Your write-up

          Your form code

          A screen capture of the form