-3

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;
    }
}
JUAN CALVOPINA M
  • 3,695
  • 2
  • 21
  • 37

2 Answers2

0

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;
}
Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
0
  1. You are using Caps for method names. Caps are for Class names.
  2. You aren't setting min. It's at a value of zero.
  3. You don't need the else if condition - just use else instead.

 public void colder() { // method name, make lower case.
    if(temp - increment >= min){
       temp = temp - increment;
    } else {
       System.out.println("Too cold");
    }
  }
Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
Dakoda
  • 175
  • 7