Join objects within collection of different objects

Asked

Viewed 754 times

1

How do I "join" or concatenate an object within a collection of different objects? I’ve tried redoing but it always gives the same error.

public class Prova {
    private String titulo;

    private int numQuestoes;

    private String disciplina;

    private double notaMaxima;

    private String professor;

    private int id = 0;

    Questao[] questao = new Questao[100];


    static int contador = 0;


  Prova(String titulo, int numQuestoes , String disciplina , double  notaMaxima , String professor){


    contador++;
    this.id = Prova.contador;

  this.titulo = titulo;
  this.numQuestoes = numQuestoes;
  this.disciplina = disciplina;
  this.notaMaxima = notaMaxima;
  this.professor = professor;
  }

  public String getTitulo() {
    return titulo;
  }

  public void setTitulo(String titulo){
      this.titulo = titulo;
  }

  public int getNumQuestoes() {
    return numQuestoes;
  }

  public void setNumQuestoes(int numQuestoes) {
    this.numQuestoes = numQuestoes;
  }

  public String getDisciplina() {
    return disciplina;
  }

  public double getNotaMaxima() {
    return notaMaxima;
  }

  public String getProfessor() {
    return professor;
  }

  public int getId() {
    return id;
  }

  public void mostrarProva() {
    System.out.println("ID: "+id);
    System.out.println("Título: "+titulo);
    System.out.println("Número de Questões: "+numQuestoes);
    System.out.println("Disciplina: "+disciplina);
    System.out.println("Nota Máxima: "+notaMaxima);
    System.out.println("Professor: "+professor);



    //System.out.println("\n");
  } 

  public class Questao {
    private int numero;
  //private String item;
    private String enunciado;
    private String resposta;
    static int contador = 0;
    private int id;



    Questao(int numero, String enunciado, String resposta){

    contador++;
    this.id = Questao.contador;

    this.numero = numero;
    this.enunciado = enunciado; 
    this.resposta = resposta;
    }

  public int getNumero(){
    return numero;
  }

  public String getEnunciado(){
    return enunciado;
  }

  public String getResposta() {
    return resposta;
  }

  public int getId() {
    return id;
  }

  public void mostrarQ() {
    System.out.println("ID :"+id);
    System.out.println("Numero : "+numero);
    System.out.println("Enunciado : "+enunciado);
    System.out.println("Resposta : "+resposta);
  }

  }

     public class CadastroDeProvas {

     Prova[] provas = new Prova[100];

     int qtdProvas = 0;
     int qtdQuest = 0;

    void adicionarQuestao (int i, Questao questao){  
         provas[i].questao[qtdQuest] = questao;
          qtdQuest++;
     }

    void imprimir(){ 
      for(int i = 0; i < qtdProvas; i++){
            provas[i].mostrarProva();
      for(int j=0;j < qtdQuest;j++) {
            provas[i].questao[j].mostrarQ();
      }

        }
    }

}

The parameter int i is the position of the object of the vector of provas[] that concatenated with the vector questao[] will receive the object questao.

 void imprimir(){ 
    for(int i = 0; i < qtdProvas; i++){
            provas[i].mostrarProva();
    for(int j=0;j < qtdQuest;j++) {
            provas[i].questao[j].mostrarQ();
    }

        }
    }

Every time I run the code imprimir() it gives error in this method, more specifically in the line highlighted below:

provas[i].questao[j].mostrarQ();
public class Questao {
    private int numero;
    //private String item;
    private String enunciado;
    private String resposta;
    static int contador = 0;
    private int id;

The mistake more specifically:

Exception in thread "main" java.lang.NullPointerException
    at br.ufc.crateus.provas.CadastroDeProvas.imprimir(CadastroDeProvas.java:26)
    at br.ufc.crateus.provas.Principal.main(Principal.java:92)
  • What are you trying to do? What goal do you want to achieve? The question got me a little confused and I think you may need to edit your question. Try giving a space between classes to visibly group them. Where is the Questao class?

  • The program so far is making a record of evidence and question. And what I want to do is to add a question to a certain piece of evidence, that is, when I print the data of a piece of evidence, the data of the respective question can be shown tbm.

