How to create getter and Setter from Arraylist?

Asked

Viewed 548 times

3

I am creating a question and answer game, with the attributes: question, correct answer and the ArrayList option (private ArrayList<String> opcao = new ArrayList<String>();). I need the getter of the options to use in my main class to print the questions and answers on the screen, only I’m not sure how to create the getter of options for being a ArrayList. I’ve tried a few ways and when I go to the main class I can’t access the getter.

public class Perguntas {

    private String pergunta;


    private ArrayList<String> opcao = new ArrayList<String>();


    private String respostaCorreta;


    //Metodo Construtor
    public Perguntas(String p, String res, String opc1, String opc2, String opc3) {
        this.pergunta = p;
        this.opcao.add(opc1);
        this.opcao.add(opc2);
        this.opcao.add(opc3);
        this.respostaCorreta = res;
    }



    //GET E SET DA PERGUNTA
    public String getPergunta() {
        return pergunta;
    }

    public void setPergunta(String p) {
        this.pergunta = p;
    }

    //GET E SET DAS OPCOES


    //GET E SET DA RESPOSTA CORRETA
    public String getRespostaCorreta() {
        return respostaCorreta;
    }

    public void setRespostaCorreta(String res) {
        this.respostaCorreta = res;
    }

}
  • This might help https://answall.com/a/100868/2541

2 answers

7


The code is confusing and seems to have some things wrong even conceptually, hence it is even complicated to do the right thing or even answer this question.

If the class calls Perguntas should have several questions there and not just one. It’s supposed to be one, right? Then she should call Pergunta.

If the question has answer established in multiple choice, why should have an answer like String, should not indicate which item number is correct?

Are you sure you need getters and setters in this way? Are you doing it because you know the reason or just because it’s what you saw in some cake recipe that said to do it like this and you don’t even understand the motivation for it? Not knowing why to wear something and what you get out of it is better not to use it. Mainly Setter that people create without even thinking if the data should be changed or not.

Then you need to define how you want to get the options. Will a list be returned? Will it be item by item? Then you need an iterator mechanism or have a way to get a specific item, but this is abstraction leak (something I think will not worry, but is still conceptually wrong). Let’s do it in the simplest way:

ArrayList<String> getOpcoes() {
   return opcoes;
}

Note that I changed the variable name to opcoes because if it’s a list then it should be plural.

And if you want a specific item:

String getOpcao(int item) {
    return opcoes.get(item);
}

I put in the Github for future reference.

This is still an abstraction leak, even if smaller. To do without leakage would need more complexity that I think is better not to involve now.

5

In this case you can make two types of getter: one that returns the entire array and one that returns only one item of the array.

//retorna o array inteiro
ArrayList<String> getOpcao() {
   return this.opcao;
}

//retorna uma opcao especifica
String getOpcao(int indice) {
    return this.opcao.get(indice);
}

I would change the variable name opcao for opcoes, because the name of a list makes more sense to be plural. So the whole list getter would be getOpcoes, while the getter of a specific item would stand getOpcao.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.