Skip to content

Instantly share code, notes, and snippets.

@Binary-Finery
Last active April 16, 2018 07:37
Show Gist options
  • Save Binary-Finery/c58d2577c8323762dd8b15ebf1e7a0c5 to your computer and use it in GitHub Desktop.
Save Binary-Finery/c58d2577c8323762dd8b15ebf1e7a0c5 to your computer and use it in GitHub Desktop.
quiz app using array list of objects to store questions, options and correct answer
public class Question {
private String question;
private String option1;
private String option2;
private String option3;
private String answer;
Question(String question, String option1, String option2, String option3, String answer) {
this.question = question;
this.option1 = option1;
this.option2 = option2;
this.option3 = option3;
this.answer = answer;
}
public String getQuestion() {
return question;
}
public String getOption1() {
return option1;
}
public String getOption2() {
return option2;
}
public String getOption3() {
return option3;
}
public String getAnswer() {
return answer;
}
}
------------------------------------------------------------------------
import java.util.ArrayList;
class QuestionBuilder {
static ArrayList<Question> questions() {
ArrayList<Question> questions = new ArrayList<>();
//lets add some questions to the question array list...
questions.add(new Question("Elon Musk is the CEO of which company?", "SpaceX", "Boston Dyamics", "Google", "SpaceX"));
questions.add(new Question("Larry Page and Sergey Brin are the founders of?", "Apple", "Microsoft", "Google", "Google"));
questions.add(new Question("Google's annual developers conference is called?", "Google IO", "Google 365", "Google Con", "Google IO"));
//now return the populated list...
return questions;
}
}
----------------------------------------------------------------------------------------
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ArrayList<Question> questions;
private String question, opt1, opt2, opt3, answer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//lets populate the questions ArrayList with all of the
//questions from the question builder class...
questions = QuestionBuilder.questions();
//now we can iterate the ArrayList using a loop and grab the info for each
// question...
for (int i = 0; i < questions.size(); i++) {
question = questions.get(i).getQuestion();
opt1 = questions.get(i).getOption1();
opt2 = questions.get(i).getOption2();
opt3 = questions.get(i).getOption2();
answer = questions.get(i).getAnswer();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment