-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClockDemo.java
More file actions
163 lines (139 loc) · 3.47 KB
/
Copy pathClockDemo.java
File metadata and controls
163 lines (139 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
//Clock base class
class Clock
{
/**
* gets hours
* @return hours of current time in string
*/
public String getHours()
{
int h =new Date().getHours();
return Integer.toString(h);
}
/**
* gets minutes
* @return minutes of current time in string
*/
public String getMinutes()
{
int m =new Date().getMinutes();
return Integer.toString(m);
}
/**
* gets Seconds
* @return seconds of current time in string
*/
public String getSeconds()
{
int s =new Date().getSeconds();
return Integer.toString(s);
}
/**
* gets current time composed of hours and minutes
* @return time in string;
*/
public String getTime()
{
return getHours() + ":" + getMinutes() + ":"+ getSeconds();
}
/**
Returns the current Date and Time as a String.
*/
private String currentTime()
{
return LocalDateTime.ofInstant(Instant.now(),
ZoneId.systemDefault()).toString();
}
}
//WorldClock inherits Clock
class WorldClock extends Clock {
private int timeZoneDiff;
/**
* @return the timeZoneDiff
*/
public int getTimeZoneDiff() {
return timeZoneDiff;
}
/**
* @param timeZoneDiff the timeZoneDiff to set
*/
public void setTimeZoneDiff(int timeZoneDiff) {
this.timeZoneDiff = timeZoneDiff;
}
public WorldClock(int timeDiff) {
timeZoneDiff=timeDiff;
}
public String getHours()
{
//get current hours at home
int home_hours = Integer.parseInt(super.getHours());
//apply the time difference to get time at new zone
int new_hours = home_hours + timeZoneDiff;
if (new_hours > 24)
{
new_hours = new_hours - 24;
}
else if (new_hours < 0)
{
new_hours = new_hours + 24;
}
//convert to String
return Integer.toString(new_hours);
}
}
//AlarmClock inherits Clock
class AlarmClock extends Clock {
private int hour;
private int min;
private int sec;
public AlarmClock( int h,int m,int s) {
// TODO Auto-generated constructor stub
hour=h;
min=m;
sec=s;
}
// Your work goes here
/**
* Sets the alarm.
* @param hours hours for alarm
* @param minutes minutes for alarm
*/
public void setAlarm(int hours, int minutes, int seconds)
{
new AlarmClock(hours,minutes,seconds);
Clock c = new Clock();
int h = Integer.parseInt(c.getHours());
int m = Integer.parseInt(c.getMinutes());
int s = Integer.parseInt(c.getSeconds());
if(h==hours && m==minutes && s==seconds)
{
//alarm fires
System.out.println(c.getTime());
System.out.println("Alarm");
}
}
}
// Derived class that initiates the execution - Driver class
public class ClockDemo
{
public static void main(String[] args)
{
//test WorldClock
System.out.println("");
System.out.println("Testing WorldClock class");
int offset = 3;
System.out.println("Offset: " + offset);
WorldClock wclock = new WorldClock(offset);
System.out.println("Hours: " + wclock.getHours());
System.out.println("Minutes: " + wclock.getMinutes());
System.out.println("Seconds: " + wclock.getSeconds());
System.out.println("Time: " + wclock.getTime());
// System.out.println("Alarm Clock");
// AlarmClock ac = new AlarmClock(03,33,15);
// ac.setAlarm(3, 35, 15);
}
}