public class AlarmClock {
private int curHour;
private int curMinute;
private int alarmHour;
private int alarmMinute;
public static void main(String[] args) {
AlarmClock myClock = new AlarmClock();
myClock.run();
}
public void AlarmClock() {
curHour = alarmHour = curMinute = alarmMinute = 0;
}
public void run() {
setTime(4,30);
show();
setTime(0,0);
show();
setAlarm(5,55);
System.out.println("Alarm is " + isAlarm());
setTime(5,55);
System.out.println("Alarm is " + isAlarm());
}
public void show() {
System.out.println("Current time is " +
curHour + ":" + curMinute);
}
public boolean isAlarm() {
if(curHour == alarmHour &&
curMinute == alarmMinute)
return true;
else
return false;
}
public int getAlarmHour() {
return alarmHour;
}
public int getAlarmMinute() {
return alarmMinute;
}
public void setAlarm (int hour, int minute) {
if(hour >=0 && hour <= 23 && minute >=0 && minute <= 59) { alarmHour = hour; alarmMinute = minute; } } public int getTimeHour () { return curHour; } public int getTimeMinute() { return curMinute; } public void setTime (int hour, int minute) { if(hour >= 0 && hour <= 23 && minute >= 0 && minute <= 59) {
curHour = hour;
curMinute = minute;
}
}
}
AlarmClock.java output
Current time is 4:30
Current time is 0:0
Alarm is false
Alarm is true
Tag: Study Code Program Java
Should you not import something? Because this just prints out the current time.
ReplyDeleteWhat about a project that can really displays real time not only the current time.
ReplyDeleteIt is possible right? Can someone post their codes for this?
:)
Thanks.
This comment has been removed by the author.
ReplyDelete