Skip to content

Commit d8c83ba

Browse files
committed
Updated.
1 parent 0d659c6 commit d8c83ba

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+6622
-50
lines changed

clock/Alarm.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
/*
44
* Object oriented programming with Java
55
* Spring 18
6-
* Assignment 1
6+
* Assignment 1 Second revision.
77
* File: Alarm.java (MWE)
88
* Description: Simple program that simulates a clock with alarm functionality.
9-
* Author: Svitri Magnusson
9+
* @author: Svitri Magnusson
1010
* CS username: kv13smn
11-
* Date: 2018-04-03
11+
* Date: 2018-04-23
1212
* Input: void
1313
* Output: console prints
1414
* Run: javac -cp /usr/share/java/junit4.jar <filenames>
@@ -35,11 +35,15 @@ public static void main(String[] args) throws LimitException {
3535
System.out.println(alarm.getTime());
3636

3737
}
38+
39+
System.out.printf("*Rewinding the clock to %02d:%02d*\n", hour, minute-1);
3840
alarm.setTime(13,36);
3941
System.out.println(alarm.getTime());
40-
System.out.println(alarm.isTriggered());
42+
System.out.println("Is the alarm triggered?: " + alarm.isTriggered());
43+
System.out.println("*TICK*");
4144
alarm.timeTick();
42-
System.out.println(alarm.isTriggered());
45+
alarm.getTime();
46+
System.out.println("Is the alarm triggered?: " + alarm.isTriggered());
4347

4448
}
4549
}

clock/AlarmClock.java

+32-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,44 @@
11
package clock;
22

3+
/*
4+
* Object oriented programming with Java
5+
* Spring 18
6+
* Assignment 1
7+
* File: AlarmClock.java
8+
* Description: This class is inherited from Clock.java and adds functions to set a time
9+
* when the users wants the alarm to go off.
10+
* Author: Svitri Magnusson
11+
* CS username: kv13smn
12+
* Date: 2018-04-23
13+
* Input: void
14+
* Output: console prints
15+
* Run: javac -cp /usr/share/java/junit4.jar <filenames>
16+
* java -cp -cp .:/usr/share/java/junit4.jar org.junit.runner.JUnitCore <main>
17+
*
18+
*/
19+
320
public class AlarmClock extends Clock {
421

522
private String alarmTime;
623
private boolean status = true;
724

825
AlarmClock() throws LimitException { }
926

27+
/* Creates an alarm clock object at specified time
28+
* for the alarm to go off.
29+
* @param hour, minute When the alarm will go off.
30+
* @throws LimitException if the values are set outside interval.
31+
*/
1032
AlarmClock(int hour, int minute) throws LimitException {
1133
setAlarm(hour, minute);
1234
}
1335

1436

15-
//Denna metod används för att ställa ett larm.
37+
/* Sets a new time for the alarm to go off.
38+
* Also activates the alarm if it was shut off.
39+
* @param hour, minute When the alarm will go off.
40+
* @throws LimitException if the values are set outside interval.
41+
*/
1642
public void setAlarm(int hour, int minute) throws LimitException {
1743
if (hour > 24 || hour < 0 || minute < 0 || minute > 59) {
1844
throw new LimitException("Min is bigger tha max limit!");
@@ -23,25 +49,26 @@ public void setAlarm(int hour, int minute) throws LimitException {
2349
}
2450

2551

26-
//Denna metod används för att avläsa om alarmet ringer (returnerar en bool).
52+
/*Asks if an alarm has gone off. Compares current time with set time.
53+
* @return Triggered boolean.
54+
*/
2755
public boolean isTriggered() {
2856

2957
if(this.getTime().equals(this.alarmTime) && this.status) {
3058
System.out.println("alarm");
31-
this.status = false;
3259
return true;
3360
}
3461
return false;
3562
}
3663

3764

38-
//Denna metod används för att slå på ett larm.
65+
//Turns an alarm on.
3966
public void turnOn() {
4067
this.status = true;
4168

4269
}
4370

44-
//Denna metod används för att stänga av ett larm.
71+
//Turns alarm off. Alarm can still be set, but won't work if it's off.
4572
public void turnOff() {
4673
this.status = false;
4774

clock/AlarmClockTest.java

+50-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,23 @@
55

66
import static org.junit.Assert.*;
77

8+
/*
9+
* Object oriented programming with Java
10+
* Spring 18
11+
* Assignment 1
12+
* File: AlarmClockTest.java (MWE)
13+
* Description: This class is inherited from Clock.java and adds functions to set a time
14+
* when the users wants the alarm to go off.
15+
* Author: Svitri Magnusson
16+
* CS username: kv13smn
17+
* Date: 2018-04-23
18+
* Input: void
19+
* Output: console prints
20+
* Run: javac -cp /usr/share/java/junit4.jar <filenames>
21+
* java -cp -cp .:/usr/share/java/junit4.jar org.junit.runner.JUnitCore <main>
22+
*
23+
*/
24+
825
public class AlarmClockTest {
926

1027

@@ -25,6 +42,11 @@ public void shouldThrowLimitExceptionMinutes() throws Exception {
2542
Clock n = new AlarmClock(2, -23);
2643
}
2744

45+
@Test(expected = LimitException.class)
46+
public void shouldThrowIllegalArgumentException() throws Exception {
47+
Clock n = new AlarmClock(-2, -23);
48+
}
49+
2850

2951
@Test
3052
public void initialStateTest() throws Exception {
@@ -34,35 +56,58 @@ public void initialStateTest() throws Exception {
3456

3557

3658
@Test
37-
public void setAlarmTimeTest() throws Exception {
59+
public void setAlarmTimeTest0() throws Exception {
3860
AlarmClock n = new AlarmClock();
3961
n.setAlarm(13,37);
4062
n.setTime(13,36);
4163
assertFalse(n.isTriggered());
64+
}
65+
66+
@Test
67+
public void setAlarmTimeTest1() throws Exception {
68+
AlarmClock n = new AlarmClock();
69+
n.setAlarm(13,37);
70+
n.setTime(13,36);
4271
n.timeTick();
4372
System.out.println(n.getTime());
4473
assertTrue(n.isTriggered());
4574
n.timeTick();
46-
System.out.println(n.getTime());
47-
assertFalse(n.isTriggered());
48-
4975
}
5076

5177

5278
@Test
53-
public void alarmSwitchTest() throws Exception {
79+
public void alarmSwitchTest0() throws Exception {
5480
AlarmClock n = new AlarmClock();
5581
n.setAlarm(13,37);
5682
n.turnOff();
5783
n.setTime(13,36);
5884
assertFalse(n.isTriggered());
85+
}
86+
87+
@Test
88+
public void alarmSwitchTest1() throws Exception {
89+
AlarmClock n = new AlarmClock();
90+
n.setAlarm(13,37);
91+
n.turnOff();
92+
n.setTime(13,36);
5993
n.timeTick();
6094
System.out.println(n.getTime());
6195
assertFalse(n.isTriggered());
96+
}
6297

98+
@Test
99+
public void alarmSwitchTest2() throws Exception {
100+
AlarmClock n = new AlarmClock();
101+
n.setAlarm(13,37);
102+
n.turnOff();
103+
n.setTime(13,36);
104+
n.timeTick();
105+
System.out.println(n.getTime());
63106
n.setAlarm(13,37);
64107
n.setTime(13,36);
65108
n.timeTick();
66109
assertTrue(n.isTriggered());
67110
}
111+
112+
68113
}

clock/Clock.java

+40-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,44 @@
11
package clock;
22

3+
/*
4+
* Object oriented programming with Java
5+
* Spring 18
6+
* Assignment 1
7+
* File: Clock.java
8+
* Description: Consists of two display classes. Basically a digital clock.
9+
* @author: Svitri Magnusson
10+
* CS username: kv13smn
11+
12+
* Date: 2018-04-23
13+
* Input: void
14+
* Output: console prints
15+
*
16+
* Run: javac -cp /usr/share/java/junit4.jar <filenames>
17+
* java -cp -cp .:/usr/share/java/junit4.jar org.junit.runner.JUnitCore <main>
18+
*
19+
*/
20+
321
public class Clock {
422

523
private NumberDisplay minutes = new NumberDisplay(0, 59);
624
private NumberDisplay hours = new NumberDisplay(0, 23);
725
private String displayString;
826

27+
/* Creates a Clock object.
28+
* @param hour, minute
29+
*
30+
* Default value will be set to 0, 0 if not arguments given.
31+
* one display for hours and one for minutes.
32+
*
33+
* @throws LimitException if the display values are outside
34+
* of the set limits.
35+
* @see NumberDisplay
36+
*/
37+
938
Clock() throws LimitException {
39+
if ( this.minutes == null || this.hours == null) {
40+
throw new IllegalArgumentException("Cannot assign null!");
41+
}
1042
this.minutes.setValue(0);
1143
this.hours.setValue(0);
1244
}
@@ -16,6 +48,7 @@ public class Clock {
1648
this.hours.setValue(hour);
1749
}
1850

51+
// Simulates the increase of one time unit.
1952
public void timeTick() {
2053
if(minutes.didWrapAround()) {
2154
this.hours.increment();
@@ -27,23 +60,26 @@ public void timeTick() {
2760

2861
}
2962

63+
/* Sets the clock to provided time.
64+
* @param hour, minute
65+
* @throws LimitException if the values are set outside interval.
66+
*/
67+
3068
public void setTime(int hour, int minute) throws LimitException {
3169
this.minutes.setValue(minute);
3270
this.hours.setValue(hour);
33-
3471
}
3572

73+
//Gets the current time of the clock as a formatted string.
3674
public String getTime() {
3775
String min = minutes.getDisplayValue();
3876
String hour = hours.getDisplayValue();
3977
String result = String.format("%s:%s", hour, min);
4078
return result;
4179
}
4280

43-
81+
//Updates the display attribute.
4482
private void updateDisplay() {
4583
this.displayString = this.getTime();
4684
}
47-
48-
4985
}

0 commit comments

Comments
 (0)