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 

No comments:

Post a Comment