1 00:00:04,689 --> 00:00:07,510 Alright, so moving on let's have a look 2 00:00:07,510 --> 00:00:08,710 at some of the names that we've been 3 00:00:08,710 --> 00:00:11,350 using for our variables, you might be 4 00:00:11,350 --> 00:00:13,660 wondering for example why I use the 5 00:00:13,660 --> 00:00:16,690 capital S on line nine in the variable 6 00:00:16,690 --> 00:00:19,240 Tim salary, well when you create names 7 00:00:19,240 --> 00:00:21,430 for things in column there are a few 8 00:00:21,430 --> 00:00:22,780 rules that you should really stick to 9 00:00:22,780 --> 00:00:24,970 you really must stick to it, firstly 10 00:00:24,970 --> 00:00:27,280 names can contain letters digits and the 11 00:00:27,280 --> 00:00:29,200 underscore character but they can't 12 00:00:29,200 --> 00:00:32,049 start with a digit names are case 13 00:00:32,049 --> 00:00:34,180 sensitive and that means that Tim in 14 00:00:34,180 --> 00:00:36,879 lowercase isn't the same as Tim with an 15 00:00:36,879 --> 00:00:39,190 uppercase T and we can see that if I 16 00:00:39,190 --> 00:00:43,230 change line seven and put a capital T 17 00:00:43,230 --> 00:00:45,699 for the Tim variable you can see we're 18 00:00:45,699 --> 00:00:48,069 now got a red error and if we hover 19 00:00:48,069 --> 00:00:50,289 over that and resolve the reference Tim 20 00:00:50,289 --> 00:00:52,690 and that's because we declared Tim on 21 00:00:52,690 --> 00:00:55,390 line six with a lowercase T so therefore 22 00:00:55,390 --> 00:00:58,870 Kotlin is saying that the variable the 23 00:00:58,870 --> 00:01:00,609 capital T has been declared 24 00:01:00,609 --> 00:01:02,499 and as a result Kotlin doesn't know what 25 00:01:02,499 --> 00:01:05,440 Tim with a capital T actually is now one 26 00:01:05,440 --> 00:01:08,020 of the most common causes of errors when 27 00:01:08,020 --> 00:01:09,430 you're starting to program is using the 28 00:01:09,430 --> 00:01:12,610 wrong case for variable end function 29 00:01:12,610 --> 00:01:14,950 names so watch out for that you must 30 00:01:14,950 --> 00:01:17,800 type the names exactly and that includes 31 00:01:17,800 --> 00:01:19,330 getting the capital and lowercase 32 00:01:19,330 --> 00:01:21,970 letters correct. Alright I'll put that 33 00:01:21,970 --> 00:01:25,780 back to where it was and let's have a 34 00:01:25,780 --> 00:01:28,480 look at other names in our code so 35 00:01:28,480 --> 00:01:30,880 starting at the top on line one we've 36 00:01:30,880 --> 00:01:33,370 got a function main we called the 37 00:01:33,370 --> 00:01:36,010 function main in lower case. Now the 38 00:01:36,010 --> 00:01:37,780 convention in Kotlin is to start 39 00:01:37,780 --> 00:01:39,670 function and variable names with a 40 00:01:39,670 --> 00:01:42,580 lowercase letter it's important to stick 41 00:01:42,580 --> 00:01:44,560 to that convention because otherwise 42 00:01:44,560 --> 00:01:46,480 people will struggle to understand your 43 00:01:46,480 --> 00:01:48,700 code and you'll struggle to understand 44 00:01:48,700 --> 00:01:50,260 it as well when you come back to read it 45 00:01:50,260 --> 00:01:52,540 again months later. Now I know I'm 46 00:01:52,540 --> 00:01:54,490 starting with a capital letter indicates 47 00:01:54,490 --> 00:01:56,620 that it's a name of a class or an 48 00:01:56,620 --> 00:01:58,900 interface. Now we can quickly see the 49 00:01:58,900 --> 00:02:01,000 classes in the program by looking for 50 00:02:01,000 --> 00:02:03,070 the names starting with capitals and 51 00:02:03,070 --> 00:02:04,090 we're going to be creating our own 52 00:02:04,090 --> 00:02:05,530 classes soon so you'll see that 53 00:02:05,530 --> 00:02:08,110 convention being used but variables 54 00:02:08,110 --> 00:02:09,550 should start with a lowercase letter 55 00:02:09,550 --> 00:02:12,760 like Tim and monthly in our example code 56 00:02:12,760 --> 00:02:15,340 here if the variable name is made up of 57 00:02:15,340 --> 00:02:17,560 two or more words strung together 58 00:02:17,560 --> 00:02:19,959 then start each subsequent word with a 59 00:02:19,959 --> 00:02:21,970 capital letter and that makes it easy to 60 00:02:21,970 --> 00:02:24,610 read a bit like using spaces but you 61 00:02:24,610 --> 00:02:26,739 definitely can't use spaces in Kotlin 62 00:02:26,739 --> 00:02:29,410 names so Tim's salary like we've defined 63 00:02:29,410 --> 00:02:32,770 on line 9 as a capital S and that makes 64 00:02:32,770 --> 00:02:34,630 it easy to see that it represents a 65 00:02:34,630 --> 00:02:37,300 salary. Now capitalizing letters in the 66 00:02:37,300 --> 00:02:39,790 name is known as camel case because the 67 00:02:39,790 --> 00:02:41,980 outline of a camel has a hump or two in 68 00:02:41,980 --> 00:02:44,140 the middle and the outline of camel case 69 00:02:44,140 --> 00:02:47,500 names look similar. Now the other thing 70 00:02:47,500 --> 00:02:49,720 you should deal with names is try and 71 00:02:49,720 --> 00:02:51,280 make them reflect the value that they 72 00:02:51,280 --> 00:02:53,590 represent now I haven't really done a 73 00:02:53,590 --> 00:02:55,390 good job of that really because people 74 00:02:55,390 --> 00:02:56,860 tend to think of a salary as being 75 00:02:56,860 --> 00:02:59,440 monthly or yearly and not usually weekly 76 00:02:59,440 --> 00:03:01,030 so I should have been a bit more 77 00:03:01,030 --> 00:03:04,150 specific so what I'm going to do is I'm 78 00:03:04,150 --> 00:03:06,069 gonna rename Tim's salary to Tim's 79 00:03:06,069 --> 00:03:08,890 weekly salary and monthly to Tim's 80 00:03:08,890 --> 00:03:12,280 monthly salary now Android studio will 81 00:03:12,280 --> 00:03:13,930 actually take care of renaming all 82 00:03:13,930 --> 00:03:16,480 occurrences of something if we rename it 83 00:03:16,480 --> 00:03:18,190 by right-clicking and then choosing 84 00:03:18,190 --> 00:03:21,190 refactor rename from the menu so I'm 85 00:03:21,190 --> 00:03:22,630 going to come up here to Tim's salary 86 00:03:22,630 --> 00:03:24,820 first so click on it and then right 87 00:03:24,820 --> 00:03:28,750 click it click on refactor rename and 88 00:03:28,750 --> 00:03:30,100 we're going to change that to Tim's 89 00:03:30,100 --> 00:03:36,579 weekly salary and notice what I've typed 90 00:03:36,579 --> 00:03:38,859 that there are changes to Tim's as well 91 00:03:38,859 --> 00:03:40,630 as 13 weekly salary Tim's weekly salary 92 00:03:40,630 --> 00:03:42,489 it's changing it in the other places 93 00:03:42,489 --> 00:03:44,560 that this particular variables being 94 00:03:44,560 --> 00:03:47,530 used so press enter now that change has 95 00:03:47,530 --> 00:03:49,989 been made and I can do the same to the 96 00:03:49,989 --> 00:03:51,910 monthly variable it's called select it 97 00:03:51,910 --> 00:03:55,269 right click refactor rename we're going 98 00:03:55,269 --> 00:03:56,069 to call this one 99 00:03:56,069 --> 00:04:00,519 Tim's monthly and the capitalized 100 00:04:00,519 --> 00:04:08,079 monthly salary press enter. Now that was 101 00:04:08,079 --> 00:04:09,790 a simple change but it suddenly makes 102 00:04:09,790 --> 00:04:12,639 the code far easier to understand now 103 00:04:12,639 --> 00:04:14,560 it's easy to really see what's going on 104 00:04:14,560 --> 00:04:17,709 on line 10 it's multiplying the weekly 105 00:04:17,709 --> 00:04:21,339 salary by 4 to get a monthly salary now 106 00:04:21,339 --> 00:04:23,110 Collin doesn't actually care what you 107 00:04:23,110 --> 00:04:25,389 call these things it converts them all 108 00:04:25,389 --> 00:04:27,220 to memory addresses before using them 109 00:04:27,220 --> 00:04:29,440 but it really helps us humans to make 110 00:04:29,440 --> 00:04:30,690 sense of the code 111 00:04:30,690 --> 00:04:32,010 I'm going to demonstrate that by 112 00:04:32,010 --> 00:04:34,020 renaming them again I'm going to use a 113 00:04:34,020 --> 00:04:36,030 cool feature of Android studio to rename 114 00:04:36,030 --> 00:04:38,220 all instances again so they don't have 115 00:04:38,220 --> 00:04:40,830 to do them all individually let's just 116 00:04:40,830 --> 00:04:42,030 go ahead and do that so I'm gonna change 117 00:04:42,030 --> 00:04:43,460 teams weekly salary 118 00:04:43,460 --> 00:04:47,460 right-click that refactor rename I'm 119 00:04:47,460 --> 00:04:51,180 gonna change up to X and for the monthly 120 00:04:51,180 --> 00:04:54,900 salary right click refactor rename I'm 121 00:04:54,900 --> 00:04:57,750 going to change that to Y so looking at 122 00:04:57,750 --> 00:04:59,730 that code now it's very hard to work out 123 00:04:59,730 --> 00:05:01,680 what this what this codes really doing 124 00:05:01,680 --> 00:05:03,690 we can see that it's multiplying 125 00:05:03,690 --> 00:05:05,640 something by four to give something else 126 00:05:05,640 --> 00:05:07,530 and if I run it will still get the same 127 00:05:07,530 --> 00:05:12,000 result that we get we got previously 32 128 00:05:12,000 --> 00:05:15,540 128 but it's certainly not easy or less 129 00:05:15,540 --> 00:05:18,060 easy to understand now I'm gonna put 130 00:05:18,060 --> 00:05:19,620 that back to what it was using another 131 00:05:19,620 --> 00:05:22,950 cool Android studio feature and do you 132 00:05:22,950 --> 00:05:24,990 can get that or get access to that from 133 00:05:24,990 --> 00:05:28,080 the edit menu click on undo but I can 134 00:05:28,080 --> 00:05:31,890 also use the commands edge shortcut on a 135 00:05:31,890 --> 00:05:35,100 Mac or ctrl z on a PC, I'm gonna do that 136 00:05:35,100 --> 00:05:37,890 now making sure that I'm in the editor 137 00:05:37,890 --> 00:05:40,380 window so we'll do that once it's put 138 00:05:40,380 --> 00:05:42,210 the first one delete the monthly salary 139 00:05:42,210 --> 00:05:44,010 back to what it was if I press it a 140 00:05:44,010 --> 00:05:46,710 second time we get the second one 141 00:05:46,710 --> 00:05:48,990 renamed back to what it was before so 142 00:05:48,990 --> 00:05:50,430 now it's a lot clearer as to what the 143 00:05:50,430 --> 00:05:52,950 codes doing. Alright so we've covered a 144 00:05:52,950 --> 00:05:54,390 few things there we've looked at 145 00:05:54,390 --> 00:05:56,160 variables and how they have to be 146 00:05:56,160 --> 00:05:57,990 declared before you can use them and 147 00:05:57,990 --> 00:06:00,270 we've looked at how you name variables 148 00:06:00,270 --> 00:06:02,310 until Kotlin what type of information 149 00:06:02,310 --> 00:06:05,220 they can refer to string variables can 150 00:06:05,220 --> 00:06:08,040 store text and it variables can store 151 00:06:08,040 --> 00:06:10,500 whole numbers you've also saw how you 152 00:06:10,500 --> 00:06:12,480 can perform arithmetic or numeric values 153 00:06:12,480 --> 00:06:14,640 we multiplied two numbers together and 154 00:06:14,640 --> 00:06:17,550 printed the results but we can also do 155 00:06:17,550 --> 00:06:20,790 addition subtraction and division let's 156 00:06:20,790 --> 00:06:22,890 have a look at how they work so I'm 157 00:06:22,890 --> 00:06:24,210 going to use print land without giving 158 00:06:24,210 --> 00:06:25,800 anything to print to put a blank line in 159 00:06:25,800 --> 00:06:27,690 the output just to make it clearer to 160 00:06:27,690 --> 00:06:29,250 see what the codes but what codes 161 00:06:29,250 --> 00:06:33,210 producing what and then here they're 162 00:06:33,210 --> 00:06:37,530 gonna type print ln parentheses which 163 00:06:37,530 --> 00:06:38,820 will have a next line and they're going 164 00:06:38,820 --> 00:06:41,280 to start down here on line 16 Bell 165 00:06:41,280 --> 00:06:44,759 apples colon space int equals 166 00:06:44,759 --> 00:06:50,070 the next line vowel or inches : into 167 00:06:50,070 --> 00:06:53,120 equals 5 next line 168 00:06:53,120 --> 00:06:57,860 var not valid this time fruit : int 169 00:06:57,860 --> 00:07:03,539 equals apples + oranges, print ln 170 00:07:03,539 --> 00:07:07,470 fruit, so if you run this to make 171 00:07:07,470 --> 00:07:11,130 sure that it works first you see we've 172 00:07:11,130 --> 00:07:12,300 got a gap there we've actually got the 173 00:07:12,300 --> 00:07:15,570 result of lever there which is 6 plus 5 174 00:07:15,570 --> 00:07:17,250 so we're using the plus operator on line 18 175 00:07:17,250 --> 00:07:21,389 to giving to get rather the value are 176 00:07:21,389 --> 00:07:23,850 of 11. Now we can do subtraction in 177 00:07:23,850 --> 00:07:25,650 the same way by changing the plus two or 178 00:07:25,650 --> 00:07:27,900 more others as you would expect by 179 00:07:27,900 --> 00:07:31,370 changing that to a minors running again 180 00:07:31,370 --> 00:07:33,479 this time we've got the number 1 which 181 00:07:33,479 --> 00:07:35,190 is 6 to take 5 which is what we'd expect 182 00:07:35,190 --> 00:07:38,130 it to be they are taking five oranges 183 00:07:38,130 --> 00:07:39,720 away from six apples is it a very 184 00:07:39,720 --> 00:07:42,090 sensible thing to do perhaps but the 185 00:07:42,090 --> 00:07:43,979 computer quite happily calculates 6 186 00:07:43,979 --> 00:07:47,970 minus 5 and produces the result. 187 00:07:47,970 --> 00:07:50,190 Now division works in the same way but we 188 00:07:50,190 --> 00:07:51,690 can't really look at that until we 189 00:07:51,690 --> 00:07:54,060 looked at another type of number now the 190 00:07:54,060 --> 00:07:56,610 int type can only store whole numbers or 191 00:07:56,610 --> 00:07:59,099 integers so if you try to set the number 192 00:07:59,099 --> 00:08:01,500 of apples to six-and-a-half we'll 193 00:08:01,500 --> 00:08:02,820 actually get an error so it shows up to 194 00:08:02,820 --> 00:08:07,110 6.5 we actually get an error the 195 00:08:07,110 --> 00:08:08,580 floating-point literal doesn't conform 196 00:08:08,580 --> 00:08:11,250 to the expected type int basically 197 00:08:11,250 --> 00:08:13,380 Kotlin saying that the types int and 198 00:08:13,380 --> 00:08:15,659 double are incompatible, well it's 199 00:08:15,659 --> 00:08:16,949 actually saying that error messages I 200 00:08:16,949 --> 00:08:18,690 just wrote read out but that's what it 201 00:08:18,690 --> 00:08:20,430 means you can assign the literal value 202 00:08:20,430 --> 00:08:24,630 6.5 to a variable of type int now if we 203 00:08:24,630 --> 00:08:26,190 want to store numbers with a fractional 204 00:08:26,190 --> 00:08:28,590 part, we have to use a real number type 205 00:08:28,590 --> 00:08:31,229 such as double. Now by the way the 206 00:08:31,229 --> 00:08:33,809 literal a literal is just a term used 207 00:08:33,809 --> 00:08:36,120 for an actual value rather than a 208 00:08:36,120 --> 00:08:39,029 variable so 6.5 here 209 00:08:39,029 --> 00:08:41,279 that's a double literal, these other 210 00:08:41,279 --> 00:08:44,159 valued the other value 5 for example on 211 00:08:44,159 --> 00:08:46,860 line 17 there in turn is an integer 212 00:08:46,860 --> 00:08:50,310 literal and up there on line 6 Tim 213 00:08:50,310 --> 00:08:51,959 equals Tim Buchalka, well that's a 214 00:08:51,959 --> 00:08:54,089 string literal. Alright so we can 215 00:08:54,089 --> 00:08:56,850 perform division on integers but the 216 00:08:56,850 --> 00:08:58,350 results will be an integer 217 00:08:58,350 --> 00:08:59,790 so we'll lose the fractional part 218 00:08:59,790 --> 00:09:03,660 they'll set the int for the value for 219 00:09:03,660 --> 00:09:06,210 apples back to six what I'm going to do 220 00:09:06,210 --> 00:09:08,850 is come down here down here and have 221 00:09:08,850 --> 00:09:12,060 another print 'ln line 21 print ln 222 00:09:12,060 --> 00:09:15,690 parentheses through apples divided by 223 00:09:15,690 --> 00:09:22,290 four if we run that notice that we get 224 00:09:22,290 --> 00:09:25,530 the answer 1 instead of 1.5 that's 225 00:09:25,530 --> 00:09:26,940 because the fractional part of the 226 00:09:26,940 --> 00:09:30,240 answers just throw it away. Now to see 227 00:09:30,240 --> 00:09:32,190 how to work with numbers having a 228 00:09:32,190 --> 00:09:34,290 fractional part we can calculate how 229 00:09:34,290 --> 00:09:35,640 many years are represented by one 230 00:09:35,640 --> 00:09:37,230 hundred and thirty weeks, let's have a go 231 00:09:37,230 --> 00:09:39,330 at doing that so what I'm going to do is 232 00:09:39,330 --> 00:09:41,060 in a space there then start on line 23 233 00:09:41,060 --> 00:09:44,070 and type in print 'ln the parentheses 234 00:09:44,070 --> 00:09:44,760 and nothing else 235 00:09:44,760 --> 00:09:46,500 it give us a bit more gap in the output 236 00:09:46,500 --> 00:09:50,130 they're going to type val space weeks colon 237 00:09:50,130 --> 00:09:56,960 int equals 130 then val space years colon 238 00:09:56,960 --> 00:10:00,080 double with a capital D equals weeks 239 00:10:00,080 --> 00:10:04,470 divided by 52 point 0 then the next line 240 00:10:04,470 --> 00:10:08,250 println years and now if we run 241 00:10:08,250 --> 00:10:13,590 that we correctly get the answer of how 242 00:10:13,590 --> 00:10:16,260 many years are represented by 130 weeks 243 00:10:16,260 --> 00:10:19,620 as 2.5. So if the calculation is going to 244 00:10:19,620 --> 00:10:21,240 return a number with a fractional part 245 00:10:21,240 --> 00:10:23,760 we have to store it in a variable of 246 00:10:23,760 --> 00:10:25,800 type double. Now there are other 247 00:10:25,800 --> 00:10:27,690 fractional number type such as float but 248 00:10:27,690 --> 00:10:30,390 double is the most commonly used now one 249 00:10:30,390 --> 00:10:31,830 thing to watch out for there is that 250 00:10:31,830 --> 00:10:34,320 Kotlin will automatically try to handle 251 00:10:34,320 --> 00:10:36,780 different numeric values numeric types 252 00:10:36,780 --> 00:10:39,120 if you mix them in a calculation so a 253 00:10:39,120 --> 00:10:41,850 years calculation worked fine because we 254 00:10:41,850 --> 00:10:43,800 divided a double value stored in weeks 255 00:10:43,800 --> 00:10:47,400 by the double value 52.0, you can see 256 00:10:47,400 --> 00:10:49,890 that on line 25 but it was important 257 00:10:49,890 --> 00:10:51,720 that we specified the point zero there 258 00:10:51,720 --> 00:10:54,720 if I change that to just 50 to get rid 259 00:10:54,720 --> 00:10:57,620 of the dot zero the fractional component 260 00:10:57,620 --> 00:11:00,060 then we actually get an error type 261 00:11:00,060 --> 00:11:02,240 mismatch required double found int 262 00:11:02,240 --> 00:11:05,010 that's because it needs a double and 263 00:11:05,010 --> 00:11:07,110 we've provided an int. Now it 264 00:11:07,110 --> 00:11:08,820 doesn't matter which of the two numbers 265 00:11:08,820 --> 00:11:10,950 in the calculation a double but 266 00:11:10,950 --> 00:11:13,620 one of them must be otherwise the result 267 00:11:13,620 --> 00:11:15,600 of the calculation is an int and we 268 00:11:15,600 --> 00:11:17,670 can't assign an int to a variable of 269 00:11:17,670 --> 00:11:19,680 type double so what's you out for that 270 00:11:19,680 --> 00:11:21,990 and when you're using numeric values in 271 00:11:21,990 --> 00:11:23,520 a calculation that could return a 272 00:11:23,520 --> 00:11:25,680 fractional part always include the 273 00:11:25,680 --> 00:11:28,230 decimal part even if it's zero so when I 274 00:11:28,230 --> 00:11:31,860 put that back with an undo back to 52.0 275 00:11:31,860 --> 00:11:33,750 if you run the program again just to be 276 00:11:33,750 --> 00:11:36,600 sure and we're still getting the answer 277 00:11:36,600 --> 00:11:38,250 2.5 which is the correct answer. 278 00:11:38,250 --> 00:11:40,230 Alright so I'm gonna stop the video 279 00:11:40,230 --> 00:11:42,120 here in the next video we're going to 280 00:11:42,120 --> 00:11:44,400 see how we can tidy up our output by 281 00:11:44,400 --> 00:11:45,810 adding labels to those bellies that 282 00:11:45,810 --> 00:11:47,940 we're printing out, I'll see you in the 283 00:11:47,940 --> 00:11:50,390 next video.