Unit IV Practice (If statements)

CS 1130 Visual Basic

Overview

Our familiarity with creating Boolean expressions (Unit III) allows us to move on to using them to control which actions occur in a program. We will use some of the Unit III exercises to address If statements. Also, we will provide some larger problem contexts in which require the use of If statements to achieve their goals. As you work on these exercises, be thinking of how some of them might apply to the project you are considering. Have fun.

The notes directory contains a zipped VB project that you can use to enter and test code for this assignment. Remember that programming practice activity can be checked by the computer—it it produces the answer you expect, your code is probably correct.

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.

Arithmetic Drill

VB form for conducting arithmetic drill and practice

Use the practice project's arithmetic drill form like that in the image at the right. You will want to examine the form to see the names of the various controls to produce appropriate code. Supply code for the following tasks.

  1. Operation identification

    When the form loads use an input box to ask the user if they wish to do A (addition), S (subtraction), M (multiplication), or D (division). Based on the response, change the Text properties of the lblTitle and the lblOperator to indicate the chosen operation. Use a class variable to save/indicate the operation. When checking the operation, check for both upper and lower case letters (or use a copy of the input that is converted to upper or lower case).

  2. Problem generation

    Also in the form-load subroutine, generate two random numbers between 0 and 10 (including both end points). Then assign appropriate values to class variables for the first value, second value, and answer. Name the variables as you wish. For subtraction & division, the "answer" will be one of the two random numbers. Consider producing a new subroutine to accomplish this task.

  3. Answer checking

    Supply code for the "Check Me" button. Notify the user if nothing was entered. If something was entered before the button was clicked check to see if the value entered matches the "answer" and notify the user about the correctness. Consider producing a new subroutine (perhaps returning True or False to accomplish this task.

Pepe's Pizza App

Use the practice project's pizza app form like that in the image at the right. You will want to examine the form to see the names of the various controls to produce appropriate code. Supply code for the following tasks.

VB form for a pizza ordering app

We imagine the form will be used by a server who "clicks" on the various radio buttons and button buttons to indicate the pizzas to be produced. The update button will normally cause a pizza description to be inserted in the list box (named lstOrder) and clear the form so another pizza could be added to the list. If, however, one of the items in the list is selected the update button will delete it from the order. The submit button finalizes the order.

  1. Flag for "okay"

    Provide code that will declare a boolean status variable named orderOkay and set it to True. Declare a string variable called order and initialize it to the null string. (Other parts of your code will use them. The code we are writing here would go in the subroutine that handles a click of the "Update the Order" button.)

  2. Check for size selection

    Produce one or more If statements that will either make the order be 8" or 12" or 16" or 20" or set orderOkay to False depending on which radio button (or none) is checked. (To get the quotes to be part of a string I think you "escape" the quote by duplicating it, e.g., order = "12"" " should result in the variable containing 12".)

  3. Check for crust selection

    Produce one or more If statements that will either "add" to order or set orderOkay to False depending on which radio button (or none) is checked. Assuming rdo12 and rdoHand were checked, the value for order would be 12" Hand Tossed.

  4. Check for toppings selection

    Produce one or more If statements that will either "add" to order or cause a new form allowing topping selections to appear or set orderOkay to False depending on which radio button (or none) is checked. The new thing here is when the user selects "Build Your Own" (rdoCustom is checked) the code would normally open a window that allowed a variety of toppings to be chosen. For now, instead of doing that just put a comment in the if statement.

  5. Check for deletion from order

    If the list box contains one or more values the user can click on one to select it. If that happens the SelectedIndex property of the list box will indicate the position in the list of the selected item (numbering starts a 0). If nothing is selected the SelectedIndex property will have a vale of -1. To delete an item from a list, you use the RemoveAt() method. For example, is we have a variable pickedItem and it has a value of 3, then lstOrder.Items.RemoveAt(pickedItem) would cause the 4th item (the one in position 3) to be removed from the list.

    Produce one or more If statements that will do nothing if one of the sets of buttons did not have something selected, that will remove the selected item from the list (if an item was selected), or that will add the current order to the list. Only one of the three things should occur.

Miscellaneous Problems

VB form for performing various if statements

Use the practice project's miscellaneous form like that in the image at the right for these exercises. You will want to examine the form to see the names of the various controls to produce appropriate code. Supply code for the following tasks. Use a message box to report the results.

Extra Credit

If you wish to submit something for extra credit download the blank projects and do the assignment in them. Then supply something that goes beyond the assignment. You might identify additional miscellaneous problems involving if statements. For the pizza app, it might include adding pricing information and/or preparing a receipt and storing it in a file (perhaps titled using the table value and a value for the current date & time. Or, you might do something else; impress me.