Division is by far the hardest part of the assignment. Now that you have struggled with it for awhile, lets talk through the process of how you would handle it.
The code for this example can be found at https://github.com/spconger/ThirdGradeMath
First you should identify what the unique elements of division are. Remember we are doing third grade level, integer division.
* For one the first number should be bigger than the second
* we want to avoid 0 in the divisor or the dividend
* because we are using integer division we must handle remainders
First we add a division method and a remainder method to the BasicMath class
You should add some error checking to these methods
Next we add a division button and a textbox called "txtRemainder" to the WebForm. Double click the button to get the click event. Then we call the GetNumbers() method just like the other operators:
We are going to have to make some alterations to the GetNumbers() method, but first lets create a new method that makes sure the first number is bigger than the second number.
We need to make other changes.
Number1 and Number2 need to be declared at the class level
Then we need to change GetNumbers() so that it looks like the code below. Basically, we need to check to see if the operator is subtraction or division, then we need to check if the first number is less than the second one. If it is we call our swap method. The other main difference is that we now assign the random numbers to the variables number1 and number2 rather than directly to the labels. We test them first and then assign them to the labels.
We also have to make some changes to the check answer method. Division needs to be handled differently because of the remainder issue.
So at the top of the CheckAnswer method we add the following if statement:
Next we will create a new check for division. We need to make sure both the answer and remainder session variables exist. Then I use a try parse to check that the users answers are valid. Here is the complete check Division method:
Here is the button click method for division:
No comments:
Post a Comment