Saturday, July 23, 2011

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 

No comments:

Post a Comment