Are you sure, it is for Java? It does not contain Const modifiers for functions.
Anyway, here is the solution, I think it will be simple to adapt to any other language.
Time.java:
public class Time {
int hours;
int minutes;
int seconds;
public Time() {
hours = 0;
minutes = 0;
seconds = 0;
}
public Time(int h, int m, int s) {
hours = h;
minutes = m;
seconds = s;
}
public String toString() {
return String.format("%d:%02d:%02d", hours, minutes, seconds);
}
public void add(Time time1, Time time2) {
hours = time1.hours + time2.hours;
minutes = time1.minutes + time2.minutes;
seconds = time1.seconds + time2.seconds;
minutes += seconds / 60;
seconds %= 60;
hours += minutes / 60;
minutes %= 60;
}
}
Main.java:
public class Main {
public static void main(String[] args) {
final Time time1 = new Time(0, 0, 2);
final Time time2 = new Time(11, 59, 59);
final Time time3 = new Time();
time3.add(time1, time2);
System.out.println(time3.toString());
}
}
Comments
Thank you very much sir!!! . Surely It is my Java assignment program and I couldn't able to solve. I don't think so about const functions. May be my ma'am might copy the questions from somewhere and gave it to us. Anyways Thank you very much!!!
Leave a comment