-1
public MyTime nextSecond()
 {
  if(getSecond()>=0||getSecond()<=58)
   return new MyTime(getHour(),getMinute(),getSecond()+1);
  else if(getSecond()==59)
   return new MyTime(getHour(),getMinute(),0);
  else
   throw new IllegalArgumentException("Invalid Second!");
 }

 public MyTime nextMinute()
 {
  if(getMinute()>=0||getMinute()<=58)
   return new MyTime(getHour(),getMinute()+1,0);
  else if(getMinute()==59)
   return new MyTime(getHour()+1,0,0);
  else
   throw new IllegalArgumentException("Invalid Minute!");
 }


 public MyTime nextHour()
 {
  if(getHour()>=0||getHour()<=22)
   return new MyTime(getHour()+1,0,0);
  else if(getHour()==23)
   return new MyTime(0,0,0);
  else
   throw new IllegalArgumentException("Invalid Hour!");
 }
}

I am a new programmer and this is my code, it doesn't have any errors but if statements are not executing!

Does anyone know why it isn't working?

SaiyanToaster
  • 138
  • 1
  • 1
  • 10

2 Answers2

2

In if condition if the first statement is correct and the extra conditional is combined with OR then it return true although the second condition is false it shouldn't be OR statement between conditions it should be AND

Abbas Elmas
  • 422
  • 6
  • 16
1

You are using logical OR where you should be using logical AND.

For example this:

if (getSecond() >= 0 || getSecond() <= 58)

should be

if (getSecond() >= 0 && getSecond() <= 58)

In your version if the value returned by getSecond() is 59 it will never reach the else if because the first if statement will evaluate getSecond() > 0 to true and because it is logical OR, it will not evaluate the second logical expression in that condition (getSecond() <= 58).

The same goes for minutes and hours.

Bohuslav Burghardt
  • 33,626
  • 7
  • 114
  • 109