Advertisement
Guest User

Untitled

a guest
May 27th, 2021
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. public class Question {
  2.  
  3. private int questionNumber; //representing the question #
  4. private String theQuestion; //the question itself.
  5.  
  6. private String[] answers; //all the answers of the question
  7. private String correctAnswer; //string of the correct answer
  8.  
  9.  
  10. //contructor
  11. public Question (int questionNumber, String question, String[] answersInput, String correctAnswerInput) {
  12.  
  13. this.questionNumber = questionNumber;
  14. this.theQuestion = question;
  15.  
  16. this.answers = new String[answersInput.length];
  17. for (int i = 0; i < this.answers.length; i++) {
  18. this.answers[i] = answersInput[i];
  19. }
  20.  
  21. //setting the correct answer
  22. this.correctAnswer = correctAnswerInput;
  23. }
  24.  
  25. //getter for correctAnswer
  26. public String getCorrectAnswer() {
  27. return this.correctAnswer;
  28. }
  29.  
  30. //getter for answer the user selected
  31. public String getAnswer(int answerLabel) {
  32. return this.answers[answerLabel - 1];
  33. }
  34.  
  35. //toString to print the question and all of the possible answers.
  36. public String toString() {
  37. String questionString = "";
  38.  
  39. questionString += "Question #" + this.questionNumber + ":\t" + this.theQuestion + "\n\n";
  40.  
  41. //presents all of the answers in the list.
  42. for (int i = 0; i < this.answers.length; i++) {
  43. questionString += (i + 1) + ": " + this.answers[i] + "\n";
  44. }
  45. return questionString;
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement