Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Saturday, July 23, 2011

Class Definition - Question

Another straightforward class definition. A question has its text, a set of answers and "knows" which is the correct answer, an associated level of difficulty, and it may or may not have an image as part of the question. The setter and getter methods are very straightforward except for the image setter which must first check whether the passed argument has a valid image associated with it located in the drawable resources. 


Again, if there is something here you do not follow just ask and I will respond. 


import android.content.Context;
import android.graphics.drawable.Drawable;


@SuppressWarnings("rawtypes")
public class Question implements Comparable
{
/*** Description *** 
* This class represents a single question and set of answers.

*** Attributes *** 
* cAnswer  - Index (in answers[]) of correct answer to question
* answers  - Array of strings representing a set of possible answers
* question - String representing the question text
* image - Drawable image created from imageSrc passed in constructor
* level - Integer level number representing the minimum level this question is available at
* c - context
* answerIndex - used to add answers to array

*** Methods ***
* Constructor - intitializes all items to default values

* checkAnswer - returns true when submitted integer representing an array index
* matches cAnswer

* setQuestion, setCAnswer, setImage, addAnswer - setter methods

* getAnswers  - Accessor method which returns answers
* getQuestion - Accessor method which returns question
* getImage    - Accessor method which returns image
* getCAnswer  - Accessor method which returns cAnswer

* compareTo   - Returns true only when comparing to same object
*/
private int cAnswer;  
private int answerIndex;
private int level;
private String [] answers; 
private String question;
private Drawable image;
private Context c;

public Question (Context c)
{
this.cAnswer = -1; 
this.question = null;
this.image = null;
this.answers = new String[c.getResources().getInteger(R.integer.max_answers)];
this.answerIndex = 0;
this.level = -1;
this.c = c;
}

public void setQuestion(String q)
{
this.question = q;
}

public void setCAnswer(int i)
{
this.cAnswer = i;
}

public void setLevel(int l)
{
this.level = l;
}

public void setImage (String imageName)
{


int id = c.getResources().getIdentifier(imageName, "drawable", c.getPackageName());
if (id != 0)
{
this.image = c.getResources().getDrawable(id);
}
else
this.image = null;

}

public void addAnswer (String a)
{
this.answers[answerIndex++] = a;
}

public String [] getAnswers()
{
return answers;
}

public String getQuestion()
{
return question;
}

public Drawable getImage()
{
return image;
}

public int getCAnswer()
{
return cAnswer;
}

public int getLevel()
{
return level;
}


public int compareTo(Object another) 
{
// TODO Auto-generated method stub


if (this.equals(another))
return 0;
else 
return -1;
}
}


Continue to Queue class definition >
< Back to Class Definition - Category 

Class Definition - Category

This section will define the Category class. I don't believe anything in this section is  particularly challenging if you have read my previous post about this project, so I will simply post the code. If you have questions, please ask and I will respond.  Remember, this is being written for Android so we need to include a Context and we fetch resources from XML files using the Context.getResources() method. If you aren't familar with Android or want to do this project for console, you can omit anything to do with Context and replace the getResources() call with a variable or integer value. 


import android.content.Context;


@SuppressWarnings("rawtypes")
public class Category implements Comparable
/***
 *** Description ***
 *
 * This class defines a Category object. Each Category contains a name, a list of questions, 
 * and an array of integers representing the number of questions at each difficulty level. 
 * 
 *** Attributes ***
 *
 * String name     - The name of the category
 * int [] levels   - The number of questions at each difficulty level
 * Queue<Question> - A Queue<T> of Question objects 
 * 
 *** Methods ***
 *
 * Constructor: Requires a context and a name. Initializes all attributes. 
 * 
 * addQuestion: Increments the appropriate levels[] index and enqueues the passed Question. 
 * getName: Returns the name
 * getQuestionsAtLevel: returns the number of questions available at the passed integer difficult level
 * getQuestions: Returns the Queue<Question> object
 * toString: returns name
 * compareTo: returns -1 if passed object is not a category, 1 if it is a different Category, or 0 if the same
 * 
 */
{
private String name;
private int [] levels; // levels[0] = easy; levels[1] = normal; levels[2] = genius
private Queue<Question> questions;

public Category (Context c, String name)
{
this.name = name;
this.levels = new int[c.getResources().getInteger(R.integer.number_of_levels)];
this.questions = new Queue<Question>();
}

public void addQuestion (Question q)
{
levels[q.getLevel()]++;
questions.enqueue(q);
}

public String getName()
{
return name;
}

public int getQuestionsAtLevel(int j)
{
int numQs = 0;
for (int i = 0; i <= j; i++)
numQs+= levels[i];
return numQs;
}

public Queue<Question> getQuestions()
{
return questions;
}


public int compareTo(Object another) {
if (another instanceof Category)
{


if (this.toString().equals(another.toString()))
{
return 0;
}
else
return 1;
}
return -1;
}

public String toString()
{
return name;
}
}

Continue to Class Definition - Question >
< Back to Q and A System Take 2 

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...