The program cannot call the set method from another class, even with it instantiated

Asked

Viewed 114 times

1

When I call the method setNomedoMetodo() in another class, even with it instantiated, appears:

The method setNaipe(String[]) is Undefined for the type Deck.

I can’t put a value on that method. And I can’t mix the cards either. Follow an excerpt from the code:

Carta Class

public class Carta {
    private String[] naipe;
    private String[] nome;

    //Métodos especiais
    public String[] getNaipe() {
        return naipe;
    }
    public void setNaipe(String[] naipe) {
        this.naipe = naipe;
    }
    public String[] getNome() {
        return nome;
    }
    public void setNome(String[] nome) {
        this.nome = nome;
    }

}         

Deck class

package Jogo;

import java.util.Random;

public class Baralho {
    Carta[] cartas = new Carta[52];
    String[] naipes = {"Copas", "Espada", "Ouros", "Paus"};
    String[] nomes = {"As", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
    String coringa;
    Random aleatorio = new Random();
    public Baralho() {
        int cont = 0;
        for (String naipe : naipes) {
            for (String nome : nomes) {
                Carta cartas = new Carta();
                this.setNaipe(naipes);
                this.setNome(nomes);
                this.cartas[cont] = cartas;
                this.embaralha(naipes);
                cont++;
            }
            this.setNaipe(coringa);
        }
        System.out.println(cartas);//Teste
    }
    public void embaralha(String[] carta) {
        aleatorio.naipes();
    }
    public void daCarta() {
        for (int i = 0; i < cartas.length; i++) {
            if (cartas[0] == null) {
                break;
            }else {
                System.out.println(cartas[0]);
            }
        }
    }
    public boolean temCarta() {
        boolean TouF = true;
        for (int i = 0; i < cartas.length; i++) {
            if (cartas[i] != null) {
                TouF = false;
            }else {
                TouF = true;
            }
        }
        return TouF;
    }
    public void imprime() {
        for (int i = 0; i < cartas.length; i++) {
            System.out.println(cartas[i]);
        }
    }
}
  • Add the full deck class.

  • I added the whole class

1 answer

0


The method setNaipe() does not exist in the class Baralho, calling in the form below:

this.setNaipe(naipes);

you are trying to call the method as if it existed in the class Baralho. If the goal is to call from the class Cartas, you should call the method from some instance of this class:

for (String naipe : naipes) {
    for (String nome : nomes) {
        Carta cartas = new Carta();
        cartas.setNaipe(naipes);
        cartas.setNome(nomes);
        this.cartas[cont] = cartas;
        this.embaralha(naipes);
        cont++;
    }

I didn’t understand the purpose of the line this.setNaipe(coringa); just below the above loop, but this line will also not work for the same reason as above.

  • Thank you very much, it helped a lot. The line this.setNaipe(joker) was to add the joker card. But as for Method shuffle, I can’t mix the cards.

  • 1

    @Pray there and other different doubts. It is better to ask another question

Browser other questions tagged

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