Why can’t I print according to the state?

Asked

Viewed 99 times

-3

public class Lampada {

private boolean ligada = true;

/*
 * Métodos acessores e modificadores
 */
public void setLigada(boolean alteraEstado) {
    this.ligada = alteraEstado;
}

public boolean isLigada() {
    return ligada;
}

}

====

It doesn’t work, I did it this other way

   public class Main {

public static void main (String [] args) {

    Scanner sc = new Scanner(System.in);
    System.out.println("Comando 'ligar' ou 'desligar' lampada.");

    String comando = sc.nextLine();
    Lampada lampada = new Lampada();

    switch (comando) {

        case "ligar":
            lampada.setLigada(true);
            System.out.println("Lampada ligada.");
            break;

        case "desligar":
            lampada.setLigada(false);
            System.out.println("Lampada desligada.");
            break;

        }
    }
} 
  • Where are you running the files? (for example, in an IDE or command prompt) and in the new version, you modified the class Lampada?

  • Maybe you forgot to recompile the files, I tested the code and it worked

  • I used the class Lampada that @Maniero suggested and the class Main of its edition

  • One of the possibilities is that Java is somehow getting confused when performing operations, try to delete the files from Lampada and of Main and try again

2 answers

2

There are some gross errors in the code, it seems random things, when it solves this compiles and works (I think, I don’t know what I should do). There’s a lot of stuff out there or that doesn’t do anything useful. I solved the problem, but the code still doesn’t make any sense, I tried to improve without making too many assumptions.

There are things that are not error, but could be better, for example, the method setLigada() doesn’t make sense because I shouldn’t get anything, I should just call. To tell you the truth, I don’t understand why you’re using a set there, it is to turn on, period. And then there is a method to turn off. This is one of the usage errors of getters/setters I always talk (give a searched here).

import java.util.Scanner;

class Lampada {
    private boolean ligada = true;
    public void setLigada(boolean alteraEstado) {
        this.ligada = alteraEstado;
    }
    public boolean isLigada() {
        return ligada;
    }
}

class Main {
    public static final Scanner sc = new Scanner(System.in);
    public static void main (String [] args) {
        System.out.println("Comando 'ligar' ou 'desligar' lampada.");
        String comando = sc.nextLine();
        Lampada lampada = new Lampada();
        switch (comando) {
            case "ligar":
                lampada.setLigada(true);
                System.out.println("Lampada ligada.");
                break;
            case "desligar":
                lampada.setLigada(false);
                System.out.println("Lampada desligada.");
                break;
        }
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • It’s probably because you have the floor public, within methods do not put access modifiers

0


Try:

Main:

import java.util.Scanner;

public class Main {
  public static void main (String [] args) {

    Scanner sc = new Scanner(System.in);
    System.out.println("Comando 'ligar' ou 'desligar' lampada.");

    String comando = sc.nextLine();
    Lampada lampada = new Lampada();

    switch (comando) {
      case "ligar":
        lampada.setLigada(true);
        System.out.println("Lampada ligada.");
        break;
      case "desligar":
        lampada.setLigada(false);
        System.out.println("Lampada desligada.");
        break;
    }
  }
}

Lampada:

class Lampada {
    private boolean ligada = true;
    public void setLigada(boolean alteraEstado) {
        this.ligada = alteraEstado;
    }
    public boolean isLigada() {
        return ligada;
    }
}

Log: (I had trouble with the images so I’ll put it in text anyway)

Bind:

Comando 'ligar' ou 'desligar' lampada.
ligar
Lampada ligada.

Hang up:

Comando 'ligar' ou 'desligar' lampada.
desligar
Lampada desligada.
  • Strange, when I tested it worked really off

  • Sure, wait a while, I’ll show prints

  • Try adding a default in the switch-case and see what happens

Browser other questions tagged

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