Time as a string

Report a typo

You are given a class named Time. It has three int fields: hours, minutes and seconds. Override the method toString in the class to return a string representation of an object.

The overridden method must return a string with hours, minutes and seconds separated by colons. If a number contains only a single digit, add a zero first.

Here are some examples: "23:59:59", "11:08:05", "01:01:01".

Write a program in Java 17
class Time {

private int hours;
private int minutes;
private int seconds;

public Time(int hours, int minutes, int seconds) {
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
}
}
___

Create a free account to access the full topic