Unit III Practice/Learning

CS 1130 Visual Basic — Fall 2017

Overview

We have just seen some IPO problems—situations where the program accepts some data from a user, does some manipulation of that data, and reports the result. IPO is one kind of code that can make up a program. A closely related kind of problem element involves having the computer ask/answer a question, e.g., Am I obese? Is overtime pay needed for this person? Should the traffic light change color? Is this a leap year? Did I win my bet in the dice (craps) game? Obviously, there are lots of questions that can be asked. An essential part of programming requires the identification of data and relationships among the data that allows the computer to answer such questions.

Unit III mostly addresses problem representation that deals with being able to formulate questions of the data elements of the problem. This unit also addresses one of the competency/mastery demonstrations. That demonstration will require evaluation (Part I) and production (Part II) of conditional expressions to demonstrate your understanding.

I encourage you to work with a partner, either using pair programming (one person types and one person watches for mistakes and swap roles every 30 minutes or so) or working side by side on separate computers. This way each person interacts with the code on each item—the only way to learn the material.

Part I — Evaluating Conditional Expressions

Examine the items below and indicate the result of the conditional expression given the value or values indicated. Note that each conditional has several values or sets of value for which you are to determine the result (True or False). You may want to print that page out and compare your results to a classmates. You could also enter them into a VB project to check the results if you wished.

  1.   number > 0  Is the value positive?
    number 0T / F number -1T / F
    number 1T / F number 0.000000001T / F
  2.   number < 0  Is the value negative?
    number 0T / F number -1T / F
    number 1T / F number -0.000000001T / F
  3.   hours > 40  Is overtime pay called for?
    hours 0T / F hours 40T / F
    hours 41T / F hours 50T / F
  4.   income > expenses  Did we make a profit this week?
    income 12345expenses 10000 T / F income 10000expenses 10000 T / F
  5.   current > greatest  Is "this" value greater than others seen thus far?
    current 99greatest 80 T / F current 99greatest 100 T / F
    current 99greatest 99 T / F current 99greatest 999 T / F
  6.   current >= greatest  Is "this" value as big as any seen thus far?
    current 99greatest 80 T / F current 99greatest 98 T / F
    current 99greatest 99 T / F current 99greatest 100 T / F
  7.   current = target  Is this the value I am searching for?
    current appletarget pear T / F current appletarget orange T / F
  8.   gender ="F"  Is this person female?
    gender M T / F gender F T / F
    gender f T / F gender FEMALE T / F
  9.   isFemale  Is this person female?
    isFemale True T / F isFemale False T / F
  10.  Not isMaleIs this person female?
    isMale True T / F isMale False T / F
  11.   score >= 0 And score <= 100  Is the test score a valid percentage value?
    score -1 T / F score 0 T / F
    score 100 T / F score 101 T / F
  12.   score < 0 Or score > 100  Is the test score outside the range of valid percentage scores?
    score -1 T / F score 0 T / F
    score 100 T / F score 101 T / F
  13.   age >= 9 And age < 13  Is this person a "pre-teen"?
    age 8 T / F age 9 T / F
    age 12.5 T / F age 13 T / F
  14.   hours >= 30 And hours < 60  Is this student a sophomore?
    hours 29 T / F hours 30 T / F
    hours 59 T / F hours 60 T / F
  15.   hours > 120 And gpa >= 2.0 And lacDone  Can this student graduate?
    hours 119gpa 3.72lacDone True T / F hours 120gpa 2.0lacDone False T / F
    hours 142gpa 1.99lacDone True T / F hours 142gpa 1.99lacDone True T / F
  16.   Not rain and temp >= 60 and temp <= 90  Is it a good day for golf?
    rain Truetemp 87 T / F rain Falsetemp 60 T / F
    rain Falsetemp 59 T / F rain Falsetemp 61 T / F

Part II — Generating Expressions

Create a new form in your practice project (or create a whole new practice project) that allows you to accept three or four values in text boxes, two or three radio button in a group box, two or three check boxes, and some buttons to use to test your code. Then determine/identify/formulate the problem elements (indicated below) that are required to answer the provided questions. The suggested activity is that you identify:

  1. data values that should originate with/from the user with either a text box or by using an input box.
  2. additional data values that should be built into the program
  3. assumptions made when the expressions were developed, e.g., grade levels/cutoffs, point at which overtime pay should be made, etc.

And then create an expression that answers a yes/no question, i.e., produces a value or True or False perhaps assigning the result to a variable. Finally, report the result using a message box. Any manipulations (calculations, function calls, etc.) needed should be included as part of your code. You may well need to look up information to identify the necessary data elements and their relationships with respect to the question. A couple examples are provided.