  • For example: I register a proof "P1" and a question "Q1". I wanted the "Q1" to be concatenated with "P1", I don’t know if "concatenate" is the best word, but that’s more or less what I tried to do in the code above. However, if I register more than ONE proof and try to concatenate a question, the program gives error when printing().

  • What do you mean by concatenate proof and question ? Concatenating in programming means "joining texts", not objects.

  • That’s why I put it in quotes. It was just for lack of a better word.

  • What I’m really wanting to do is in the above comments.

Show 1 more comment

3 answers

0

You will be able to solve better using arraylist, if you do not know yet from a look.

Now if you are doing a job for college and/or course then.

First array does not accept different value types. If you make an array to insert integers, you cannot successively add String or Double type values.

0


First, you should better organize your code with identation and never forget the modifiers public or private, After all, in almost every case, package visibility is not what you want. And whenever you want, it’s always good to put a comment highlighting that (/* package */) to make it clear that you really want the package visibility and not only forgot the visibility modifier, besides saying in which other class and method of the same package this method or constructor should be used.

Once done, remember that working directly with arrays is torture. Prefer to use lists, Mapis or something else. In mature, modern and well-developed Java projects, it makes little sense to use arrays directly, and when they do appear, it is usually to use varargs, either be used with annotations, or because a particular API that needs to be used produces or consumes arrays or are arrays of primitive types used that correspond to raw data used in I/O operations, or to accomplish something very simple for which it does not need much sophistication. Out of these cases, rararamente is good idea the direct use of arrays. And even in these cases, often the array is encapsulated in some other class.

In your case, when using lists, you no longer need to worry about how many questions or how many proofs are registered, just see the size of the list. In addition, the need to check whether certain array positions are null is eliminated, which even eliminates the NullPointerException that you had. It also eliminates the need to keep tracking up a lot of crazy accountants and all the complexity involved in this.

I notice you have an array called questao. Now, if the name is singular, I would not expect it to be an array. The name questoes would be much better. Already the name provas is correctly plural.

It is also a good idea to add the modifier final in fields of objects that cannot be assigned after completion of the constructor.

In addition, the use of mutable variables with the modifier static (global variables) is always an anti-standard. A bad thing, a sign that something in your project is wrong. In your case, the variables contador of the classes Prova and Questao are used to count the evidence and questions, and therefore it makes sense that they were in the class CadastroDeProvas. Moreover, in the case of Questao, the id could be replaced by id plus the question number in the test.

Finally, if you want to get a description of an object, use the method toString(), which is already implemented satisfactorily for lists. Put the System.out.println directly in the show method, prevents you from taking that description and deciding what to do with it if you want to put it somewhere else. Another alternative would be to work with StringBuilders.

And to ensure a good encapsulation, never access an array or other class list field directly. Always do this through a method. And if method should not export the array or list, rather do the desired operation with it. I mean, instead of:

provas[i].questao[qtdQuest] = questao;

You’d do something like:

provas[i].adicionarQuestao(questao);

However, it still uses provas as if it were an array. Moreover, the answerability of adding a question to the proof should be in the class Prova, and not in class CadastroDeProvas.

That said, let’s restructure your classes:

import java.util.ArrayList;
import java.util.List;

public class Prova {
    private final String titulo;

    private final String disciplina;

    private final double notaMaxima;

    private final String professor;

    private final int id;

    private final List<Questao> questoes = new ArrayList<>();

    // Deve ser utilizado apenas pelo método criarProva da classe CadastroDeProvas.
    /* package */ Prova(int id, String titulo, String disciplina, double notaMaxima, String professor) {
        this.id = id;
        this.titulo = titulo;
        this.disciplina = disciplina;
        this.notaMaxima = notaMaxima;
        this.professor = professor;
    }

    public String getTitulo() {
        return titulo;
    }

    public int getNumeroQuestoes() {
        return questoes.size();
    }

    public String getDisciplina() {
        return disciplina;
    }

    public double getNotaMaxima() {
        return notaMaxima;
    }

    public String getProfessor() {
        return professor;
    }

    public int getId() {
        return id;
    }

