In: Computer Science
public class Clock { private int hr; private int min; private int sec; public Clock() { setTime(0, 0, 0); } public Clock(int hours, int minutes, int seconds) { setTime(hours, minutes, seconds); } public void setTime(int hours, int minutes, int seconds) { if (0 <= hours && hours < 24) hr = hours; else hr = 0; if (0 <= minutes && minutes < 60) min = minutes; else min = 0; if(0 <= seconds && seconds < 60) sec = seconds; else sec = 0; } public int getHours() { return hr; } public int getMinutes() { return min; } public int getSeconds() { return 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 + ":"); if (sec < 10) System.out.print("0"); System.out.print(sec); } public void incrementSeconds() { sec++; if (sec > 59) { sec = 0; incrementMinutes(); } } public void incrementMinutes() { min++; if (min > 59) { min = 0; incrementHours(); } } public void incrementHours() { hr++; if(hr > 23) hr = 0; } public boolean equals(Clock otherClock) { return (hr == otherClock.hr && min == otherClock.min && sec == otherClock.sec); } public void makeCopy(Clock otherClock) { hr = otherClock.hr; min = otherClock.min; sec = otherClock.sec; } public Clock getCopy() { Clock temp = new Clock(); temp.hr = hr; temp.min = min; temp.sec = sec; return temp; } }
We learned the above class Clock, which was designed to implement the time of day in a program. Certain application in addition to hours, minutes, and seconds might require you to store the time zone. Please do the following:
This java code contains ExtClock class and it is tested in main function .
import java.util.Calendar;
import java.util.TimeZone;
class Clock
{
private int hr;
private int min;
private int sec;
public Clock()
{
setTime(0, 0, 0);
}
public Clock(int hours, int minutes, int seconds)
{
setTime(hours, minutes, seconds);
}
public void setTime(int hours, int minutes, int seconds)
{
if (0 <= hours && hours < 24)
hr = hours;
else
hr = 0;
if (0 <= minutes && minutes < 60)
min = minutes;
else
min = 0;
if(0 <= seconds && seconds < 60)
sec = seconds;
else
sec = 0;
}
public int getHours()
{
return hr;
}
public int getMinutes()
{
return min;
}
public int getSeconds()
{
return 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 + ":");
if (sec < 10)
System.out.print("0");
System.out.print(sec);
}
public void incrementSeconds()
{
sec++;
if (sec > 59)
{
sec = 0;
incrementMinutes();
}
}
public void incrementMinutes()
{
min++;
if (min > 59)
{
min = 0;
incrementHours();
}
}
public void incrementHours()
{
hr++;
if(hr > 23)
hr = 0;
}
public boolean equals(Clock otherClock)
{
return (hr == otherClock.hr && min == otherClock.min
&& sec == otherClock.sec);
}
public void makeCopy(Clock otherClock)
{
hr = otherClock.hr;
min = otherClock.min;
sec = otherClock.sec;
}
public Clock getCopy()
{
Clock temp = new Clock();
temp.hr = hr;
temp.min = min;
temp.sec = sec;
return temp;
}
}
/* creation of new class ExtClock that is derived from class Clock
*/
class ExtClock extends Clock
{
public String Tzone; //data member to store the timezone
public void getTzone() //method which displays the current
timezone
{
Calendar n = Calendar.getInstance(); //this is to get calendar
instance
/* java Calendar class and Timezone class are imported at the top
of the code */
/* getTimeZone method of Calendar class is used to retrieve current
timezone */
TimeZone tzone = n.getTimeZone();
Tzone=tzone.getDisplayName(); /*to store the timezone to Tzone
variable using getDisplayName method of TimeZone class */
System.out.println("current timezone is : " +Tzone );
}
}
public class Main{
public static void main(String[] args) {
ExtClock c = new ExtClock();
c.getTzone(); //This displays the time zone
}
}
//This code will give the correct output
Output
current timezone is : Coordinated Universal Time
NB
Test program is also included with that code.Its is tested successfully and out put is obtained.
if test program needed , then here it is
(before that make changes to the java code by making the classes Clock and ExtClock to a package named Pclock by simply writing "package Pclock; " at top of the code ie top of importing calendar class and save it with file name of that class . Remove void main code from that package)
program to test the ExtClock class
import Pclock.*; //to import the Pclock package
public class Main{
public static void main(String[] args) {
ExtClock c = new ExtClock();
c.getTzone(); //This displays the time zone
}
}