-1

I am a beginner and I am trying to create a quiz to ask the user 3 Questions, I created 2 arrays: one for the questions and one for the right answer:

public static void main (String[] param) {

    String QArray[] = new String[3];
    QArray[0] = "What is 5 x 10?";
    QArray[1] = "What is 10 x 12?";
    QArray[2] = "What about 10 x 10?";

    String AArray[] = new String[3];
    AArray[0] = "50";
    AArray[1] = "120";
    AArray[2] = "100";
    ...
}

In order to make it work:

    for (int n = 0; n < QArray.length; n++) {
        System.out.println("Question" + (n + 1));
        System.out.println((QArray[n]));

        for (int m = 0; m < 3; m++) {
            String ans = scanner.nextLine();
            if (ans.equalsIgnoreCase(AArray[n])) {
                System.out.println("You got it right!");
                break;
            } else {
                System.out.println("\nThat is incorrect!");
            }
        }
    }

However I need to include getters and setters for my code, how could I include getters and setters with arrays? What do I need to replace it with?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    `Question` and `Answer` should be `objects` with their `getters` and `setters`, then we create an object Array and add them. It's very rough, I think an object-oriented programming lesson is not too much .. – F0XS Dec 11 '17 at 09:07
  • 1
    Possible duplicate of [How do getters and setters work?](https://stackoverflow.com/questions/2036970/how-do-getters-and-setters-work) – Kurt Van den Branden Dec 11 '17 at 11:08

1 Answers1

0

I do not understand why you need getters setters here. You have done it pretty much ok using String arrays.

However what you can do is create Question Objects and Answer Objects:

class Question{
    private String question;

    public String getQuestion() {
        return question;
    }

    public void setQuestion(String questionString) {
        question=questionString
    }
}

class Answer {
    private String answer;

    public String getAnswer() {
        return answer;
    }

    public void setAnswer(String answerString) {
        answer=answerString
    }
}

And use the getters and setters appropriately where required in the Code above.

edit: marked class variables private.

ddarellis
  • 3,912
  • 3
  • 25
  • 53
Rakesh
  • 104
  • 1
  • 11
  • If you do not understand why you need getters and setters is that you do not understand the principle of encapsulation of object-oriented programming, that explains why you did not put the class variables in private. – F0XS Dec 11 '17 at 09:35
  • I intended to , I just forgot here sir. I get what you are trying to say. But What he has done was fine for the requirement even if it was procedure oriented, unless he was told by his superiors to use objects and have getters setters therefore. – Rakesh Dec 11 '17 at 10:13
  • It has nothing to do with these superiors ... Java is an object oriented language it must be used as such with these coding rules. The goal is not only that it works, it must be understood by any person who would resume work and that it is maintainable in time. – F0XS Dec 11 '17 at 10:19
  • In addition, it is still false proof that you do not understand .... your setters is `this.question = questionString;` And your response setter has for parameter `String questionString`.... Without wanting to offend you if you do not understand these principles you can not help it, your answer will be necessarily false. See [here](/questions/1568091/why-use-getters-and-setters-accessors) it can help you. – F0XS Dec 11 '17 at 10:24
  • What if this is the one and final requirement? Why would one go such an elaborate way of achieving the functionality ? Writing it in the main class as he is done is still OO as I see it for the limited scope his program has. – Rakesh Dec 11 '17 at 11:05
  • Also I don't get what is wrong in my getters and setters, could you elaborate? Maybe I will understand better. – Rakesh Dec 11 '17 at 11:06
  • These are coding standards and it is handy to use them so that everyone understands what the person is trying to do. I repeat myself but your setter is wrong you would know if you understood the utility of the OOP it is necessary to use `THIS` so the method is`this.question = questionString;`as I wrote all the 'hour.... – F0XS Dec 11 '17 at 11:10
  • this is used to distinguish the class variable and the setter function paramater if they have same names. See for that sole purpose my setter function paramater is questionString and not question. Here [this] (https://stackoverflow.com/a/43843468/5010582) can help you. – Rakesh Dec 11 '17 at 11:15
  • All IDEs generate getters and setters automatically so they use the notion this, if this person has to ask the question is that he does not know how it works ... That's what I explain to you at the first if you put your code in your way without any explanation, it's not a correct answer and it's not going to help him copied what you did ... you did not say he wanted to understand by asking this question ... – F0XS Dec 11 '17 at 11:24
  • Maybe I should have explained. Anyways, I sincerely hope our little exchanges here would benefit the OP. :) – Rakesh Dec 11 '17 at 12:04