    public void adicionarQuestao(String enunciado, String resposta) {
        questoes.add(new Questao(this, questoes.size() + 1, enunciado, resposta);
    }

    @Override
    public String toString() {
        return new StringBuilder("ID: " + id
                + "\nTítulo: " + titulo
                + "\nNúmero de Questões: " + getNumeroQuestoes()
                + "\nDisciplina: " + disciplina
                + "\nNota Máxima: " + notaMaxima
                + "\nProfessor: " + professor
                + "\nQuestões: " + questoes);
    }
}
public class Questao {
    private final Prova prova;
    private final int numero;
    private final String enunciado;
    private final String resposta;

    // Deve ser utilizado apenas pelo método adicionarQuestao da classe Prova.
    /* package */ Questao(Prova prova, int numero, String enunciado, String resposta) {
        this.prova = prova;
        this.numero = numero;
        this.enunciado = enunciado; 
        this.resposta = resposta;
    }

    public Prova getProva() {
        return prova;
    }

    public int getNumero() {
        return numero;
    }

    public String getEnunciado() {
        return enunciado;
    }

    public String getResposta() {
        return resposta;
    }

    @Override
    public String toString() {
        return "ID da prova: " + prova.getId()
                + "\nNúmero: " + numero
                + "\nEnunciado: " + enunciado
                + "\nResposta: " + resposta
    }
}
import java.util.ArrayList;
import java.util.List;

public class CadastroDeProvas {
     private final List<Prova> provas = new ArrayList<>();

     public Prova criarProva(String titulo, String disciplina, double notaMaxima, String professor) {
         Prova p = new Prova(provas.size() + 1, titulo, disciplina, notaMaxima, professor);
         provas.add(p);
     }

     @Override
     public String toString() { 
         return provas.toString();
     }
}

At last, your class Prova also seems that will confuse two distinct concepts. A concept is the model of proof, with the statement of questions. Another thing is the answer given by each student in the test he received and the obtained grade. However, to change that I would need to know a few more things about your project.

0

OK.. I think I understand what you need.

Where you wrote:

void imprimir(){ 
    for(int i = 0; i < qtdProvas; i++){
            provas[i].mostrarProva();
    for(int j=0;j < qtdQuest;j++) {
            provas[i].questao[j].mostrarQ();
    }

        }
    }

You wanted to say... something like:

  • Test 1 + Test 1 data;

    • Prova1.Questao1 + data of question 1
    • Prova1.Questao2 + question data 2
    • Prova1.Questao2 + question data 3
    • Prova1.Questaon + data from question N
  • Proof 2 + Proof 2 data;

    • (etc.)

Correct me if I’m wrong. I’m still trying to figure out your need.

If so, do something like

public class Questao{
//todos os campos da classe + construtores, getters and setters necessarios

@Override
public String toString(){
    return String.format(
  "ID : %s\n"+
  "Numero : %d \n" + 
  "Enunciado : %s \n" +
  "Resposta : %s \n", id, numero, enunciado, resposta );
}

}

Now define the Proof:

public class Prova{
    //todos os campos, construtores, getters e setters

    private List<Questao> questoes = new Arraylist<>();

    @Override
    public String toString(){
        String saida = 
            String.format("ID: %d \n" + 
                    "Título: %s \n" + 
                    "Número de Questões: %d \n" +
                    "Disciplina: %s \n" +
                    "Nota Máxima: %f \n" + 
                    "Professor: %s \n",
                    id, numQuestoes, disciplina, notaMaxima, professor 
            );
        for(Questao q : questoes){
            saida += q.toString();
        }
    }
}

Now, just run for it:

public class Cadastrodeprovas {

 List<Prova> provas = new Arraylist<>();

 int qtdProvas = 0;
 int qtdQuest = 0;

void adicionarQuestao (int i, Questao questao){  
     provas.getQuestoes.add(questao);
     //será necessário algum ajuste aqui
 }

void imprimir(){ 
   for(Prova p : provas){
        System.out.println(p);
   }
}

Implicitly, the println method will call the toString method inherited from the Object class (mother of all java classes) and will bring the verbalization you want.

  • Man, it was just that msm! Yesterday morning a friend gave me a strength here and it worked , he showed practically what you did now , vlw Carlos !!

  • That’s it, @Ícarotimóteo . It is always advisable to mark your question as solved, when applicable. Other people can find the questions and answers more easily. This is the dynamic of this forum.

Browser other questions tagged

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