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