Your condition is wrong. When you write this:
if (newHour > -1 || newHour < 24) {
You really mean this:
if (newHour > -1 && newHour < 24) {
Answer from nicomp on Stack OverflowVideos
Your condition is wrong. When you write this:
if (newHour > -1 || newHour < 24) {
You really mean this:
if (newHour > -1 && newHour < 24) {
@nicomp is correct and you should also be using >= 24 and 60 instead of >. You might consider changing the constructor for Clock to
public Clock(int newHour, int newMinute, int newSecond) {
setHours(newHour);
setMinutes(newMinute);
setSeconds(newSecond);
}
and then do all of your validation in the set methods, instead of having some validation in the set methods and some in the constructor.
It is hard to tell what your question actually is, but this line:
seconds = seconds ++;
will NOT increment seconds. I have tried it:
int seconds = 0;
seconds = seconds++;
System.out.println(seconds);
prints 0.
The reason is that the ++ suffix applies the increment AFTER assigning the value to the variable, so you lose the increment.
Try simply
seconds++;
You could just make a toString function in your Time function. It makes a string with the 3 integers and you can print it in your main.
code TimeViewer:
public String toString(){
return hours + ":" + minutes + ":" + seconds
}
code Main:
TimeComponent timecomponent = new TimeComponent(0, 0, 0);
System.out.println(timecomponent);
I watched your code and it seems like your doing something weard in the constructor not linking with the class data (initial).