Search code examples
javabluej

Is this a legal if sentance?


I just started coding and can't get this "Heater" object to work. It just prompts the if else statement "its too hot" when trying to give temp a higher value and it does not stop/ goes below the min temp value when calling the "colder" method. Thanks

public class Heater {
    private double temp;
    private double min;
    private double max;
    private double increment;

    public Heater(double min, double max) {
        temp = 15;
        increment = 5;
    }

    public void Warmer() {
        if(temp + increment <= max) {
            temp = temp + increment;
        } else if(temp + increment > max) {
            System.out.println("Too hot");
        }
    }

    public void Colder() {
        if(temp - increment >= min){
            temp = temp - increment;
        } else if (temp - increment < min){
            System.out.println("Too cold");
        }
    }

    public double getTemp() {
        return temp;
    }
}

Solution

  • You're not setting min and max in your constructor, so they stay at 0 by default. Try this:

    public Heater(double min, double max) {
        this.min = min;
        this.max = max;
        temp = 15;
        increment = 5;
    }