Java Reference
In-Depth Information
Next, let's give the definitions of the other methods of the
class
Clock
. These
definitions are simple and easy to follow.
public int
getHours()
{
return
hr;
//return the value of hr
}
public int
getMinutes()
{
return
min;
//return the value of min
}
public int
getSeconds()
{
return
sec;
//return the value of sec
}
public void
printTime()
{
if
(hr < 10)
System.out.print("0");
System.out.print(hr + ":");
if
(min < 10)
System.out.print("0");
System.out.print(min + ":");
8
if
(sec < 10)
System.out.print("0");
System.out.print(sec);
}
public void
incrementHours()
{
hr++;
//increment the value of hr by 1
if
(hr > 23)
//if hr is greater than 23,
hr = 0;
//set hr to 0
}
public void
incrementMinutes()
{
min++;
//increment the value of min by 1
if
(min > 59)
//if min is greater than 59
{
min = 0;
//set min to 0
incrementHours();
//increment hours
}
}
Search WWH ::
Custom Search