I'm trying to write a Time object that has an increment method adding arbitrary seconds to it, so here is the code :
class Time {
int hour, minute;
double second;
public Time(int hour, int minute, double second) {
this.hour = hour;
this.minute = minute;
this.second = second;
}
public Time(double seconds) {
this.hour = (int) (seconds/3600);
seconds -= this.hour*3600;
if (this.hour >= 24) this.hour -= 24;
this.minute = (int) (seconds/60);
seconds -= this.minute*60;
this.second = seconds;
}
public static double convertToSeconds(Time t) {
double seconds = t.hour*3600 + t.minute*60 + t.second;
return seconds;
}
public static void increment(Time t, double s) {
double seconds = convertToSeconds(t) + s;
t = new Time(seconds);
printTime(t);
}
public static void printTime(Time t) {
System.out.println(t.hour + ":" + t.minute + ":" + t.second);
}
public static void main(String[] args) {
Time t = new Time(11, 8, 3.0);
printTime(t);
increment(t, 60);
printTime(t);
}
}
It gives this output :
11:8:3.0
11:9:3.0
11:8:3.0
It shows the object has been changed as expected inside the increment method, but back in main after the invocation of the increment method, the object remains the same. What's the problem with it?