1 00:00:04,920 --> 00:00:07,859 In the previous few videos, we produced 2 00:00:07,859 --> 00:00:10,200 the main part of our app that responded 3 00:00:10,200 --> 00:00:12,389 to the different button clicks. Now the 4 00:00:12,389 --> 00:00:14,430 operations buttons need a method called 5 00:00:14,430 --> 00:00:16,139 performOperation to do the arithmetic, 6 00:00:16,139 --> 00:00:18,840 and we created a dummy method to allow 7 00:00:18,840 --> 00:00:20,430 us to test the program, up to that point. 8 00:00:20,430 --> 00:00:22,470 So it's now time to look at what the 9 00:00:22,470 --> 00:00:23,970 performOperation 10 00:00:23,970 --> 00:00:25,870 will do. 11 00:00:25,870 --> 00:00:28,420 Now the program stores the two numbers 12 00:00:28,420 --> 00:00:30,340 that are involved in the calculation in 13 00:00:30,340 --> 00:00:34,690 variables - operand1 and operand2. now 14 00:00:34,690 --> 00:00:37,000 when the program starts, operand 1 will 15 00:00:37,000 --> 00:00:40,180 be null and operand2 will be zero. Now 16 00:00:40,180 --> 00:00:42,250 the performOperation method is called 17 00:00:42,250 --> 00:00:44,890 with two parameters. The first is a 18 00:00:44,890 --> 00:00:47,080 String holding the value from the Edit 19 00:00:47,080 --> 00:00:47,620 Text - 20 00:00:47,620 --> 00:00:48,879 and we've already checked that that 21 00:00:48,879 --> 00:00:51,550 isn't an empty string. Now the second 22 00:00:51,550 --> 00:00:54,040 parameter is the operation, again as a 23 00:00:54,040 --> 00:00:56,260 String, holding the symbol that 24 00:00:56,260 --> 00:00:59,290 represents the operation. So the first 25 00:00:59,290 --> 00:01:01,000 thing that this method will do is check 26 00:01:01,000 --> 00:01:03,910 operand1. If it's null then there 27 00:01:03,910 --> 00:01:06,280 isn't any calculation to perform, but we 28 00:01:06,280 --> 00:01:07,960 do have to store the value in the 29 00:01:07,960 --> 00:01:10,149 operand. And as well as that, we need to 30 00:01:10,149 --> 00:01:12,880 update the result on the screen. The 31 00:01:12,880 --> 00:01:14,770 input EditText widget is also 32 00:01:14,770 --> 00:01:16,990 cleared, then we're done, and the program 33 00:01:16,990 --> 00:01:18,940 goes back to waiting for a button to be 34 00:01:18,940 --> 00:01:19,470 clicked. 35 00:01:19,470 --> 00:01:23,229 Now if operand1 isn't null, then the 36 00:01:23,229 --> 00:01:26,590 value passed in must be operand2. After 37 00:01:26,590 --> 00:01:28,240 the initial call of this method, operand1 38 00:01:28,240 --> 00:01:30,700 is only ever updated with the result 39 00:01:30,700 --> 00:01:32,830 of a calculation - as long as you count 40 00:01:32,830 --> 00:01:35,110 equals as a calculation, but you'll see 41 00:01:35,110 --> 00:01:37,690 that in a minute. Now the next step is to 42 00:01:37,690 --> 00:01:40,390 decide which operation is pending. There 43 00:01:40,390 --> 00:01:42,039 will always be an operation pending, 44 00:01:42,039 --> 00:01:44,319 because we've initialized pending 45 00:01:44,319 --> 00:01:46,869 Operation to be equal, to be a String - in 46 00:01:46,869 --> 00:01:48,130 other words, the equal sign. 47 00:01:48,130 --> 00:01:50,830 Now if the pending operation is equals, 48 00:01:50,830 --> 00:01:53,140 we set it to the operation that caused 49 00:01:53,140 --> 00:01:55,209 this method to be called - the one passed 50 00:01:55,209 --> 00:01:58,720 in as the second parameter. We then check 51 00:01:58,720 --> 00:02:01,840 to see if that was also equals. If it was 52 00:02:01,840 --> 00:02:03,880 then there's no arithmetic operation to 53 00:02:03,880 --> 00:02:06,280 be performed - instead we just transfer 54 00:02:06,280 --> 00:02:09,098 the value to operand1 and display that 55 00:02:09,098 --> 00:02:12,459 as the result. So the result of an equals 56 00:02:12,459 --> 00:02:13,900 operation, when there's no other 57 00:02:13,900 --> 00:02:16,810 operation pending, is just to update the 58 00:02:16,810 --> 00:02:19,239 result with the new value, and put the 59 00:02:19,239 --> 00:02:22,000 new value into operand1, ready for the 60 00:02:22,000 --> 00:02:24,340 next calculation. So there's no need to 61 00:02:24,340 --> 00:02:26,860 clear the value in operand2, because 62 00:02:26,860 --> 00:02:28,690 that will get a new value the next time 63 00:02:28,690 --> 00:02:31,390 this method's called. Now if the pending 64 00:02:31,390 --> 00:02:34,120 operation wasn't equals, the operation is 65 00:02:34,120 --> 00:02:36,670 performed on operand1 and operand2. 66 00:02:36,670 --> 00:02:39,430 Then the result is set to operand1, 67 00:02:39,430 --> 00:02:42,290 which now holds the answer to the 68 00:02:42,290 --> 00:02:44,810 calculation, and the method ends, clearing 69 00:02:44,810 --> 00:02:47,380 the input field on its way out. 70 00:02:47,380 --> 00:02:49,730 Now it may actually help to see both 71 00:02:49,730 --> 00:02:52,040 flowcharts together, so here you can see 72 00:02:52,040 --> 00:02:54,800 the design of the complete program. Al 73 00:02:54,800 --> 00:02:56,510 right, so it's time now to write the code 74 00:02:56,510 --> 00:02:59,920 for the performOperation function. 75 00:02:59,920 --> 00:03:02,900 Alright, so here we are again in Android 76 00:03:02,900 --> 00:03:05,570 Studio. So what I'm going to do is create 77 00:03:05,570 --> 00:03:07,190 this function - this perform 78 00:03:07,190 --> 00:03:09,800 Operation. So we're going to change this 79 00:03:09,800 --> 00:03:12,860 and we're gonna start at the top. And our 80 00:03:12,860 --> 00:03:16,490 first bit of code; if parenthesis, it's gonna be 81 00:03:16,490 --> 00:03:20,990 operated1 - if that's equal to null. Then we 82 00:03:20,990 --> 00:03:22,880 open a code block, and it's going to be 83 00:03:22,880 --> 00:03:28,760 operand1 is equal to value.toDouble, 84 00:03:28,760 --> 00:03:31,610 and otherwise, you put an else outside of 85 00:03:31,610 --> 00:03:32,960 the code block and open another code 86 00:03:32,960 --> 00:03:37,940 block. Otherwise, operand2 is equal to 87 00:03:37,940 --> 00:03:42,770 value.toDouble. Then we're going to type, 88 00:03:42,770 --> 00:03:45,650 after that, if parentheses pending 89 00:03:45,650 --> 00:03:53,030 Operation is equal to, the equal sign. Then 90 00:03:53,030 --> 00:03:58,160 pendingOperation equals operation - and 91 00:03:58,160 --> 00:03:59,060 actually, what I should have done there is 92 00:03:59,060 --> 00:04:01,459 open a code block, then have the code block 93 00:04:01,459 --> 00:04:04,310 closed there. Next, we're going to type 94 00:04:04,310 --> 00:04:08,420 when parentheses pendingOperation and 95 00:04:08,420 --> 00:04:10,580 open a code block. Then we're going to 96 00:04:10,580 --> 00:04:14,270 type double quote equal, then a dash 97 00:04:14,270 --> 00:04:19,399 greater than sign, operand1 equals 98 00:04:19,399 --> 00:04:22,940 operand2. Then we're going to - that's the 99 00:04:22,940 --> 00:04:25,250 equal sign - then the test for the divide 100 00:04:25,250 --> 00:04:29,500 button. So the / - greater than sign, if, 101 00:04:29,500 --> 00:04:34,970 then parentheses, operand2, if that's 102 00:04:34,970 --> 00:04:41,360 equal equals to 0.0. Then we're going 103 00:04:41,360 --> 00:04:47,780 to type operand1 equals Double.NaN, 104 00:04:47,780 --> 00:04:49,240 then 105 00:04:49,240 --> 00:04:54,039 and here we're going to handle attempt 106 00:04:54,039 --> 00:05:02,080 to divide by zero - to divide by zero. So 107 00:05:02,080 --> 00:05:03,699 we're specifically testing for zero 108 00:05:03,699 --> 00:05:08,229 there. Otherwise, else then operand1, 109 00:05:08,229 --> 00:05:10,750 or open a code block again, equals 110 00:05:10,750 --> 00:05:14,710 operand1 and two exclamation marks 111 00:05:14,710 --> 00:05:20,639 here. Then divided by operand2, 112 00:05:20,639 --> 00:05:22,990 closing code block, as you can see there. 113 00:05:22,990 --> 00:05:27,660 Next we want to handle the, on the next line, the - 114 00:05:27,660 --> 00:05:30,009 and I should really indent this so 115 00:05:30,009 --> 00:05:31,060 it's a little bit more readable, because 116 00:05:31,060 --> 00:05:32,320 it's not really readable the way I've 117 00:05:32,320 --> 00:05:33,389 done it. So I'm gonna do it like that, 118 00:05:33,389 --> 00:05:35,530 which is how we should really have had 119 00:05:35,530 --> 00:05:40,139 it anyway. So we've got our if operand equals 0.0 - 120 00:05:40,139 --> 00:05:42,729 well this is the code that's executed on 121 00:05:42,729 --> 00:05:45,099 that line. Then we've got the else there, 122 00:05:45,099 --> 00:05:47,590 this is all to deal with the, deal with 123 00:05:47,590 --> 00:05:49,060 the division. Then we're going to 124 00:05:49,060 --> 00:05:50,500 come back here, and we're going to handle 125 00:05:50,500 --> 00:05:51,849 the next one which is multiplication - 126 00:05:51,849 --> 00:05:55,659 the asterisk. So we do a dash greater than 127 00:05:55,659 --> 00:06:00,810 sign operand1 equals operand1, 128 00:06:00,810 --> 00:06:05,370 operand1, two exclamation marks, times 129 00:06:05,370 --> 00:06:06,610 operand2. 130 00:06:06,610 --> 00:06:10,270 Okay, next line we want to do handle 131 00:06:10,270 --> 00:06:13,780 the minus, so double quotes minus - 132 00:06:13,780 --> 00:06:15,610 greater than sign. It's going to be 133 00:06:15,610 --> 00:06:20,949 operand1 again, is equal to operand1, two 134 00:06:20,949 --> 00:06:24,400 exclamation marks, take operand2. And 135 00:06:24,400 --> 00:06:28,389 the last one will be + - it's obviously on 136 00:06:28,389 --> 00:06:30,159 the next line - in double quotes, 137 00:06:30,159 --> 00:06:34,719 - greater than sign operand1 equals 138 00:06:34,719 --> 00:06:42,610 operand1, two exclamation marks + operand 139 00:06:42,610 --> 00:06:46,690 2. Alright, we close off that code block 140 00:06:46,690 --> 00:06:51,159 so we delete the rest here - for the when, 141 00:06:51,159 --> 00:06:53,889 that is. And the next one, which closes 142 00:06:53,889 --> 00:06:56,590 off the if for the original test on line 143 00:06:56,590 --> 00:06:59,260 83, and the else for checking whether 144 00:06:59,260 --> 00:07:01,750 operand1 is 0. Then on the last line 145 00:07:01,750 --> 00:07:03,070 down here, we've got, 146 00:07:03,070 --> 00:07:04,420 we're going to delete the display 147 00:07:04,420 --> 00:07:07,270 Operation.text equals, and instead, what 148 00:07:07,270 --> 00:07:08,580 we're going to do is put down there; 149 00:07:08,580 --> 00:07:16,690 result.setText, the operand1.to 150 00:07:16,690 --> 00:07:21,610 String. And then we want to type new 151 00:07:21,610 --> 00:07:27,430 Number.setText double quotes and 152 00:07:27,430 --> 00:07:29,620 two empty, or two double quotes within 153 00:07:29,620 --> 00:07:32,110 the parentheses, I should say. Alright, 154 00:07:32,110 --> 00:07:33,640 so what is this function doing now? 155 00:07:33,640 --> 00:07:36,310 Well, the first thing it does is checks 156 00:07:36,310 --> 00:07:39,130 if operand1 is null, and assigns it to 157 00:07:39,130 --> 00:07:41,260 the numeric value of the value String 158 00:07:41,260 --> 00:07:43,020 that was passed as the first parameter. 159 00:07:43,020 --> 00:07:45,640 Now if that was the case, the rest of the 160 00:07:45,640 --> 00:07:48,400 code is skipped until the result is 161 00:07:48,400 --> 00:07:52,530 displayed, way down here on line 104 - 162 00:07:52,530 --> 00:07:54,940 basically, at the end of the function. 163 00:07:54,940 --> 00:07:56,290 Now the input widget's cleared on the 164 00:07:56,290 --> 00:07:58,930 next line, ready for a new number to be 165 00:07:58,930 --> 00:08:02,320 entered. Now if operand1 wasn't null, the 166 00:08:02,320 --> 00:08:05,170 value gets assigned to operand2, then 167 00:08:05,170 --> 00:08:06,670 the rest of the code - the main part of 168 00:08:06,670 --> 00:08:09,490 the function's code - is executed. Now 169 00:08:09,490 --> 00:08:12,100 if the pending operations is equals - so 170 00:08:12,100 --> 00:08:14,160 we're down to this line here now, line 171 00:08:14,160 --> 00:08:17,260 93 - then the current operation should be 172 00:08:17,260 --> 00:08:19,420 performed. And so therefore, pending 173 00:08:19,420 --> 00:08:22,840 Operation is assigned the new operation. 174 00:08:22,840 --> 00:08:25,180 So in other words, operand1 equals 175 00:08:25,180 --> 00:08:28,660 operand2. Now if you're used to Java, you 176 00:08:28,660 --> 00:08:31,240 may actually well be horrified by the 177 00:08:31,240 --> 00:08:35,799 code, here on a line 88, because in Java, you 178 00:08:35,799 --> 00:08:37,390 should never compare strings using 179 00:08:37,390 --> 00:08:40,720 equals equals. It almost always fails, so 180 00:08:40,720 --> 00:08:42,190 the comparison will be false. 181 00:08:42,190 --> 00:08:44,740 Now that's such a common source of bugs, 182 00:08:44,740 --> 00:08:47,320 that Kotlin has changed the way that 183 00:08:47,320 --> 00:08:49,510 equals equals works, when dealing with 184 00:08:49,510 --> 00:08:51,850 objects. So it now does the comparison 185 00:08:51,850 --> 00:08:54,550 that you'd expect. If you want the Java 186 00:08:54,550 --> 00:08:57,160 behavior in Kotlin, you need to use 3 187 00:08:57,160 --> 00:09:00,400 equal signs, and as it's rare that you do 188 00:09:00,400 --> 00:09:02,500 want that behavior, the extra typing isn't 189 00:09:02,500 --> 00:09:05,290 a problem. Alright, so the code then 190 00:09:05,290 --> 00:09:08,560 uses a when statement, to decide when to 191 00:09:08,560 --> 00:09:11,590 add and subtract, etc. And I 192 00:09:11,590 --> 00:09:13,180 think I pointed down here before, when 193 00:09:13,180 --> 00:09:15,070 were talking about pendingOperation, but 194 00:09:15,070 --> 00:09:16,270 of course, I was referring 195 00:09:16,270 --> 00:09:17,890 to the code that was being executed up 196 00:09:17,890 --> 00:09:19,870 here. But we're now at the stage where we 197 00:09:19,870 --> 00:09:21,280 actually are now talking about the 198 00:09:21,280 --> 00:09:24,070 pendingOperation. Alright, so the code 199 00:09:24,070 --> 00:09:25,750 at that point, is using a when operation 200 00:09:25,750 --> 00:09:27,540 to decide whether to add, subtract etc. 201 00:09:27,540 --> 00:09:30,490 Now if the operation is still equals 202 00:09:30,490 --> 00:09:34,030 after the previous test, operand1 gets 203 00:09:34,030 --> 00:09:36,250 the new value, which has already been 204 00:09:36,250 --> 00:09:38,440 converted to a number and is stored in 205 00:09:38,440 --> 00:09:41,230 operand2. Otherwise, the appropriate 206 00:09:41,230 --> 00:09:43,690 calculation is performed, and the results 207 00:09:43,690 --> 00:09:46,510 stored in operand1. Now there's a check 208 00:09:46,510 --> 00:09:48,970 for division by 0 - you can see that here 209 00:09:48,970 --> 00:09:52,360 on line 94 - and the calculator will 210 00:09:52,360 --> 00:09:54,550 display NAN for Not A Number, 211 00:09:54,550 --> 00:09:57,400 rather than crash. NAN, by the way, is a 212 00:09:57,400 --> 00:09:59,740 special value to cater for cases like 213 00:09:59,740 --> 00:10:01,990 this, where a calculation results in 214 00:10:01,990 --> 00:10:03,970 something that can't be represented as a 215 00:10:03,970 --> 00:10:05,680 number, which you'd get when you try to 216 00:10:05,680 --> 00:10:08,620 divide by 0. Alright, and then finally the 217 00:10:08,620 --> 00:10:11,680 method displays the results in the result 218 00:10:11,680 --> 00:10:13,420 EditText, and clears the input field 219 00:10:13,420 --> 00:10:16,420 before returning. Now one other thing to 220 00:10:16,420 --> 00:10:18,640 look at is, why have we got these two 221 00:10:18,640 --> 00:10:22,450 exclamation marks after the operand1, 222 00:10:22,450 --> 00:10:24,580 whenever we use it in a calculation? 223 00:10:24,580 --> 00:10:26,560 We've actually used that, as you can see, a 224 00:10:26,560 --> 00:10:28,960 total of four times in this method - in 225 00:10:28,960 --> 00:10:31,870 this function - for each of the operations; 226 00:10:31,870 --> 00:10:34,020 the divide, multiply, subtract and add. 227 00:10:34,020 --> 00:10:36,670 Well the exclamation mark is sometimes 228 00:10:36,670 --> 00:10:40,360 called bang, which would make exclamation 229 00:10:40,360 --> 00:10:42,010 mark, exclamation mark, the bang bang 230 00:10:42,010 --> 00:10:44,620 operator. And that's not a bad name for 231 00:10:44,620 --> 00:10:46,420 it, because if you've used it incorrectly, 232 00:10:46,420 --> 00:10:48,940 your code's dead. Now the Kotlin 233 00:10:48,940 --> 00:10:51,580 documentation describes it as being for 234 00:10:51,580 --> 00:10:54,100 Null Pointer Exception lovers, and if you 235 00:10:54,100 --> 00:10:56,350 use it on a null object, you will get a 236 00:10:56,350 --> 00:10:58,810 NullPointerException. So let's actually 237 00:10:58,810 --> 00:11:01,060 have a look at what it does, and why I 238 00:11:01,060 --> 00:11:03,730 considered it safe to use it here. So 239 00:11:03,730 --> 00:11:04,750 we're going to look at the divide 240 00:11:04,750 --> 00:11:07,360 operation, and specifically, this 241 00:11:07,360 --> 00:11:11,620 code here on line 97. So if I remove the 242 00:11:11,620 --> 00:11:13,900 bang bang operator, so delete the two 243 00:11:13,900 --> 00:11:16,450 exclamation marks, you can see that we 244 00:11:16,450 --> 00:11:17,920 actually get an error. And if I just 245 00:11:17,920 --> 00:11:20,500 hover over, Smart cast to Double is 246 00:11:20,500 --> 00:11:22,090 impossible, because operand1 is a 247 00:11:22,090 --> 00:11:24,010 mutable property that could have been 248 00:11:24,010 --> 00:11:26,710 changed by this time. Now the error 249 00:11:26,710 --> 00:11:28,720 previous to that, before the Kotlin was 250 00:11:28,720 --> 00:11:30,110 updated, is that None of the following 251 00:11:30,110 --> 00:11:31,700 functions can be called with the 252 00:11:31,700 --> 00:11:33,980 arguments supplied. So the point here is 253 00:11:33,980 --> 00:11:35,930 that the error message is implying that 254 00:11:35,930 --> 00:11:38,600 one of the operands is of the wrong type, 255 00:11:38,600 --> 00:11:40,750 which can be confusing at first, because 256 00:11:40,750 --> 00:11:42,829 after all, they're both doubles aren't 257 00:11:42,829 --> 00:11:43,250 they? 258 00:11:43,250 --> 00:11:45,980 Well actually, no they're not. operand2 259 00:11:45,980 --> 00:11:48,950 is a double, but operand1 is a Double? - 260 00:11:48,950 --> 00:11:51,110 the nullable version of 261 00:11:51,110 --> 00:11:54,680 Double. And if we come up and just check 262 00:11:54,680 --> 00:11:56,260 the declaration of these two properties, 263 00:11:56,260 --> 00:11:59,029 way up to code there, we've got our Double 264 00:11:59,029 --> 00:12:00,980 on line 16, Double? for 265 00:12:00,980 --> 00:12:04,010 operand1 and just Double, which is set 266 00:12:04,010 --> 00:12:06,640 to a value of 0.0 on line 17. 267 00:12:06,640 --> 00:12:09,380 So Double question mark, which is the 268 00:12:09,380 --> 00:12:12,110 nullable Double class, isn't the same as 269 00:12:12,110 --> 00:12:14,779 the non-nullable Double. Kotlin treats 270 00:12:14,779 --> 00:12:17,089 them as different types, which is how it 271 00:12:17,089 --> 00:12:19,519 prevents us from accidentally assigning 272 00:12:19,519 --> 00:12:21,350 null to an object. Now going back to our 273 00:12:21,350 --> 00:12:25,510 divide operation again, here on line 97, 274 00:12:25,510 --> 00:12:29,029 and I'm going to put back the bang bang 275 00:12:29,029 --> 00:12:30,920 operator - the two exclamation marks - after 276 00:12:30,920 --> 00:12:35,089 operand1, to remove that error. So 277 00:12:35,089 --> 00:12:37,160 what the bang bang operator does, is 278 00:12:37,160 --> 00:12:40,250 return a non-nullable value of operand1. 279 00:12:40,250 --> 00:12:42,860 So that'll only work if operand1 isn't 280 00:12:42,860 --> 00:12:45,380 null. If it is null, we'll get a 281 00:12:45,380 --> 00:12:46,640 NullPointerException. 282 00:12:46,640 --> 00:12:49,040 Okay, so that's what it does, and why we 283 00:12:49,040 --> 00:12:51,440 need to use it. Now the reason, by the way, 284 00:12:51,440 --> 00:12:53,720 that it's safe to use it, is because all 285 00:12:53,720 --> 00:12:56,180 this code is inside a null check. So 286 00:12:56,180 --> 00:12:57,649 right at the start of this function, 287 00:12:57,649 --> 00:12:59,149 right at the very top, we've got a 288 00:12:59,149 --> 00:13:01,790 test here on line 83; if operand1 is 289 00:13:01,790 --> 00:13:04,310 equal to null. So right at the start of 290 00:13:04,310 --> 00:13:06,019 this function, we test to see if operand1 291 00:13:06,019 --> 00:13:09,829 is null, up here on line 83. And we'll 292 00:13:09,829 --> 00:13:11,839 only actually be executing this code if 293 00:13:11,839 --> 00:13:14,209 it isn't, so therefore, it's a safe place 294 00:13:14,209 --> 00:13:15,290 to use bang bang. 295 00:13:15,290 --> 00:13:17,240 Now Kotlin provides the bang bang 296 00:13:17,240 --> 00:13:19,190 operater for when you need it, but 297 00:13:19,190 --> 00:13:20,420 remember that it's your responsibility 298 00:13:20,420 --> 00:13:23,360 to ensure you don't use it on an object 299 00:13:23,360 --> 00:13:26,600 that could possibly be null. Alright, so 300 00:13:26,600 --> 00:13:28,579 the method exactly follows the flowchart 301 00:13:28,579 --> 00:13:29,990 now and should work, so let's actually 302 00:13:29,990 --> 00:13:34,070 run it and see if it does work. We're 303 00:13:34,070 --> 00:13:35,750 going to run the code and switch over to 304 00:13:35,750 --> 00:13:40,209 the emulator. So let's now try some 305 00:13:40,209 --> 00:13:42,740 operations to see whether they work. So 306 00:13:42,740 --> 00:13:43,940 let's try the first one. We'll do 307 00:13:43,940 --> 00:13:50,410 seventy, then we'll do a plus five, and 308 00:13:50,410 --> 00:13:53,260 then we type the, or click on equals. 309 00:13:53,260 --> 00:13:55,850 And you can see we've got 75 at the top, 310 00:13:55,850 --> 00:13:57,830 so that's working. Now we can try 311 00:13:57,830 --> 00:13:59,960 multiplying by ten. So if we type now, at 312 00:13:59,960 --> 00:14:02,030 this point, the multiply, or click on 313 00:14:02,030 --> 00:14:07,550 the multiply button 10, then equals, we 314 00:14:07,550 --> 00:14:09,890 should get the answer - hopefully, 750. You 315 00:14:09,890 --> 00:14:11,740 can see we've got 750 at the top there. 316 00:14:11,740 --> 00:14:15,920 So let's divide now, by 375. So I'm going 317 00:14:15,920 --> 00:14:18,950 to type 375, 3 7 318 00:14:18,950 --> 00:14:23,000 5, then divide. Click on divide, then we get 319 00:14:23,000 --> 00:14:25,280 the answer of 2, which is correct. Now 320 00:14:25,280 --> 00:14:27,560 if I do a divided by 2, here again, so 321 00:14:27,560 --> 00:14:31,300 if I actually click on divide, 2 322 00:14:31,300 --> 00:14:35,780 equals, we get the answer of 1. Now if I 323 00:14:35,780 --> 00:14:39,680 try to divide by zero, by entering the 324 00:14:39,680 --> 00:14:42,370 divide, or clicking on the divide button, zero, 325 00:14:42,370 --> 00:14:46,160 then press equals, or click on equals, we get the 326 00:14:46,160 --> 00:14:47,480 NaN - Not a Number. 327 00:14:47,480 --> 00:14:50,000 Now that would normally crash, but we're 328 00:14:50,000 --> 00:14:52,490 getting the result NaN instead, and 329 00:14:52,490 --> 00:14:55,100 the reason for that - we're handling this 330 00:14:55,100 --> 00:14:58,640 here, is the code on line 94 and 95. So we're 331 00:14:58,640 --> 00:15:00,800 basically sending operands to NaN, 332 00:15:00,800 --> 00:15:03,350 rather than the app crashing, because, of 333 00:15:03,350 --> 00:15:04,970 course, dividing by zero isn't allowed. 334 00:15:04,970 --> 00:15:06,980 Alright, so I don't think we've done a 335 00:15:06,980 --> 00:15:13,840 minus yet, so we'll try typing 65 -, 65. 336 00:15:13,840 --> 00:15:17,150 Well actually, we'll type 65 equals to get rid of the 337 00:15:17,150 --> 00:15:19,640 NaN, so I've done that now. Now we'll try 338 00:15:19,640 --> 00:15:24,710 a subtraction, so we'll put minus 80, then 339 00:15:24,710 --> 00:15:32,120 minus 50 equals minus 65, which is 340 00:15:32,120 --> 00:15:38,110 correct. And then we'll type 150 plus, 150 341 00:15:38,110 --> 00:15:41,600 plus, and we get the answer of 85, which 342 00:15:41,600 --> 00:15:43,910 is correct. So I think things are working 343 00:15:43,910 --> 00:15:46,340 fine. I'll try typing a few decimals now, 344 00:15:46,340 --> 00:15:54,350 so if I try typing 10.25 plus, we get the 345 00:15:54,350 --> 00:15:56,750 answer of 95.25. 346 00:15:56,750 --> 00:15:59,680 And let's just try another one; plus 347 00:15:59,680 --> 00:16:03,980 64,64.987 348 00:16:03,980 --> 00:16:09,230 equals 160.237. 349 00:16:09,230 --> 00:16:11,120 So that seems to be working well. So 350 00:16:11,120 --> 00:16:12,770 we've got our basic calculator working 351 00:16:12,770 --> 00:16:14,690 well, at this point in time. So it does 352 00:16:14,690 --> 00:16:17,090 work fairly well and one nice touch, is 353 00:16:17,090 --> 00:16:18,560 that there's no need for a Clear button 354 00:16:18,560 --> 00:16:21,260 to start in your calculation. So when you 355 00:16:21,260 --> 00:16:23,150 want to start a new calculation, press 356 00:16:23,150 --> 00:16:25,670 equals to finish the current one, then 357 00:16:25,670 --> 00:16:27,620 type the new number and press equals 358 00:16:27,620 --> 00:16:29,900 again. The new number is then transferred 359 00:16:29,900 --> 00:16:32,450 up into the result, ready to add 360 00:16:32,450 --> 00:16:34,010 to it or multiply it, or whatever you 361 00:16:34,010 --> 00:16:36,650 want to do next. However, that's not 362 00:16:36,650 --> 00:16:39,080 necessarily very intuitive, so we're 363 00:16:39,080 --> 00:16:40,010 going to look at that more in the next 364 00:16:40,010 --> 00:16:44,200 video. So I'll see you in the next video.