javamultiple-constructors

Is it correct to use multiple constructors this way?


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.


Solution

  • For the code you posted, this would be a simple approach:

    package com.steve.research;
    
    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) {
            this(counter, question, answer1, answer2, null, null, rightAnswer);
        }
    
        public Quiz(int counter, String question, String answer1, String answer2, String answer3, String rightAnswer) {
            this(counter, question, answer1, answer2, answer3, null, rightAnswer);
        }
    
        public Quiz(int counter, String question, String answer1, String answer2, String answer3, String answer4, String rightAnswer) {
            this.counter = counter;
            this.question = question;
            this.answer1 = answer1;
            this.answer2 = answer2;
            this.answer3 = answer3;
            this.answer4 = answer4;
            this.rightAnswer = rightAnswer;
        }
    }
    

    For an improved approach, I suggest you look at "varargs" for the questions. Since you have a variable number of questions, you can put String ... questions as the last constructor argument (so rightAnswer has to go before).

    public class Quiz {
    
        private int counter;
        private String question;
        private String rightAnswer;
        private String[] answers;
    
        public Quiz(int counter, String question, String rightAnswer, String... answers) {
            this.counter = counter;
            this.question = question;
            this.rightAnswer = rightAnswer;
            this.answers = answers;
        }
    
        public static void main(String[] args) {
            new Quiz(1, "one plus one", "two", "one", "two", "three");
            new Quiz(1, "one plus one", "two", "one", "two", "three", "four");
            new Quiz(1, "one plus one", "two", "one", "two", "three", "four", "five");
        }
    }
    

    Note that answers is now a string array String[] and you can reference answers.length, answers[0] and so on.

    One more comment: calls to no-args super() in a constructor are usually superfluous (you don't need them).