Pass Task 3.2: Clock: Swinburne University of Technology

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Swinburne University of Technology

Object Oriented Programming (2020 S2)


Doubtfire Submission

Pass Task 3.2: Clock

Submitted By:
viren shah Tutor:
102110529 Matt Noone
2020/09/18 15:04

September 18, 2020

Produced by Doubtfire
File 1 of 7 Clock Class

1 using System;
2 using System.Collections.Generic;
3 using System.Data;
4 using System.Text;
5

6 namespace Task3_2
7 {
8 public class Clock
9 {
10 private counter _hour;
11 private counter _minute;
12 private counter _second;
13 private string _time;
14

15 public string time


16 {
17 get
18 {
19 return _time;
20 }
21

22 }
23

24 public Clock() // track the value


25 {
26 _hour = new counter("hour");
27 _minute = new counter("minute");
28 _second = new counter("second");
29 _time = "time";
30

31 }
32

33 public void tick() // increment


34 {
35 _second.Increment();
36 if (_second.Value > 59)
37 {
38 _minute.Increment();
39 _second.reset();
40

41 }
42

43 if (_minute.Value > 59)


44 {
45 _hour.Increment();
46 _minute.reset();
47 }
48

49 if (_hour.Value > 23)


50 {
51 _hour.reset();
52 }
53

Page 1 of 11
File 1 of 7 Clock Class

54 }
55

56 public string DisplayTime() // value on the screen


57 {
58 return _hour.Value.ToString("D2")+":"+_minute.Value.ToString("D2") +
,→ ":"+_second.Value.ToString("D2");
59 }
60

61 public void reset()


62 {
63 _second.reset();
64 _minute.reset();
65 _hour.reset();
66 }
67 }
68 }

Page 2 of 11
File 2 of 7 Counter Class

1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4

5 namespace Task3_2
6 {
7 public class counter
8 {
9 private string _name;
10 private int _count;
11

12 public int Count


13 {
14 get
15 {
16 return _count;
17 }
18 }
19

20 public string Name


21 {
22 get
23 {
24 return _name;
25 }
26

27 set
28 {
29 _name = value;
30 }
31 }
32

33 public int Value


34 {
35 get
36 {
37 return _count;
38 }
39 }
40

41 public void Increment()


42 {
43 _count++;
44 }
45

46 public void reset()


47 {
48 _count = 0;
49 }
50

51 public counter(string name)


52 {
53 _name = name;

Page 3 of 11
File 2 of 7 Counter Class

54 _count = 0;
55

56 }
57

58

59

60 }
61 }

Page 4 of 11
File 3 of 7 Program Class

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading;
6 using System.Threading.Tasks;
7

8 namespace Task3_2
9 {
10 class Program
11 {
12

13 static void Main(string[] args)


14 {
15 Clock _clock = new Clock();
16 for (int i = 0; i < 86400; i++)
17 {
18 _clock.tick();
19 Thread.Sleep(1000);
20 Console.WriteLine(_clock.DisplayTime());
21 }
22

23 Console.ReadLine();
24 }
25 }
26 }

Page 5 of 11
File 4 of 7 Clock Unit Tests

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 using NUnit.Framework;
7 using Task3_2;
8

9 namespace n_Unit
10 {
11 [TestFixture]
12 public class Clock_test
13 {
14 private Clock _test_clock;
15

16 [SetUp]
17

18 public void Setup()


19 {
20 _test_clock = new Clock();
21 }
22

23

24 [TestCase("00:00:00")]
25

26 public void Test_default(string to_test_true)


27 {
28 Assert.IsTrue(_test_clock.DisplayTime()==to_test_true);
29 }
30

31

32

33 [TestCase("00:00:20")]
34 public void Test_ticks_sec(string to_test_true)
35 {
36 for (int i = 0; i < 20; i++)
37 {
38 _test_clock.tick();
39 }
40 Assert.IsTrue(_test_clock.DisplayTime() == to_test_true);
41 }
42

43

44 [TestCase("00:01:00")]
45 public void Test_ticks_min(string to_test_true)
46 {
47 for (int i = 0; i < 60; i++)
48 {
49 _test_clock.tick();
50 }
51 Assert.IsTrue(_test_clock.DisplayTime() == to_test_true);
52 }
53

Page 6 of 11
File 4 of 7 Clock Unit Tests

54

55 [TestCase("01:00:00")]
56 public void Test_ticks_hrs(string to_test_true)
57 {
58 for (int i = 0; i < 3600; i++)
59 {
60 _test_clock.tick();
61 }
62 Assert.IsTrue(_test_clock.DisplayTime() == to_test_true);
63 }
64

65

66 [TestCase("00:00:00")]
67 public void Test_roll_over(string to_test_true)
68 {
69 for (int i = 0; i < 60*60*24; i++)
70 {
71 _test_clock.tick();
72 }
73 Assert.IsTrue(_test_clock.DisplayTime() == to_test_true);
74 }
75

76 [TestCase("00:00:00")]
77 public void Test_reset(string to_test_true)
78 {
79 for (int i = 0; i < 60 * 60 * 25; i++)
80 {
81 _test_clock.tick();
82 }
83 _test_clock.reset();
84 Assert.IsTrue(_test_clock.DisplayTime() == to_test_true);
85 }
86

87

88 }
89 }

Page 7 of 11
File 5 of 7 Screenshot

Page 8 of 11
File 5 of 7 Screenshot

Page 9 of 11
File 6 of 7 Screenshot Unit Tests

Page 10 of 11
File 7 of 7 Image of UML diagram

Page 11 of 11

You might also like