The practice exercises

Choose which of the form elements/controls is most appropriate for the problem and use it/them for input from the user. Note: if there are not enough numeric or string text boxes, you can use the other kind—the only difference is the label and the name, all text boxes will accept either kind of data.

Numerics (mostly)

  1. Input is numeric? (There is a function for this IsNumeric().)
  2. Am I passing the course? (Assume a 90, 80, 70, etc. grading system)
  3. Is it below freezing outside? (Use whatever temperature scale you are familiar with.)
  4. Do I have enough money?

    (Input values will be original cost, percent off, and available money. Tax rate can be hard coded (in a variable or as a literal value).

  5. Am I getting a B in the course? (Assume a 90, 80, 70, etc. grading system)
  6. First is smaller?

    Is the first input number less than the second input number. (Test it with both integer and double values.)

  7. Blood pressure worrisome?

    Declare a variable and set its value to answer the question, "Does the blood pressure reading indicate a possible problem?" Blood pressure could indicate a problem if the systolic value is above 140 or the diastolic value is above 90.

  8. Normal height?

    Declare a variable and set its value to answer the question, "I'm an American male, is my height normal?" The normal range for height of American males is 69.5 inches ± 3 inches.

  9. Card a 10?

    Assume you have a card value (0-51) and what to check to see if it is a ten. Numbers 0-12 represent spades (ace to king), 13-25 diamonds (ace to king), 26-38 clubs, (ace to king), and 39-51 hearts (ace to king).

  10. Rolled the point (in craps)?

    The "point" value can be entered via one of the text boxes and be 4, 5, 6, 8, 9, or 10. Generate random values for the roll (two dice each with value 1-6) and check to see if the roll is equal to the point.

  11. Draw IPERS?

    To draw IPERS (Iowa Public Employees Retirement System) retirement one must no longer work for an IPERS employer and meet one of three conditions for age and service: sum of age and years of service equals or exceeds 88; age is at least 62 and years of service is at least 20; age is 65 or more. Use an input box to ask the user if they s/he is still working for an IPERS employer.

  12. Your problem?

    Make up your own question involving numeric data?

Strings (mostly)

  1. Title length okay?

    Assume it can have up to 50 characters.

  2. Abstract length okay?

    Assume it must have at least 75 character but no more than 200 characters.

  3. Guessed letter in hangman word?

    Hangman is a game in which a blank for each letter in a word is shown and the user repeatedly guesses a letter that might be in the word. If it is, the program shows the letter, if not the user makes progress toward being hanged (losing). This item checks to see if the guessed letter is in the word.

  4. First is smaller?

    Is the first input string less than the second input string. (Test it with upper and lower case and with digits or numbers.)

  5. String has two spaces in a row?
  6. First word is first (alphabetically)

    Assume two inputs.

  7. String contains a sentence?

    Sentences end with a period, an exclamation point, or a question mark.

  8. String contains a vowel

    You can check both upper and lower case or use the .ToLower() or .ToUpper() method to check both simultaneously.

  9. URL is in the ".edu" domain?
  10. Is the e-mail address for an educational institution?

    It would have a domain of .edu>.

  11. User entered a reasonable e-mail address?

    A valid e-mail address contains "something", an @ sign, "something", a period or dot, and "something".

  12. Your problem?

    Make up your own question involving string data?

Others (mostly)

You will need to decide how to to get input from the form (if needed) to complete the following items.

  1. Coin flip is heads?

    Decide what heads means and use Rnd() to answer the question.)

  2. Eligible for the scholarship?

    Must be Native American, have a GPA of at least 3.2 GPA, and an ACT score of 21 or more.

  3. Choose door 2?

    There will be three possible values (1, 2, or 3). Use Rnd() to generate a value in the range [0..1), then test the value for being in the middle third of the range.

  4. Background color contain any red?

    (Me.BackColor.R will return an indicator of the red value of the background color. It will be a number in the range 0 to 255. Zero indicates none of the color is included.)

  5. Does the form's background color contain all the colors?

    (use Me.BackColor.R, Me.BackColor.G, Me.BackColor.B )

  6. Does the button have the same background color as the form? (Use any button you wish.)
  7. Is high school the highest level of education?
  8. Did the person complete a doctorate's degree?
  9. Did the person complete any post-baccalaureate degree?
  10. Did the person indicate completion of any level of education?
  11. Is the person of Hispanic descent?
  12. Is the person of Black or Native American descent?
  13. Did the person indicate and type of descent?
  14. Your problem?

    Make up your own question involving something besides just numeric and string data?