I am not really sure how this works, but if I want to give the option for giving more or less variables to an object of a class, would this work with multiple constructors like this?
Let's say I would like to create a multiple choice questionaire, however I do not know how many answers my user would like to input, 2,3,4,5,6 maybe? So for that:
public class Quiz {
private int counter;
private String question;
private String answer1;
private String answer2;
private String answer3;
private String answer4;
private String answer5;
private String answer6;
private String rightAnswer;
public Quiz(int counter,String question, String answer1, String answer2, String rightAnswer){
super();
this.counter = counter;
this.question = question;
this.answer1 = answer1;
this.answer2 = answer2;
this.rightAnswer = rightAnswer;
}
public Quiz(int counter, String question, String answer1, String answer2, String answer3, String rightAnswer) {
super();
this.counter = counter;
this.question = question;
this.answer1 = answer1;
this.answer2 = answer2;
this.answer3 = answer3;
this.rightAnswer = rightAnswer;
}
public Quiz(int counter, String question, String answer1, String answer2, String answer3, String answer4,
String rightAnswer) {
super();
this.counter = counter;
this.question = question;
this.answer1 = answer1;
this.answer2 = answer2;
this.answer3 = answer3;
this.answer4 = answer4;
this.rightAnswer = rightAnswer;
}
//...more options
Perhaps I could just do 1 constructor with some kind of enum or switch? In the end of the day, after trying this method, for some reason putting this into a hashmap and then serializing it to a file does not work where as with 1 constructor it works but doesn't write everything in there. I am a bit confused of what the problem is, maybe it's something to do with my toString override, but anyway, just tell me about this one so that I have one less confusing problem to worry about.