Step 6: Add a Subtraction Problem

To add a subtraction problem, you need to:

  • Store the subtraction values.

  • Generate random numbers for the problem (and be sure that the answer is between 0 and 100).

  • Update the method that checks the answers so that the method checks the new subtraction problem too.

  • Update your timer's Tick event handler so that the event handler fills in the correct answer when time runs out.

To add a subtraction problem

  1. First, you need a place to store the values, so add two ints (Integers) for the subtraction problem to your form. The new code appears between the addition integers and the timer integer. The code should look like the following.

    Public Class Form1
    
        ' Create a Random object to generate random numbers.
        Private randomizer As New Random
    
        ' These Integers will store the numbers
        ' for the addition problem.
        Private addend1 As Integer
        Private addend2 As Integer
    
        ' These Integers will store the numbers
        ' for the subtraction problem.
        Private minuend As Integer
        Private subtrahend As Integer
    
        ' This Integer will keep track of the time left.
        Private timeLeft As Integer
    
    public partial class Form1 : Form
    {
        // Create a Random object to generate random numbers.
        Random randomizer = new Random();
    
        // These ints will store the numbers
        // for the addition problem.
        int addend1;
        int addend2;
    
        // These ints will store the numbers
        // for the subtraction problem.
        int minuend;
        int subtrahend;
    
        // This int will keep track of the time left.
        int timeLeft;
    

    Note

    The names of the new ints—minuend and subtrahend—aren't programming terms. They are the traditional names in arithmetic for the number that's being subtracted (the subtrahend) and the number being subtracted from (the minuend). The difference is the minuend minus the subtrahend. You could use other names, because your program doesn't require specific names for ints, controls, components, or methods. There are some rules (for example, names can't begin with digits), but in general, you could use names such as x1, x2, x3, x4, and so on. But it would be difficult to read the code, and nearly impossible to track problems. You will use the traditional names for multiplication (multiplicand × multiplier = product) and division (dividend ÷ divisor = quotient) later in this tutorial.

  2. Next, modify the StartTheQuiz() method to fill in a random subtraction problem. The new code follows the "Fill in the subtraction problem" comment. The code should look like the following.

    ''' <summary>
    ''' Start the quiz by filling in all of the problems
    ''' and starting the timer.
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub StartTheQuiz()
    
        ' Fill in the addition problem.
        addend1 = randomizer.Next(51)
        addend2 = randomizer.Next(51)
        plusLeftLabel.Text = addend1.ToString()
        plusRightLabel.Text = addend2.ToString()
        sum.Value = 0
    
        ' Fill in the subtraction problem.
        minuend = randomizer.Next(1, 101)
        subtrahend = randomizer.Next(1, minuend)
        minusLeftLabel.Text = minuend.ToString()
        minusRightLabel.Text = subtrahend.ToString()
        difference.Value = 0
    
        ' Start the timer.
        timeLeft = 30
        timeLabel.Text = "30 seconds"
        Timer1.Start()
    
    End Sub
    
    /// <summary>
    /// Start the quiz by filling in all of the problems
    /// and starting the timer.
    /// </summary>
    public void StartTheQuiz()
    {
        // Fill in the addition problem.
        addend1 = randomizer.Next(51);
        addend2 = randomizer.Next(51);
        plusLeftLabel.Text = addend1.ToString();
        plusRightLabel.Text = addend2.ToString();
        sum.Value = 0;
    
        // Fill in the subtraction problem.
        minuend = randomizer.Next(1, 101);
        subtrahend = randomizer.Next(1, minuend);
        minusLeftLabel.Text = minuend.ToString();
        minusRightLabel.Text = subtrahend.ToString();
        difference.Value = 0;
    
        // Start the timer.
        timeLeft = 30;
        timeLabel.Text = "30 seconds"; 
        timer1.Start();
    }
    

    This code uses the Random class Next() method a little differently. When you give it two values, it picks a random number that's greater than or equal to the first one and less than the second one. The following line chooses a random number from 1 through 100, and stores it in minuend.

    minuend = randomizer.Next(1, 101)
    
    minuend = randomizer.Next(1, 101);
    

    The Random class Next() method can be called in several ways. When you can call a method in more than one way, that's called an overloaded method, and you can use IntelliSense to explore this. Take another look at the IntelliSense window tooltip for the Next() method.

    Intellisense window tooltip

    Next method

    Notice how the tooltip shows (+ 2 overload(s)). This means that there are two other ways that you can call the Next() method. When you type the new code for the StartTheQuiz() method, you can see more information. As soon as you type randomizer.Next(, the IntelliSense window opens. Press the UP ARROW and DOWN ARROW keys to cycle through the overloads, as shown in the following picture.

    Intellisense window overloads

    Intellisense window overloads

    The one in the preceding picture is the one you want, because it lets you specify a minimum and maximum value.

  3. Modify the CheckTheAnswer() method to check for the correct subtraction answer. The code should look like the following.

    ''' <summary>
    ''' Check the answer to see if the user got everything right.
    ''' </summary>
    ''' <returns>True if the answer's correct, false otherwise.</returns>
    ''' <remarks></remarks>
    Public Function CheckTheAnswer() As Boolean
    
        If addend1 + addend2 = sum.Value AndAlso 
           minuend - subtrahend = difference.Value Then
    
            Return True
        Else
            Return False
        End If
    
    End Function
    
    /// <summary>
    /// Check the answer to see if the user got everything right.
    /// </summary>
    /// <returns>True if the answer's correct, false otherwise.</returns>
    private bool CheckTheAnswer()
    {
        if ((addend1 + addend2 == sum.Value)
            && (minuend - subtrahend == difference.Value))
            return true;
        else
            return false;
    }
    

    The && is the Visual C# logical and operator. In Visual Basic, the equivalent operator is AndAlso. It's the same as saying, "If addend1 plus addend2 is equal to the value of the sum NumericUpDown, and if minuend minus subtrahend is equal to the value of the difference NumericUpDown." The CheckTheAnswer() method only returns true if the addition problem is correct and the subtraction problem is correct.

  4. Change the last part of the timer's Tick event handler so it fills in the correct answer when time runs out. The code should look like the following.

    Else
        ' If the user ran out of time, stop the timer, show
        ' a MessageBox, and fill in the answers.
        Timer1.Stop()
        timeLabel.Text = "Time's up!"
        MessageBox.Show("You didn't finish in time.", "Sorry")
        sum.Value = addend1 + addend2
        difference.Value = minuend - subtrahend
        startButton.Enabled = True
    End If
    
    else
    {
        // If the user ran out of time, stop the timer, show
        // a MessageBox, and fill in the answers.
        timer1.Stop();
        timeLabel.Text = "Time's up!";
        MessageBox.Show("You didn't finish in time.", "Sorry");
        sum.Value = addend1 + addend2;
        difference.Value = minuend - subtrahend;
        startButton.Enabled = true;
    }
    
  5. Save and run your code. Your program should now have a subtraction problem, as shown in the following picture.

    Math quiz with subtraction problem

    Math quiz with subtraction problem

To continue or review