Home > Blockchain >  Make arraylist of questions, options and answres in java android studio
Make arraylist of questions, options and answres in java android studio

Time:08-11

I am new to java programing and i need to make an arraylist which will store questions, options and answres. What is the best way to do that if I want to use that arraylist for questions with 4 options and also for questions with multiple choices and questions with typed answer by user.

My text file with questions looks like this:

Which Java keyword is used to define a subclass?
extends

What is the original name of the Java language?
A) Swift
B) C--
C) Oak
D) Ruby
C

Which of the following types are supertypes of Rectangle?
A) PrintStream
B) Shape
C) RectangularShape
D) Object
BCD

CodePudding user response:

I think you should receate a model for that. A model is a data class.

class QuestionModel() {
    String question;
    String singleAnswer;
    List<String> listSingleChoiceAnswer;
    List<String> listMultipleChoiceAnswer;

public QuestionModel(
    String singleAnswer, 
    List<String> listSingleChoiceAnswer,
    List<String> listMultipleChoiceAnswer
) {
    this.singleAnswer = singleAnswer;
    this.listSingleChoiceAnswer = listSingleChoiceAnswer;
    this.listMultipleChoiceAnswer = listMultipleChoiceAnswer;
}
}

Then, when you want to create a list question

Arraylist<QuestionModel> listQuestion = new Arraylist<QuestionModel>;

Ex, you want to add a typeable answer question

listQuestion.add(new QuestionModel("Which Java keyword is used to define a subclass?", "extends", null, null))

or a multiple choice

Arraylist<String> multipleAns = Arraylist<String>("A","B","C","D")
listQuestion.add(new QuestionModel("Which of the following types are supertypes of Rectangle?", null, null, multipleAns))

Finally you can display questions depend on not NULL type answer.

  • Related