I am working on a java quiz application. In this I want the four answer options to be as an array instead of coding it is as Option 1,2,..4. How do I add an array of objects to the constructor ?
I am pasting my code here:
- POJO Class
<code>package com.param;
import java.util.Arrays;
public class Question {
private int id;
private String question;
private String opt1;
private String opt2;
private String opt3;
private String opt4;
private String answer;
public Question(int id, String question, String opt1, String opt2,String opt3, String opt4, String answer) {
super();
this.id = id;
this.question = question;
this.opt1 = opt1;
this.opt2 = opt2;
this.opt3 = opt3;
this.opt4 = opt4;
this.answer = answer;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getOpt1() {
return opt1;
}
public void setOpt1(String opt1) {
this.opt1 = opt1;
}
public String getOpt2() {
return opt2;
}
public void setOpt2(String opt2) {
this.opt2 = opt2;
}
public String getOpt3() {
return opt3;
}
public void setOpt3(String opt3) {
this.opt3 = opt3;
}
public String getOpt4() {
return opt4;
}
public void setOpt4(String opt4) {
this.opt4 = opt4;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
@Override
public String toString() {
return "Question [id=" + id + ", question=" + question + ", opt1=" + opt1 + ", opt2=" + opt2 + ", opt3=" + opt3
+ ", opt4=" + opt4 + ", answer=" + answer + "]";
}
}
</code>
<code>package com.param;
import java.util.Arrays;
public class Question {
private int id;
private String question;
private String opt1;
private String opt2;
private String opt3;
private String opt4;
private String answer;
public Question(int id, String question, String opt1, String opt2,String opt3, String opt4, String answer) {
super();
this.id = id;
this.question = question;
this.opt1 = opt1;
this.opt2 = opt2;
this.opt3 = opt3;
this.opt4 = opt4;
this.answer = answer;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getOpt1() {
return opt1;
}
public void setOpt1(String opt1) {
this.opt1 = opt1;
}
public String getOpt2() {
return opt2;
}
public void setOpt2(String opt2) {
this.opt2 = opt2;
}
public String getOpt3() {
return opt3;
}
public void setOpt3(String opt3) {
this.opt3 = opt3;
}
public String getOpt4() {
return opt4;
}
public void setOpt4(String opt4) {
this.opt4 = opt4;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
@Override
public String toString() {
return "Question [id=" + id + ", question=" + question + ", opt1=" + opt1 + ", opt2=" + opt2 + ", opt3=" + opt3
+ ", opt4=" + opt4 + ", answer=" + answer + "]";
}
}
</code>
package com.param;
import java.util.Arrays;
public class Question {
private int id;
private String question;
private String opt1;
private String opt2;
private String opt3;
private String opt4;
private String answer;
public Question(int id, String question, String opt1, String opt2,String opt3, String opt4, String answer) {
super();
this.id = id;
this.question = question;
this.opt1 = opt1;
this.opt2 = opt2;
this.opt3 = opt3;
this.opt4 = opt4;
this.answer = answer;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getOpt1() {
return opt1;
}
public void setOpt1(String opt1) {
this.opt1 = opt1;
}
public String getOpt2() {
return opt2;
}
public void setOpt2(String opt2) {
this.opt2 = opt2;
}
public String getOpt3() {
return opt3;
}
public void setOpt3(String opt3) {
this.opt3 = opt3;
}
public String getOpt4() {
return opt4;
}
public void setOpt4(String opt4) {
this.opt4 = opt4;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
@Override
public String toString() {
return "Question [id=" + id + ", question=" + question + ", opt1=" + opt1 + ", opt2=" + opt2 + ", opt3=" + opt3
+ ", opt4=" + opt4 + ", answer=" + answer + "]";
}
}
- Question Service class
<code>package com.param;
public class QuestionService {
Question[] questions = new Question[4];
public QuestionService() {
questions[0] = new Question(1,"Value of pie","3.12","3.01", "3.14", "3.11", "3.14");
questions[1] = new Question(2,"Your age", "24","54", "35", "25", "25");
questions[2] = new Question(3,"This year", "1898","2034", "2024", "1999", "2024");
questions[3] = new Question(4,"Days in jan", "31","30", "34", "11", "31");
}
public void playQuiz() {
System.out.println("The quiz begins");
for(Question q: questions) {
//System.out.println(q.getId()+"t");
System.out.println(q.getId() + "." + q.getQuestion()+"n");
System.out.println(q.getOpt1()+"n");
System.out.println(q.getOpt2()+"n");
System.out.println(q.getOpt3()+"n");
System.out.println(q.getOpt4()+"n");
}
}
}
</code>
<code>package com.param;
public class QuestionService {
Question[] questions = new Question[4];
public QuestionService() {
questions[0] = new Question(1,"Value of pie","3.12","3.01", "3.14", "3.11", "3.14");
questions[1] = new Question(2,"Your age", "24","54", "35", "25", "25");
questions[2] = new Question(3,"This year", "1898","2034", "2024", "1999", "2024");
questions[3] = new Question(4,"Days in jan", "31","30", "34", "11", "31");
}
public void playQuiz() {
System.out.println("The quiz begins");
for(Question q: questions) {
//System.out.println(q.getId()+"t");
System.out.println(q.getId() + "." + q.getQuestion()+"n");
System.out.println(q.getOpt1()+"n");
System.out.println(q.getOpt2()+"n");
System.out.println(q.getOpt3()+"n");
System.out.println(q.getOpt4()+"n");
}
}
}
</code>
package com.param;
public class QuestionService {
Question[] questions = new Question[4];
public QuestionService() {
questions[0] = new Question(1,"Value of pie","3.12","3.01", "3.14", "3.11", "3.14");
questions[1] = new Question(2,"Your age", "24","54", "35", "25", "25");
questions[2] = new Question(3,"This year", "1898","2034", "2024", "1999", "2024");
questions[3] = new Question(4,"Days in jan", "31","30", "34", "11", "31");
}
public void playQuiz() {
System.out.println("The quiz begins");
for(Question q: questions) {
//System.out.println(q.getId()+"t");
System.out.println(q.getId() + "." + q.getQuestion()+"n");
System.out.println(q.getOpt1()+"n");
System.out.println(q.getOpt2()+"n");
System.out.println(q.getOpt3()+"n");
System.out.println(q.getOpt4()+"n");
}
}
}
So in the Question service class, instead of giving each individual option as a parameter, how can I give the array of that object?
Please guide me on this.
Thanks
New contributor
Paramesh Athreya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.