Hi, I am making a program where I am to ask the user a series of questions and add up the amount they get right blah blah....

I have to ask them the area of a square, the perimeter of the square along with other shapes....

So I was going to have a square class, rectangle class etc etc...

These classes would then all inherit the scoring class which holds the variables which will be incremented when a question is right (or the other way round).

What I am stuck on though is a way in which when the user inputs their answer...does anyone have an idea how I could take this answer and make sure that it is passed to the correct shape class and the answer is checked against the correct method?

I think it will involve taking the answer from the textbox and sending it in to the shapes constructor, not sure how I would make sure I send it to the correct one though?

Hope that makes sense! I am not asking anybody to do anything for me, just whether they have any ideas about how to implement it!

Thanks
James

So, are you talking about some sort of math tutor program, with questions about shapes?

I'd make an interface or abstract question class. Let's go with interface for now:

interface IQuestion {
    String Text { get; }
    Boolean CheckAnswer(String ans);
}

Then you make up some question classes:

class RectangleQuestion : IQuestion {
    Random r = new Random();
    int answer;
    public String Text {
        get {
            int length = r.Next(1,10);
            int width = r.Next(1,10);
            answer = length * width;
            return String.Format("What is the area of a rectangle that is {0} feet wide and {1} feed in length", width, length);
        }
    }

    public Boolean CheckAnswer(String ans) {
        int ansValue;
        Boolean result = false;
        if (Int32.TryParse(ans, out ansValue)) {
            if (ansValue == answer) {
                result = true;
            }
        }

        return result;
    }
}

Once you have a bunch of these type of classes you create a Question Factory that returns a random one of these classes which you can store in a variable of type IQuestion. Now you use the interface members to set the question text on your form and in the 'submit' button handler you pass the textbox to the question object CheckAnswer method and do what you need based on the true/false value returned.

In response to your question ddanbe, you could consider it an e - learning program that asks questions about some shapes and also NAND gates....

Momerath, thank you...I understand what you are saying, thanks, what I want to do though is to have more than one question...so say I have a circle class, rectangle class, square class....

What I then wish to do is randomly grab a question so that every time the user opens the program, a different set of questions will be provided rather than the same ordered set.

Thanks
James

That's what the Question Factory would be for (as I mentioned above). It would randomly select a class and return it to your application. You'd call it as many times as you need (either as needed or create a list of returned classes). Your main code won't care what the questions actually are as they only deal with the interface. You'll be able to add new question classes and not have to modify the main routine.

Using reflection you wouldn't have to modify the Question Factory either, but that might be more complicated that you wish to get into right now :)

Thanks, im not sure I explained it right, what I meant was I want to grab a random question from a class, as each class would hold more than one question?

Just modify the Text property. Currently each time you access it the question parameters change. You could modify it to return a different question (area, perimeter, etc) and as long as you set answer to the correct value, you are golden. For example:

public String Text {
    get {
        String value;
        int length = r.Next(1,10);
        int width = r.Next(1,10);
        if (r.Next(2) == 0) {
            answer = length * width;
            value = String.Format("What is the area of a rectangle that is {0} feet wide and {1} feet in length", width, length);
        } else {
            answer = 2 * (length + width);
            value = String.Format("What is the perimeter of a rectangle that is {0} feet wide and {1} feet long", width, length);
        }
    }
}

If you have more questions then it would probably be better to put them in their own methods and use switch/case to select

public String Text {
    get {
        switch (r.Next(10)) {
            case 0 : return QuestionType0(); break;
            case 1 : return QuestionType1(); break;
            case 2 : return QuestionType2(); break;
            ... etc ...
        }
    }
}

private String QuestionType0() {
    int length = r.Next(1,10);
    int width = r.Next(1,10);
    answer = length * width;
    return String.Format("What is the area of a rectangle that is {0} feet wide and {1} feet in length", width, length);
}
commented: Good work! +8

A problem with the method used above is you can't implement adaptive questioning where you ask particular types of questions that the student is having issues with. You always get a random question (which makes the first way better, where one class is one question type. The QuestionFactory can bias question types as you see fit).

Thanks Momerath! you have given me some really good ideas..I am not going to go along the Interface route as I have not gotten that far yet...

I am going to work along the lines of having my base class...and my question classes which will inherit this base class will use as you say a switch statement to return the method chosen from a random number...it will then display that question and check for correct or wrong answer...the result of which will then be placed in another class and at the end of the test a print out of all these results will be displayed...

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.