Call the Java method

Asked

Viewed 153 times

0

I’m doing some POO exercises and I can’t call the method ligar of my code.

    public class Telefone {
    public String modelo;
    public int numeros;
    public boolean antena;

    void status(){
        System.out.println("O modelo do telefone é: " + this.getModelo());
        System.out.println("O telefone possui " + this.getNumeros() + "Numeros");
        System.out.println("A antela dele está pronta? " + this.getAntena());
        System.out.println("O telefone pode ligar?" + this.ligar());
    }

    public String getModelo(){
        return this.modelo;
    }
    public void setModelo(String Modelo){
        modelo = Modelo;
    }

    public int getNumeros(){
        return this.numeros;
    }
    public void setNumeros(int Numeros){
        numeros = Numeros;
    }

    public boolean getAntena(){
        return this.antena;
    }
    public void setAntena(boolean Antena){
        antena = Antena;
    }
    void ligar(){
        if(this.antena == true){
            System.out.println("Estou ligando!");
        } else if (this.antena == false){
            System.out.println("Não posso ligar!");
        }
    }

}
  • Put public on the call method.

5 answers

3

There are some problems in this code but I will focus on the question. This method ligar() has no real function there and should not exist, everything can be simplified:

void status(){
    System.out.println("O modelo do telefone é: " + getModelo());
    System.out.println("O telefone possui " + getNumeros() + " Numeros");
    System.out.println("A antela dele está pronta? " + getAntena());
    System.out.println("O telefone pode ligar? " + (antena ? "Estou ligando!" : "Não posso ligar!"));
}

I put in the Github for future reference.

Solve the root cause of the problem instead of looking for alternatives to circumvent the original problem created.

Think about whether we can eliminate this last question and already make the statement, or simplify the answer, it seems a redundant text.

2

In case the link() method is void type, then there is no return, but you call it in println as if it returned something. You should just call it:

void status(){
System.out.println("O modelo do telefone é: " + this.getModelo());
System.out.println("O telefone possui " + this.getNumeros() + "Numeros");
System.out.println("A antela dele está pronta? " + this.getAntena());
this.ligar();
}

void ligar(){
if(this.antena == true){
    System.out.println("Estou ligando!");
} else if (this.antena == false){
    System.out.println("Não posso ligar!");
}
}    

1

First this is within a class right? Let’s assume that this class is

public class Telefone{}

After you have defined the methods of this class, you should instantiate it in your main class or your main method in your own class as in the example below if it is a method:

public static void main(String args[]){

    Telefone telefone = new Telefone(); // Passando os parametros da sua classe dentro dos parenteses.
    telefone.ligar(); // após isso é so utilizar os metodos que você deseja, chamando a classe e logo em seguida o seu metodo.

}

Or if you wish to use this method within any of your functions just call this.ligar(); where you want to use it.

0


You were trying to concatenate with a String the call of a method of the type void, i.e., that does not return any value, as follows: System.out.println("O telefone pode ligar?" + this.ligar());. If your intention is to know if it is possible to connect or not, make the following change to your code by adding the method call in the next line void, to get the desired result in your code:

void status(){
    System.out.println("O modelo do telefone é: " + this.getModelo());
    System.out.println("O telefone possui " + this.getNumeros() + " Numeros");
    System.out.println("A antela dele está pronta? " + this.getAntena());
    System.out.println("O telefone pode ligar?");
    this.ligar();
}

-3

Declares the status last. Tip: It’s kind of useless to create getters and setters if the variables are public, create a new class and leave them private and make the getters and setters. And in another class call them.

Browser other questions tagged

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