1

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.

khelwood
  • 55,782
  • 14
  • 81
  • 108
Sky Mage
  • 31
  • 5
  • 4
    You should look up "Constructor overloading" – QBrute Mar 09 '17 at 09:35
  • if you are gonna reuse values you can invoke one constructor from another using this(args); – Sarthak Mittal Mar 09 '17 at 09:36
  • i might be getting this wrong but instead of all of this multiple parametar confusion, why not just put all the answers in arraylist or something... I feel like your logic and approach is weird .... – Marko Mar 09 '17 at 09:41
  • no need to call super the Quiz class extends implicitly the object class – ΦXocę 웃 Пepeúpa ツ Mar 09 '17 at 09:44
  • "putting this into a hashmap and then serializing it to a file does not work" - please note that "this doesn't work" is rarely helpful. If you want us to be of any help you should tell us in what way it didn't work, what you did exactly, what you expected and what you got instead (any errors, different results etc.). – Thomas Mar 09 '17 at 09:45
  • @Thomas yeah, well I actually did something that worked while I was writing this, that's why I didn't bother posting the code... I just wanted to know about constructor overload. Out of all of the OOP, it's the constructors that confuse me the most for some reason... – Sky Mage Mar 09 '17 at 10:52
  • @Marko It's more of a "too much use of code generate button". It's not really my logic... – Sky Mage Mar 09 '17 at 11:00

3 Answers3

5

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).

Thomas
  • 87,414
  • 12
  • 119
  • 157
vikingsteve
  • 38,481
  • 23
  • 112
  • 156
  • Nice breakdown, this is what I'd suggest as well. I'd have one change to the constructor though: `answers` might be empty which could break the system. Thus either `rightAnswer` should be part of the possible answers (which could then be shuffled to not depend on order) or there should be a `firstAnswer` before a varargs `additionalAnswers` to force the caller to provide at least one (though the latter option might still break it if `answers` doesn't contain `rightAnswer`). - That's more of a suggestion for the OP though. I just dump it here because of the close relation ;) – Thomas Mar 09 '17 at 09:50
  • @Thomas nice suggestion. You could put `if (answers.length == 0) throw IllegalArgumentException("must have at least one answer")` in the constructor. – vikingsteve Mar 09 '17 at 10:16
  • Yes, perhaps the improved approach is quite a nice thing to have, however I am actually making object of that class and putting them into a hashmap so that when you create a question, the answer is encrypted. Meaning that if you create a whole quiz and give that serialized file to a student, they can not just open the file to see the answers (everything is encrypted), they have to open it using the program, which only shows them after doing the quiz, or does not show them at all, but only the result. This is part of a massive code, included in a GUI, so I got all the checks for min 2 answers. – Sky Mage Mar 09 '17 at 10:46
0

why not use List of answers.

 public int Quiz(int counter, List<String> answers, String rightAnswer){...}

also you can use override constructor like

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){
    this(counter,answer1,answer2,rightAnswer);
    this.answer3 = answer3;

    }

it will look little organised.

Thomas
  • 87,414
  • 12
  • 119
  • 157
SAQ
  • 186
  • 3
  • 9
0

Create a constructor to catch all the values like,

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;
    }

then you can do 2 thing ,

1: Create other constructors and inside those use the constructor created above like this.

public Quiz(int counter,String question, String answer1, String answer2,String rightAnswer){
 this(counter,question, answer1, answer2, null, rightAnswer)
}

2: Create separate static methods for each like

public Quiz getQuizeWithTwoAnswers(int counter,String question, String answer1, String answer2,String rightAnswer){
    return new Quiz(counter,question, answer1, answer2, null, rightAnswer)}

this will help improve the readability .

Keaz
  • 955
  • 1
  • 11
  • 21
  • Ok,well i guess I rely on too much code generation :D hehehe thanks a lot for this. I might consider using it. – Sky Mage Mar 09 '17 at 10:57