Math Quiz Application - Part 1: Requirements Analysis & Post Design


 I was chatting with a friend online, as I am prone too do often, while coding and the topic of his high school visual basic 5 class came up.  He told me how he was the top student in his class flying through all the simple code projects that had been assigned when the teacher, whom I will call DBT from here on out, gave him a code problem to solve.  Reviewing the problem solution there is no way to take what was being taught in his class at the time and complete the task effectively.  This is why:

The problem: Make an application that posses a math problem to the end user, allows the end user to answer the problem, receive a confirmation of the answers correctness, and at the end of the "quiz" have a tallied score percentage.

Well of course I want to pick this veritable challenge up, it should be easy enough after all.

First we need to analyze the system requirements.  Upon further investigation I learned the system has the following specific requirements.

  1. Generate a random set of numbers from 0 to 999 for variable x and variable y (e.g. x = 1; y = 1; x + y = 2 ).
  2. Generate a random arithmetic operator using only the following operators: (and of course further reading because researching the subject matter your involving yourself in does tend to help, even if it is a refresher)
    1. Addition: Further Reading
      1. Symbol: plus sign '+'
      2. Also referred to as 'add'
      3. We will not be performing problems outside of the basic addition of positive natural numbers.
      4. At it's base addition is the process of combining two separate quantities to make one.
    2. Subtraction: Further Reading
      1. Symbol: minus sign '-'
      2. We will not be performing problems outside of the basic subtraction of positive natural numbers.
        1. There was no specification for negative sums (e.g. 1 - 2 = -1) a supposition that x must always be greater than y must be accounted for.
      3. At it's base subtraction is the process of removing a specified quantity from a unit or collection to be left with less than what was started with.
    3. Multiplication: Further Reading
      1. Symbol: 'x', '.'(dot), '*', or x(y), or xy are all appropriate expressions for multiplication.
        1. we will use the more modern expression of the Astrix symbol.
        2. Consider giving an options selection for the user to choose which best suits their own preference of symbol within the range of appropriate arithmetic symbols.
      2. We will be performing multiplication on positive natural numbers only.
      3. Multiplication is the process of scaling one unit by another unit (e.g. 2 apples multiplied by 2 is 4, or adding 2 apples 2 times)
    4. Division: Further Reading 
      1. Symbol: '/', '÷', x/y
        1. We will use the computer expression generally denoted as '/' a forward slash.
      2.  Division is the process of repeatedly subtracting one unit by another unit.
  3. Use variable x = (Random Number 0 to 999), arithmetic operator, and variable y = (Random Number 0 to 999) to generate the full question and calculate for the answer.
  4.   Compare the user answer to the calculated answer of variable z.
    1. If the user has answered the problem do not allow them to answer the problem a second time.
    2. If the user has gotten the answer correct, add the answer to the total score percentile.
    3. If the user has gotten the answer incorrect, subtract the answer to the total score percentile.
See now on the base this is fairly simple.

The first thing we need to ask ourselves is what is a question?  The solution is relatively simple.
  • A question is a series of combined text in a logically constructed form that poses a request for an answer.
  • A question in a collection of questions has a number to identify itself from all other questions in the collection.
  • A question has a correct answer to be solved for.
  • A question has a response that can be either true or false (In the case of basic arithmetic there are only write or wrong answers)
  • Until a question is answered it is unanswered. (this seems unimportant to mention, however if we are checking to see if the question has already been answered to prevent the answer from being corrected, then answered correctly we need to keep in mind that this is in fact a state of the question: answered & unanswered).
  • The question does not change unless being edited by the creator of the question.
  • the answer to the question does not change unless being edited by the creator.
Further basic arithmetic questions have additional properties.
  • An arithmetic question has an x value.
  • An arithmetic question has a y value.
  • An arithmetic questions answer in our program will be denoted by z as a numerical comparison of the users response.
  • An arithmetic question has an arithmetic operator such as +, -, *, or /.
  • An arithmetic question must solve for z.
  • An arithmetic question has only one answer, unless x or y values are changed prior to being answered. (as our program will auto-generate these values there is no need for outside editing.
 For the sake of logistics we will create the following constant values in the program to prevent errors on the programmatic syntax (grammar errors).

An enumeration or static class representation of the arithmetic operators.

Enum ArithmeticOperators
  1. public enum ArithmeticOperators
  2. {
  3.     Add = 1,
  4.     Subtract = 2,
  5.     Multiply = 3,
  6.     Divide = 4
  7. }


In the case of the this enumeration we are adding the flags values in order to allow the program to randomly pick which operator to use.

Next we will examine the skeleton outline of our basic arithmetic operators functions

Add Method
  1. public static int Add(int x, int y)
  2. {
  3.     int z = x + y;
  4.  
  5.     return z;
  6. }

The structure is the same for each arithmetic operator, replace the appropriate operator and name in place of Add and +. (e.g. Subtract(x, y), Multiply(x, y), Divide(x, y)).

Next we need the question skeleton:

Code Snippet
  1. public class Question
  2. {
  3.     private string _id; // the id or question number.
  4.     private string _text; // the text that comprises the question.
  5.     private string _answer; // the text that comprises the answer.
  6.     private int _x; // variable x
  7.     private int _y; // variable y
  8.     private int _z; // variable z
  9.  
  10.  
  11.  
  12.     public int Number { get { return _id; } }
  13.     public string Text { get { return question; } }
  14.     public string Answer { get { return answer; } } // returns the correct answer, in this case z
  15.     public int X { get { return RandomValue; } }
  16.     public int Y { get { return RandomValue; } }
  17.     public int Z { get { return SolveForXY(x, y, ArithmeticOperators.ArithmeticOperator); } }
  18.  
  19.     private int RandomValue()
  20.     {
  21.         // Generate random number between 0 and 999 using the Random() class
  22.         int randomNumber = Random().Next(0, 999);
  23.         return randomNumber;
  24.     }
  25.  
  26.     private int SolveForXY(int x, int y, ArithmeticOperators @operator)
  27.     {
  28.         // in the code I will use a switch case as I see them as cleaner and more effective.  
  29.         //if operator is add then Add(x, y)
  30.         //if operator is subtract then Subtract(x, y)
  31.         //if operator is Multiply then Multiply(x, y)
  32.         //if operator is Divide then Divide(x, y)
  33.     }
  34.  
  35. }

To create a list of questions we need to use an array. I will use List<T> in C# to accomplish this as it is the easiest way to perform the operation, you can also use the base class Question as an array of itself (e.g. Question[] questions = new Question[(Number of questions to generate)];).

This concludes Math Quiz Application - Part 1.

Next post will go in depth into the class functionality and additional problems that arose from the initial analysis, design, code, and testing of the project.