Answer to Question #228198 in Java | JSP | JSF for Macwin

Question #228198
Create a class called time that has separate int member data for hours, minutes and seconds. One constructor should initialize this data to 0, and another should initialize it to fixed values. Another member function should display it, in 11:59:59 format. The final member function should add two objects of type time passed as arguments.
A main () program should create two initialized time objects (should they be const?) and one that isn't initialized. Then it should add the two initialized values together, leaving the result in the third variable. Finally it should display the value of this variable. Make appropriate member functions const
1
Expert's answer
2021-08-21T09:04:40-0400

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());
    }
}




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Macwin
21.08.21, 20:43

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

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS