0
1
00:00:00,870 --> 00:00:06,750
So, now that we've created a pizza calculator using computed and observe properties, I want to set you
1

2
00:00:06,780 --> 00:00:10,990
a challenge to build your own Computed Paint Calculator.
2

3
00:00:11,130 --> 00:00:18,600
So go ahead and create a brand-new playground and delete all of the code other than import foundation.
3

4
00:00:18,600 --> 00:00:21,010
Now, we're going to create a variable called width,
4

5
00:00:21,210 --> 00:00:26,280
and this is going to be a float, and we're going to start it off with, say, 1.5.
5

6
00:00:26,280 --> 00:00:31,170
So we have a wall that has a width of 1.5 meters.
6

7
00:00:31,170 --> 00:00:36,830
Now, the next thing we're going to create is a variable called height and it, again, is going to be a float,
7

8
00:00:37,110 --> 00:00:39,520
and this is going to be 2.3 meters.
8

9
00:00:39,540 --> 00:00:47,460
So we've got a wall that is 1.5 by 2.3. And now I want to be able to work out the number of buckets of
9

10
00:00:47,490 --> 00:00:52,170
paint that I need in order to cover this wall with paint.
10

11
00:00:52,420 --> 00:01:02,000
So let's say that a single bucket of paint only covers 1.5 meter squared area,
11

12
00:01:02,140 --> 00:01:10,150
then can you use what we've learned about computed properties, create a computed property called
12

13
00:01:10,540 --> 00:01:18,550
bucketsOfPaint, such that whenever I tried to print buckets of paint, then it should be able to show me the
13

14
00:01:18,610 --> 00:01:25,530
updated number of buckets I need to buy in order to cover my wall with this width and height.
14

15
00:01:25,720 --> 00:01:31,840
So have a think about how you would achieve that, and Pause the video and complete the challenge.
15

16
00:01:36,180 --> 00:01:41,310
Now,let's leave this print statement down here so that once it works, then all of the errors will go
16

17
00:01:41,310 --> 00:01:41,930
away.
17

18
00:01:42,360 --> 00:01:50,100
But let's go ahead and define our computed property. Now, buckets of paint is going to be an integer because,
18

19
00:01:50,190 --> 00:01:53,990
usually, the only cell buckets of paint in entire buckets,
19

20
00:01:54,000 --> 00:01:54,750
right?
20

21
00:01:54,780 --> 00:02:02,700
So inside here, we're going to set the getter for our property and we're going to first work out the area
21

22
00:02:02,700 --> 00:02:09,010
that we need to cover, and areas cause worked out by width being multiplied by height.
22

23
00:02:10,280 --> 00:02:12,520
And now we have our area.
23

24
00:02:12,740 --> 00:02:21,550
Now, previously, we said that per bucket of paint, it covers 1.5 meters squared of area.
24

25
00:02:21,830 --> 00:02:31,250
So areaCoveredperBucket to equal a Float that is 1.5 meters squared.
25

26
00:02:31,610 --> 00:02:40,490
Now, next, we can calculate the number of buckets we need by saying let numberOfBuckets to equal the
26

27
00:02:40,490 --> 00:02:45,220
total area we need to cover, divided by areaCoveredperBucket.
27

28
00:02:45,770 --> 00:02:53,660
And now we can return this numberOfBuckets as the value of our property buckets of paint.
28

29
00:02:54,030 --> 00:03:01,790
Now, there's just one problem because the numberOfBuckets is an integer, whereas this has been working
29

30
00:03:01,880 --> 00:03:03,410
with floats.
30

31
00:03:03,530 --> 00:03:10,680
So we need to convert this into an Integer and we can do that by just directly casting it.
31

32
00:03:10,700 --> 00:03:14,260
Now, there's one other problem that you might have spotted.
32

33
00:03:14,360 --> 00:03:20,300
You can see that from our calculations, the number of buckets required is 2.3.
33

34
00:03:20,390 --> 00:03:26,270
But when we turn it into an integer, our float gets rounded down into 2.
34

35
00:03:26,270 --> 00:03:34,010
Now, that doesn't really work because if we need 2.3 buckets to cover our wall with paint, 2 buckets will
35

36
00:03:34,010 --> 00:03:36,480
leave some parts uncovered.
36

37
00:03:36,500 --> 00:03:41,700
So instead of rounding down, in this case, we actually always need to round up.
37

38
00:03:41,720 --> 00:03:47,210
So even if we only need 2.3 buckets, we actually have to go and buy 3 buckets.
38

39
00:03:47,210 --> 00:03:54,410
So what we can do is we can use ceiling, and what ceiling does is it will round up a number either a
39

40
00:03:54,410 --> 00:03:55,800
double or a float.
40

41
00:03:56,000 --> 00:04:02,540
So in this case, between ceilf, we're going to put our numberOfBuckets which at the moment is 2.3
41

42
00:04:02,540 --> 00:04:09,170
and that gets rounded up into 3, and that is what we're going to be turning into a Integer,
42

43
00:04:09,620 --> 00:04:15,610
and that is what we're going to be returning as the value of our buckets of paint property.
43

44
00:04:16,010 --> 00:04:23,960
So, now when we say print buckets of paint, we get three buckets printed down here. And if we decide to
44

45
00:04:23,990 --> 00:04:32,480
paint a different wall, say, something that is at  a 3.4 meters by 2.1, then, automatically,
45

46
00:04:32,780 --> 00:04:37,960
this will get updated and we now need to buy 5 buckets of paint instead.
46

47
00:04:37,970 --> 00:04:45,680
Now, part two of this challenge is what if we had a couple of buckets lying around in the house
47

48
00:04:45,680 --> 00:04:50,660
and I want to know what is the total area that this paint can cover.
48

49
00:04:50,720 --> 00:04:59,180
So can you pause the video and write the setter for bucketsOfPaint property, such that when I said
49

50
00:04:59,240 --> 00:05:04,070
bucketsOfPaint to, say, I don't know, I've got 4 buckets lying around,
50

51
00:05:04,070 --> 00:05:06,040
I want to be up to print down here.
51

52
00:05:06,200 --> 00:05:13,880
This amount of paint can cover an area of X square meters where X is worked out depending on how many
52

53
00:05:13,880 --> 00:05:15,230
buckets of paint we've got.
53

54
00:05:15,440 --> 00:05:23,520
So pause the video now and complete this part of the challenge. Okay, so as we mentioned before, at the moment,
54

55
00:05:23,610 --> 00:05:30,360
our bucketsOfPaint is a read-only property or what they call a get-only property because we haven't
55

56
00:05:30,360 --> 00:05:33,970
specified a setter. So let's add that in there.
56

57
00:05:34,350 --> 00:05:42,780
Now, when this property gets set, as it is down here, on line 21, we want to be able to work out the area
57

58
00:05:42,960 --> 00:05:46,040
that these numberOfBuckets of paint can cover.
58

59
00:05:46,050 --> 00:05:58,290
So let's say, let areaCanCover =  newValue that this property is being set to, 
59

60
00:05:58,350 --> 00:06:04,080
multiplied by 1.5 which, remember, is the area that can be covered per bucketOfPaint.
60

61
00:06:04,380 --> 00:06:06,610
So now we've got another problem.
61

62
00:06:06,690 --> 00:06:13,800
So when we click on this area, it tells us that you cannot multiply an Integer with a Double.
62

63
00:06:14,010 --> 00:06:19,800
So in order to fix this, we're going to have to change this newValue which at the moment, remember, is
63

64
00:06:19,800 --> 00:06:23,360
an Integer to a Double,
64

65
00:06:23,450 --> 00:06:26,580
so we can cast it straight into a Double.
65

66
00:06:26,810 --> 00:06:29,340
And now, we're comparing like with like
66

67
00:06:29,360 --> 00:06:32,000
and we're multiplying Double with a Double.
67

68
00:06:32,000 --> 00:06:39,080
So, we now have an area that's worked out from the newValue that this property gets set to, and we can
68

69
00:06:39,080 --> 00:06:42,660
now use a print statement to output some text.
69

70
00:06:42,680 --> 00:06:51,730
"This amount of paint can cover an area of  \ (areaCanCover)"
70

71
00:06:52,300 --> 00:06:58,630
So, now when I changed this value numberOfBuckets we have lying around, then you can see automatically
71

72
00:06:58,720 --> 00:07:06,310
our setter gets triggered and it tells us the amount or the area that can be covered using the buckets
72

73
00:07:06,310 --> 00:07:07,900
that we have lying around.
73

74
00:07:07,900 --> 00:07:12,580
Now, you can see, even though at the moment, we're using print statements and it's kind of a contrived
74

75
00:07:12,580 --> 00:07:19,360
example, but it's not difficult to see how you can output this into a UIElement, say, a label or the title
75

76
00:07:19,360 --> 00:07:24,780
of a button, and you can use the getter to dynamically work out the value of a property,
76

77
00:07:24,970 --> 00:07:31,480
and this is really, really useful in cutting down on unnecessary functions and methods that you're writing
77

78
00:07:31,510 --> 00:07:32,510
all over the place.
78

79
00:07:32,560 --> 00:07:38,530
And it also makes your code a lot easier to maintain because, in most cases, you don't really want to have
79

80
00:07:38,530 --> 00:07:46,000
to go into the getter and setter. And all you need to do is to set or change certain properties in a
80

81
00:07:46,000 --> 00:07:50,400
couple of places, and the rest will get updated automatically.
