53.3k views
2 votes
Class Time {

private int hour;

private int minute;

private int second;

public Time( ) { }

public Time(int hr , int min, int sec) { }

public void setTime(int hr , int min, int sec) { }

public void printTime( ) { }

public void tick( ) {

// tick method defined

}

}

The class Time defines a time object. It represents time (military time) in 2400 hours.

10 AM = 10 hours, 0 minutes and 0 seconds

4 PM = 16 hours, 0 minutes and 0 seconds

7:26PM = 19 hours, 26 miunutes and 0 seconds

Assume 0 represents 00 in this case.

1) Implement the two constructor:

Time(): will set the instance variables to zero.

Time(int hr , int min, int sec): will set the time with parameters passed in.

1 Answer

5 votes

Final answer:

The question involves implementing two constructors for a Java class representing time in military format. The default constructor initializes time to 00:00:00, and the parameterized constructor sets the time to the provided hour, minute, and second.

Step-by-step explanation:

The student is asking for the implementation of two constructors for a Time class in Java. The constructors will initialize the instance variables of the class, which represent military time.

The default constructor Time() sets the hour, minute, and second to zero. The parameterized constructor Time(int hr, int min, int sec) sets the hour, minute, and second to the values passed to the constructor.

Implementing Constructors:

public Time() {
this.hour = 0;
this.minute = 0;
this.second = 0;
}

public Time(int hr, int min, int sec) {
this.hour = hr;
this.minute = min;
this.second = sec;
}

User Payal Bansal
by
7.9k points