Tuesday, July 19, 2011

Question Class

This class is the most straightforward of all that will be used to construct this Q&A system. The class simply represents a trivia question. 


Each question has the following attributes:

  • Question text
  • An array of possible answers 
  • The index in the array of the correct answer
  • A drawable image for image related questions (not implemented yet)
The class has simple getter and setter methods with access each of the fields. 



private class Question
/*** Description *** 
* This class represents a single question/answer.

*** Variables *** 
* 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

*** 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
*/
{
private int cAnswer;  
private int answerIndex;
private String [] answers; 
private String question;
private Drawable image;

public Question ()
{
this.cAnswer = -1; 
this.question = null;
this.image = null;
this.answers = new String[MAX_ANSWERS];
this.answerIndex = 0;
}

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

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

public void setImage (String imagePath)
/* NOT FUNCTIONAL YET */
{
this.image = null;
}

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

public boolean checkAnswer(int answer)
{
return (cAnswer == answer);
}

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

public String getQuestion()
{
return question;
}

public Drawable getImage()
{
return image;
}

public int getCAnswer()
{
return cAnswer;
}


}

No comments:

Post a Comment