A simple digital alarm clock has a display, showing “hour:minute”, and four butt
ID: 3691528 • Letter: A
Question
A simple digital alarm clock has a display, showing “hour:minute”, and four buttons marked "Clock Set", "Alarm Set", "+", and "-". Normally the display shows the time. By pressing "Clock Set" the display shows the time but allows setting the clock by pressing "+" or "-" to increase or decrease the hour. Pressing "Clock Set" second time allows the changing the minutes, and pressing the third time returns the display to showing the time. By pressing "Alarm Set" the display shows the alarm time and allows setting the alarm hour by pressing "+" or "-" to increase or decrease the hour. Pressing "Alarm Set" second time allows the changing the minutes, and pressing the third time returns the display to showing the time. That is the alarm clock can be in one of the five states: "display time", "set Clock hour", "set Clock minute", "set Alarm hour", and "set alarm minute". This is a modification of the problem given in your book – Page 183. The hardware buttons of the clock invoke methods in the AlarmClock interface.
Sketch a State Pattern based design for implementing the alarm clock. Implement your production code.
Deliverables: Your complete production code and the test suite
given test case :
@Test
public void shouldHandleAll ( ) {
// show time mode
@Test
assertEquals ( "11:32",clock.readDisplay());
// + and - has no effect in show time mode
@Test
clock.increase() ;
assertEquals ( "11:32",clock.readDisplay()) ;
clock. decrease() ;
assertEquals ( "11:32",clock.readDisplay()) ;
// swi t c h t o s e t hour mode
@Test
clock .mode () ;
assertEquals ( "06:15",clock.readDisplay() ) ;
clock.increase() ; / / inc r ement hour by one
clock.increase() ;
clock.increase() ;
assertEquals ( "09:15",clock.readDisplay() ) ;
// swi t c h t o s e t minute mode
@Test
clock.mode() ;
clock.decrease() ;
clock.decrease() ;
clock.decrease() ;
clock.decrease() ;
clock.decrease() ;
assertEquals ( "09:10",clock.readDisplay() ) ;
// go b a c k t o show t ime mode
@Test
clock.mode() ;
assertEquals ( "11:32",clock.readDisplay()) ;
// r emembe r s t h e s e t a l a rm t ime .
@Test
clock.mode() ;
assertEquals ( "09:10",c lock.readDisplay() ) ;
}
Explanation / Answer
The fallowing code will works efficiently for an alaram..
1: /**
2: An alarm clock should include a time to sound the alarm as well as methods
3: to set the alarm.
4: */
5: public class AlarmClock extends Clock
6: {
7: public int alarmHour;
8: public int alarmMinute;
9: public int alarmSecond;
10:
11: //Constructors
12: public AlarmClock()
13: {
14: super();
15: alarmHour = 0;
16: alarmMinute = 0;
17: alarmSecond = 0;
18: }
19:
20: public AlarmClock(int theHour, int theMinute, int theSecond, int alarmH, int alarmM, int alarmS)
21: {
22: super(theHour, theMinute, theSecond);
23: setAlarmHour(alarmH);
24: setAlarmMinute(alarmM);
25: setAlarmSecond(alarmS);
26: }
27:
28: //Accessors
29: public void setAlarmHour(int alarmH)
30: {
31: if((alarmH >= 0) && (alarmH <= 24))
32: alarmHour = alarmH;
33: else
34: System.out.println("Fatal error: invalid alarm hour");
35: }
36:
37: public void setAlarmMinute(int alarmM)
38: {
39: if((alarmM >= 0) && (alarmM <= 59))
40: alarmMinute = alarmM;
41: else
42: System.out.println("Fatal error: invalid alarm minute");
43: }
44:
45: public void setAlarmSecond(int alarmS)
46: {
47: if((alarmS >= 0) && (alarmS <= 59))
48: alarmSecond = alarmS;
49: else
50: System.out.println("Fatal error: invalid alarm second");
51: }
52: //Mutators
53: public int getAlarmHour()
54: {
55: return alarmHour;
56: }
57:
58: public int getAlarmMinute()
59: {
60: return alarmMinute;
61: }
62:
63: public int getAlarmSecond()
64: {
65: return alarmSecond;
66: }
67:
68: public String getCurrentAlarmTime()
69: {
70: return "The alarm is set to " + alarmHour + ":" + alarmMinute + ":" + alarmSecond;
71: }
72:
73: //Facilitators
74: public String toString()
75: {
76: return "The current time is " + getHour() + ":" + getMinute() + ":" +
77: getSecond() + " The alarm is set to " + getAlarmHour() + ":" +
78: getAlarmMinute() + ":" + getAlarmSecond();
79: }
80:
81: public boolean equals(Object o)
82: {
83: if(o == null)
84: return false;
85: else if(getClass() != o.getClass())
86: return false;
87: else
88: {
89: AlarmClock otherClock = (AlarmClock) o;
90: return((getHour() == otherClock.getHour()) && (getMinute() == otherClock.getMinute())
91: && (getSecond() == otherClock.getSecond()) && (alarmHour == otherClock.alarmHour)
92: && (alarmMinute == otherClock.alarmMinute) && (alarmSecond == otherClock.alarmSecond));
93: }
94:
95: }
96:
97: }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.