jobosno
jobosno

Reputation: 3

Java getter returning original value instead of value updated by setter

I'm doing a project to make filters for a photo, where we set new RGB values with getter and setter methods.

public class Color {

private double red;
private double green;
private double blue;

public Color() {
this.setRed(1.0);
this.setGreen(1.0);
this.setBlue(1.0);
}

public Color(double red, double green, double blue) {
this.setRed(red);
this.setGreen(green);
this.setBlue(blue);
}

... (code for other functions after those tests pass)

public double getRed(){
  return red;
}

public void setRed(double color1){
  if (color1 <= 1.0){
      if (color1 >= 0.0){
          red = color1;
      }
  }
  else if (color1>1.0){
      red=1.0;
  }
  else if (color1<0.0){
      red=0.0;
  }
}

}

Each of the getters and setters are duplicates of getRed/setRed. I've checked the definitions via a program I do not have access to, but this is the output I have received:

Failed tests:

Scenario: 3.1 - after calling setRed(-1.0), getRed()'s return value should be 0.0

Color(1.0,1.0,1.0).setRed(-1.0) - OK

Color(1.0,1.0,1.0).getRed() - expected 0.0 - returned 1.0

So the setter is functioning as I need it to, but the getter is returning the original value. It's not an isolated case, each of my getters is returning the original value (1.0) for their respective colors while each setter successfully runs.

Upvotes: 0

Views: 592

Answers (1)

ctomek
ctomek

Reputation: 1796

Your last condition else if (color1<0.0) { red=0.0; } will never be true, because all cases when color1 is less than 0 will be caught in first if statement if (color1 <= 1.0). Hence, value 0.0 will never be set.

Upvotes: 2

Related Questions