Saturday, July 23, 2011

Q and A System - Take 2

I have spent the past few days revamping the Q and A system. I have created new classes, made others obsolete, and created a much more flexible and powerful interface than the prior system. I will walk you through this system so that you can understand exactly how it does what it does. 

To begin, I will explain the type of question and answer structure we will be dealing with. This system will contain a set of categories, each category containing a set of questions, and each question containing a set of answers. In theory, this could be done using a database or an XML file. For this example, I will use an XML file. 

The XML file will be formatted like so:

<questions>
   <category name="category1">
      <question text="This is a question?" minLevel="0" image="">
             <answer text="Option1" correct="0" />
             <answer text="Option2" correct="0" />
             <answer text="Option3" correct="1" />
             <answer text="Option4" correct="0" />
      </question>
   ...
   </category>
   ...
</questions>

I will explain later what minLevel, correct, and all of the other attributes are. 


The goal for our Q and A system is to turn this XML file into an identical data structure. Originally, I had omitted creating a structure for the categories since they can be represented easily as simply a text string. However, I later found myself wanting my program to want to have the ability to answer questions such as, "for the given difficulty level, how many questions does this category contain?" In order to answer this question, a category structure had to be built. 

So, we want to have a list of categories with varying length (assume we want this system to be used for various q and a games). Each category in the list has a list of questions which also vary in quantity. And each question has a set of answers (in this case, we set an upper bound on how many potential answers can exist). So it looks like the structures we will need to define are:

Category - The name of the category, an array of integers representing how many questions the category contains at varying difficulty settings, and a Queue<T> of question objects. 

Question - An array of possible answers, the index of the correct answer, the difficulty level of the question, and a drawable image.

Queue<T> - This class will create of list of T objects. If you are unfamiliar with Java generics, this will offer you a quick glance at how they work. This class will operate just like you would expect a queue to behave... you can push items on, pop them off, get items by index (not an array so this is a bit deceiving), or retrieve them by matching the Object's toString() method call. 

SO, lets begin...

No comments:

Post a Comment