The program can not call the set method of another class, even with it instantiated

1

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

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

I can not put a value in this method. Nor can I mix the cards. Here's a snippet of code:

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

}         

Class Deck

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]);
        }
    }
}
    
asked by anonymous 01.05.2018 / 22:11

1 answer

1

The setNaipe() method does not exist in class Baralho , calling as follows:

this.setNaipe(naipes);

You are trying to call the method as if it existed in the Baralho class. If the goal is to call the Cartas class, you must 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 did not understand the purpose of the line this.setNaipe(coringa); just below the loop above, but this line will not work for the same reason above.

    
01.05.2018 / 22:41