The class then typed in code for the “Add” button in which it calculated the sum of two numbers in the text fields but crashed when one of its input is numeric. This issue was addressed in the String Operations example in which the class learned how to check if the input in the text fields are numeric and how to combine two or more strings. These examples can be found on the school network drive under S:\Handout\Programming 11-12. Note: You must copy the folders onto your desktop or your storage device. You will not be able to save and run from the read-only network drive. Assignment: You will modify the Math Operations example that meets the following criteria
- The results can be decimals. For example, if 12.1 and 23.4 are entered into the text fields, the result is 35.5.
- The program adds, subtracts, multiplies and divides numbers. It then displays the result in a MsgBox.
- The text in the MsgBox displays a sentence. For example, if 12 and 13 are entered, it displays “The sum is 25.” rather than just “25”. To do this, review how to join strings in the the String Operations example.
- If one of the inputs in non-numeric (i.e. blank or text), then the MsgBox displays “Enter numbers only.”. To do this, review the IsNumeric function in the String Operations example.
- The text in the MsgBox displays “Unable to divide.” if the second number is zero when dividing. To do this, the input has to be a number and then check if it is zero. There are two ways to check:
If (num2 <> 0) Then normal division Else error message End If
If (num2 = 0) Then error message Else normal division End If
- Proper comments and well-labeled names (2 marks)
- Add, subtract, multiply and divide (4 marks)
- Provides correct answers.
- Includes decimal inputs and outputs.
- Exceptions (2 marks)
- Division by 0
- Visual Design (2 marks)
- Displays “The result is ___.” with a period.
- No spelling, grammar or punctuation errors in MsgBox.
- Visually nice and easy to use.
- Take time to edit the “Add” button properly so that it displays the result in a sentence and handles non-numeric inputs properly. After this is done, copy/paste and modify the other three arithmetic operations.
- To display a number with a string in a message box, you need to type:
MsgBox("The sum is " + sum.ToString())
- To check if your text fields are numeric, you need to type:
' Create variables Dim num1 As Decimal ... If (IsNumeric(Input1.Text) and IsNumeric(Input2.Text)) Then ' Store into variables num1 = Input1.Text ... ' Calculate and display result Else ' Display error message End If
- Handle the “division by zero” exception last. Hint: Both inputs must be numbers before you can check if the second input is zero.