1 00:00:00,000 --> 00:00:00,530 2 00:00:00,530 --> 00:00:04,200 TOMMY MACWILLIAM: In this video, we're going to write our first Android app. 3 00:00:04,200 --> 00:00:08,550 To get started we're going to be using an app called Android Studio to write 4 00:00:08,550 --> 00:00:10,530 all of our Android code. 5 00:00:10,530 --> 00:00:15,240 Android Studio is an IDE, or an Integrated Development Environment, 6 00:00:15,240 --> 00:00:18,990 that's provided by Google to help you write Android apps. 7 00:00:18,990 --> 00:00:22,470 So let's take a look at Android Studio and create our first app. 8 00:00:22,470 --> 00:00:26,310 So I'm going to open up Android Studio. 9 00:00:26,310 --> 00:00:29,150 And now to get started, I'm going to come over here and select 10 00:00:29,150 --> 00:00:31,820 Start a New Android Studio Project. 11 00:00:31,820 --> 00:00:34,128 Now here I have a bunch of different options here. 12 00:00:34,128 --> 00:00:36,170 Our app is going to be pretty simple, so I'm just 13 00:00:36,170 --> 00:00:40,540 going to select this third one over here, this Empty Activity. 14 00:00:40,540 --> 00:00:42,700 Then I'm going to click Next. 15 00:00:42,700 --> 00:00:45,890 And then it's going to ask me just some information about my project. 16 00:00:45,890 --> 00:00:47,420 So first I'll give it a name. 17 00:00:47,420 --> 00:00:50,003 I'm just going to call this JavaExample. 18 00:00:50,003 --> 00:00:52,420 And then it's going to ask you a few questions about where 19 00:00:52,420 --> 00:00:53,840 to save your project. 20 00:00:53,840 --> 00:00:56,740 I'm just going to put mine on the desktop for now. 21 00:00:56,740 --> 00:01:00,040 For package name you can select whatever you want here. 22 00:01:00,040 --> 00:01:03,670 A common convention when writing Android and Java code 23 00:01:03,670 --> 00:01:07,810 is to take some domain name you own and have the package be that domain name 24 00:01:07,810 --> 00:01:08,740 reversed. 25 00:01:08,740 --> 00:01:15,610 So CS50's home page is cs50.harvard.edu, so I've selected edu.harvard.cs50 26 00:01:15,610 --> 00:01:17,020 as my package name. 27 00:01:17,020 --> 00:01:20,260 And then the end here, this javaexample, is just a lowercase version 28 00:01:20,260 --> 00:01:22,960 of the project name that I selected. 29 00:01:22,960 --> 00:01:26,200 For language we're going to select Java. 30 00:01:26,200 --> 00:01:28,990 And lastly, this minimum API level. 31 00:01:28,990 --> 00:01:32,860 This basically says, what is the lowest version of Android 32 00:01:32,860 --> 00:01:34,980 you want your app to support? 33 00:01:34,980 --> 00:01:37,930 And you can see here that as I change different versions 34 00:01:37,930 --> 00:01:41,080 the IDE will tell me approximately what percent of devices 35 00:01:41,080 --> 00:01:43,420 out there that we're going to support. 36 00:01:43,420 --> 00:01:46,420 So for these videos we're just going to be using Android 5.0, which 37 00:01:46,420 --> 00:01:48,730 is pretty widely supported. 38 00:01:48,730 --> 00:01:51,750 But it's up to you what minimum version you want to support. 39 00:01:51,750 --> 00:01:53,500 And we have a couple other questions here. 40 00:01:53,500 --> 00:01:55,510 One is about supporting instant apps. 41 00:01:55,510 --> 00:01:57,130 We're not going to worry about that. 42 00:01:57,130 --> 00:02:00,200 And this last checkbox is about using AndroidX. 43 00:02:00,200 --> 00:02:03,940 And so this is basically just a newer version of Android libraries. 44 00:02:03,940 --> 00:02:06,790 So we're going to check this, since that's sort of the new version 45 00:02:06,790 --> 00:02:08,380 that we want to use. 46 00:02:08,380 --> 00:02:12,180 After that we'll click Finish. 47 00:02:12,180 --> 00:02:13,050 And here we go. 48 00:02:13,050 --> 00:02:15,320 So now we're inside Android Studio. 49 00:02:15,320 --> 00:02:18,810 And you can see that we actually generated a bunch of files for us. 50 00:02:18,810 --> 00:02:21,260 And so we'll go through those in a bit. 51 00:02:21,260 --> 00:02:27,540 Before we do, we first need to create an AVD, or an Android Virtual Device. 52 00:02:27,540 --> 00:02:31,580 So this will basically allow you to run Android apps on your laptop 53 00:02:31,580 --> 00:02:34,700 rather than needing to plug in an Android device. 54 00:02:34,700 --> 00:02:36,560 So to create an AVD, what we're going to do 55 00:02:36,560 --> 00:02:38,930 is we're going to go through Android Studio, 56 00:02:38,930 --> 00:02:42,890 we're going to specify what type of device we'd like to simulate, 57 00:02:42,890 --> 00:02:45,945 as well as what version of Android is installed, and so on. 58 00:02:45,945 --> 00:02:47,820 And then once we've created that, we're going 59 00:02:47,820 --> 00:02:50,520 to be able to run apps on that device. 60 00:02:50,520 --> 00:02:53,570 So let's go through and create an AVD now. 61 00:02:53,570 --> 00:02:58,280 So to create an AVD inside of Android Studio, I'm going to come up here. 62 00:02:58,280 --> 00:03:02,420 And in the top right, you'll see this little Android logo and a phone. 63 00:03:02,420 --> 00:03:04,850 And it says AVD Manager when I hover over it. 64 00:03:04,850 --> 00:03:08,170 So I'm going to click that. 65 00:03:08,170 --> 00:03:11,820 And you see here that I already have a couple of virtual devices created. 66 00:03:11,820 --> 00:03:15,700 But just to walk you through creating one, down on the bottom right here, 67 00:03:15,700 --> 00:03:18,900 you'll see a button that says Create Virtual Device. 68 00:03:18,900 --> 00:03:20,850 So I'll click that. 69 00:03:20,850 --> 00:03:22,740 And I'll have this pop-up appear. 70 00:03:22,740 --> 00:03:25,790 And here you can see a bunch of different types of phones. 71 00:03:25,790 --> 00:03:30,840 So for example, if I wanted to emulate a Pixel 3a, I can click on that. 72 00:03:30,840 --> 00:03:32,340 Then I'll click Next. 73 00:03:32,340 --> 00:03:35,190 And then here it's going to ask you what version of Android 74 00:03:35,190 --> 00:03:37,300 you want to put on that phone. 75 00:03:37,300 --> 00:03:39,270 So, up to you which version you want to use. 76 00:03:39,270 --> 00:03:40,740 I try to use a recent version. 77 00:03:40,740 --> 00:03:43,170 So I already have Android 9 downloaded. 78 00:03:43,170 --> 00:03:44,970 But if I didn't have Android 8 downloaded, 79 00:03:44,970 --> 00:03:47,820 I could just click on Download here. 80 00:03:47,820 --> 00:03:51,120 After you click Next, you have a few different options here 81 00:03:51,120 --> 00:03:55,950 around just some other sort of graphics options and some advanced stuff. 82 00:03:55,950 --> 00:03:57,700 You don't really have to worry about that. 83 00:03:57,700 --> 00:04:01,400 You can just click Finish to create your AVD. 84 00:04:01,400 --> 00:04:04,430 So before we get started writing any Android code, 85 00:04:04,430 --> 00:04:07,910 let's walk through a little bit of the syntax of the programming language 86 00:04:07,910 --> 00:04:10,650 that we'll be using to write Android apps. 87 00:04:10,650 --> 00:04:14,300 So we're going to be using Java when writing our Android apps. 88 00:04:14,300 --> 00:04:16,910 Google also supports this programming language called Kotlin. 89 00:04:16,910 --> 00:04:18,250 That's pretty similar to Java. 90 00:04:18,250 --> 00:04:22,070 But throughout these videos we'll just be using Java. 91 00:04:22,070 --> 00:04:25,190 So Java is a little similar to C. It has some differences but also 92 00:04:25,190 --> 00:04:27,150 a lot of similarities. 93 00:04:27,150 --> 00:04:31,250 Let's start by first looking at all of these data types available in Java. 94 00:04:31,250 --> 00:04:33,650 Some of these probably look really familiar. 95 00:04:33,650 --> 00:04:37,640 Much like in C, Java has int and char, double, float. 96 00:04:37,640 --> 00:04:40,610 All that stuff you're familiar with is supported in Java. 97 00:04:40,610 --> 00:04:45,890 And Java also has a whole lot of more advanced data types like list and map 98 00:04:45,890 --> 00:04:47,890 that we'll get into in a bit. 99 00:04:47,890 --> 00:04:52,280 But the Java standard library, or the sort of set of objects and APIs 100 00:04:52,280 --> 00:04:56,780 and libraries that's provided to you by using Java, is really, really large. 101 00:04:56,780 --> 00:04:59,030 And so we won't go through everything in these videos, 102 00:04:59,030 --> 00:05:01,730 but we'll try to touch on some classes that you'll really 103 00:05:01,730 --> 00:05:04,830 commonly use while writing apps. 104 00:05:04,830 --> 00:05:07,750 So here's our first few lines of Java code. 105 00:05:07,750 --> 00:05:10,110 And here we're just going to initialize a few variables. 106 00:05:10,110 --> 00:05:11,970 And this should look really familiar. 107 00:05:11,970 --> 00:05:14,790 In this first line here we're creating a new variable. 108 00:05:14,790 --> 00:05:16,570 Its type is a String. 109 00:05:16,570 --> 00:05:19,038 Notice that in Java that's capitalized. 110 00:05:19,038 --> 00:05:21,330 And then we have an equal sign, and the right-hand side 111 00:05:21,330 --> 00:05:23,890 is just the value of that string. 112 00:05:23,890 --> 00:05:27,540 Then the next line, we're creating an int, setting its value equal to 50. 113 00:05:27,540 --> 00:05:31,020 And then we can change that value using the same exact syntax that you 114 00:05:31,020 --> 00:05:35,220 used in C. Also notice that every line ends with a semicolon, something 115 00:05:35,220 --> 00:05:36,990 you're also used to from C. 116 00:05:36,990 --> 00:05:39,580 Conditions look pretty much the same. 117 00:05:39,580 --> 00:05:41,820 We can create another string variable. 118 00:05:41,820 --> 00:05:45,840 In this first line here, notice how I'm not using a double equal sign here. 119 00:05:45,840 --> 00:05:48,150 I'm saying title.equals(). 120 00:05:48,150 --> 00:05:49,900 So we'll get into this a bit later. 121 00:05:49,900 --> 00:05:53,880 But this is something that's called a method on the String object. 122 00:05:53,880 --> 00:05:57,270 So rather than saying equals equals whenever you're comparing strings, 123 00:05:57,270 --> 00:05:59,820 you'll have to remember to say .equals(), 124 00:05:59,820 --> 00:06:03,060 or else you won't actually get the right comparison. 125 00:06:03,060 --> 00:06:07,080 On the third line here this is equivalent to printf in C. 126 00:06:07,080 --> 00:06:09,510 This is how I would print something out to the screen. 127 00:06:09,510 --> 00:06:12,720 I can say System.out.println, and that's going 128 00:06:12,720 --> 00:06:15,120 to print something to standard out. 129 00:06:15,120 --> 00:06:19,790 Else works sort of the same way that you're used to in C. 130 00:06:19,790 --> 00:06:21,080 Next up are arrays. 131 00:06:21,080 --> 00:06:24,730 Again, pretty similar to C. In this first line here 132 00:06:24,730 --> 00:06:28,460 I'm declaring an integer array called values. 133 00:06:28,460 --> 00:06:31,040 And then on the right-hand side, this is where 134 00:06:31,040 --> 00:06:34,410 I'm assigning an initial list of values to that array. 135 00:06:34,410 --> 00:06:37,880 So to do that in Java, I'll say new followed by int-- 136 00:06:37,880 --> 00:06:39,800 that's the type of the array again-- 137 00:06:39,800 --> 00:06:42,320 and then in braces 1, 2, 3. 138 00:06:42,320 --> 00:06:46,340 So this syntax, a little bit different than what you're used to seeing. 139 00:06:46,340 --> 00:06:50,970 And then in the next few lines I'm going to be iterating through that array. 140 00:06:50,970 --> 00:06:55,850 So unlike in C, Java arrays know how large they are. 141 00:06:55,850 --> 00:07:01,350 So I can just say values.length in order to get the length of this array. 142 00:07:01,350 --> 00:07:04,070 So there's sort of no need for me to pass that in separately. 143 00:07:04,070 --> 00:07:06,575 With any array I can always get its length. 144 00:07:06,575 --> 00:07:09,200 And then the rest of this for loop should look pretty familiar. 145 00:07:09,200 --> 00:07:13,070 I'm creating a variable to start that I'm going to use as my counter variable 146 00:07:13,070 --> 00:07:14,810 to step through that array. 147 00:07:14,810 --> 00:07:18,170 I want to keep looping until that counter variable is less 148 00:07:18,170 --> 00:07:21,650 than the length of my array, because remember, arrays are zero-indexed. 149 00:07:21,650 --> 00:07:23,600 They start at 0, not 1. 150 00:07:23,600 --> 00:07:27,470 And then at the end of each loop I just want to increment that counter variable 151 00:07:27,470 --> 00:07:31,250 so its value is one more than it was on the previous iteration. 152 00:07:31,250 --> 00:07:34,430 So hopefully all this looks pretty familiar at this point. 153 00:07:34,430 --> 00:07:37,790 Java also supports something called lists. 154 00:07:37,790 --> 00:07:42,860 And you can think of a list as basically a dynamically sized array. 155 00:07:42,860 --> 00:07:45,980 So with arrays, once you initialize them with some values, 156 00:07:45,980 --> 00:07:48,950 it's really hard to add on one or remove a value. 157 00:07:48,950 --> 00:07:53,580 With lists it's actually really easy to add and remove values. 158 00:07:53,580 --> 00:07:57,590 So in this first line, this is the syntax for creating a list. 159 00:07:57,590 --> 00:07:59,990 So we're first saying my type is List. 160 00:07:59,990 --> 00:08:02,420 And then you'll in angle brackets here, we're 161 00:08:02,420 --> 00:08:07,370 going to specify the type of the element inside of the list. 162 00:08:07,370 --> 00:08:10,010 So this is a list of strings. 163 00:08:10,010 --> 00:08:14,150 So I have List, and then in brackets String, which is just the type. 164 00:08:14,150 --> 00:08:16,640 Then my list is called values. 165 00:08:16,640 --> 00:08:20,880 And then I'm going to be creating a new instance of a list. 166 00:08:20,880 --> 00:08:23,480 So that's why I have this new operator. 167 00:08:23,480 --> 00:08:27,500 And the type of list I want to create is an ArrayList. 168 00:08:27,500 --> 00:08:30,170 Java supports a few different types of lists. 169 00:08:30,170 --> 00:08:34,640 ArrayLists is the simplest, and so we're just going to use that. 170 00:08:34,640 --> 00:08:40,159 Then in these next two lines I'm going to be adding an element to that list. 171 00:08:40,159 --> 00:08:43,789 I can say values.add("one"), values.add("two"). 172 00:08:43,789 --> 00:08:46,880 And after those two lines execute, I now have 173 00:08:46,880 --> 00:08:49,710 a list of strings with two elements. 174 00:08:49,710 --> 00:08:53,940 So you can see I'm dynamically adding element to that list. 175 00:08:53,940 --> 00:08:59,580 Then to iterate over that list this, this is called a for-in operator. 176 00:08:59,580 --> 00:09:02,990 Basically it's just a shorthand for that for i equals zero, 177 00:09:02,990 --> 00:09:06,650 i is less than i++ syntax we just saw. 178 00:09:06,650 --> 00:09:11,960 I can say for String value, followed by a colon, and then the name of the list 179 00:09:11,960 --> 00:09:13,370 I want to iterate over. 180 00:09:13,370 --> 00:09:17,000 So by saying String value colon values, I'm basically saying, 181 00:09:17,000 --> 00:09:21,110 I want to iterate over every element in this values array. 182 00:09:21,110 --> 00:09:24,620 And then what I want to do at that value is just print it out. 183 00:09:24,620 --> 00:09:29,690 So I can say System.out.println, and that's going to print the value. 184 00:09:29,690 --> 00:09:33,650 So just to dive in a little deeper on that syntax, 185 00:09:33,650 --> 00:09:35,930 we saw that using that angle bracket, we're 186 00:09:35,930 --> 00:09:39,870 specifying the type that's contained within the list. 187 00:09:39,870 --> 00:09:41,970 In Java this is called a generic. 188 00:09:41,970 --> 00:09:46,070 And it's basically a way for one class or one type 189 00:09:46,070 --> 00:09:48,500 to understand something about what's inside of it. 190 00:09:48,500 --> 00:09:52,290 So you can basically pass a type to some other type. 191 00:09:52,290 --> 00:09:55,190 So for example, if I wanted to create a list of integers 192 00:09:55,190 --> 00:09:59,420 rather than saying capital String, I would just say capital Integer, 193 00:09:59,420 --> 00:10:03,020 and then I can add integers to that list in the same way. 194 00:10:03,020 --> 00:10:06,440 Java also supports a data type called a map. 195 00:10:06,440 --> 00:10:09,440 In other languages this might be called a dictionary, 196 00:10:09,440 --> 00:10:11,540 or in JavaScript an object. 197 00:10:11,540 --> 00:10:14,120 And the syntax is pretty similar. 198 00:10:14,120 --> 00:10:16,670 You can think about a map as, rather than just 199 00:10:16,670 --> 00:10:22,970 being a list of values, a mapping from some key to some other value. 200 00:10:22,970 --> 00:10:27,140 Much like a dictionary maps words to definitions, our Java map 201 00:10:27,140 --> 00:10:30,230 is going to be kind of the same concept. 202 00:10:30,230 --> 00:10:33,160 So in this first line we're creating a map. 203 00:10:33,160 --> 00:10:35,300 And this time inside of my angle brackets 204 00:10:35,300 --> 00:10:39,130 I'm specifying two things instead of one. 205 00:10:39,130 --> 00:10:43,630 And that's because we need to give both the key and the value. 206 00:10:43,630 --> 00:10:46,600 So by saying map string comma string, I'm saying, 207 00:10:46,600 --> 00:10:51,790 I want to create a map that maps strings to strings. 208 00:10:51,790 --> 00:10:54,720 Then the right-hand side, I'm saying new HashMap. 209 00:10:54,720 --> 00:10:57,250 And again, Java has a few different types of maps. 210 00:10:57,250 --> 00:11:00,890 HashMap is probably the simplest so we're just going to use that. 211 00:11:00,890 --> 00:11:04,150 And then notice inside of those brackets on the right-hand side, 212 00:11:04,150 --> 00:11:05,370 I don't have anything. 213 00:11:05,370 --> 00:11:09,280 If I wanted to, I could technically repeat string comma string there. 214 00:11:09,280 --> 00:11:11,530 But the Java compiler can understand, because I 215 00:11:11,530 --> 00:11:13,563 said string comma string on the left, I don't 216 00:11:13,563 --> 00:11:14,980 need to say it again on the right. 217 00:11:14,980 --> 00:11:16,990 So it's just sort of a shorthand. 218 00:11:16,990 --> 00:11:21,320 These next two lines are how I might insert something into that map. 219 00:11:21,320 --> 00:11:24,130 So I can say airports.put(). 220 00:11:24,130 --> 00:11:26,750 And then I'm creating this mapping between airport codes 221 00:11:26,750 --> 00:11:28,240 and the name of the airport. 222 00:11:28,240 --> 00:11:31,930 So I'm creating a new entry with a key of "SFO" 223 00:11:31,930 --> 00:11:34,480 and a value of "San Francisco." 224 00:11:34,480 --> 00:11:39,190 Similarly I can create a key of "BOS" and a value of "Boston." 225 00:11:39,190 --> 00:11:41,680 Then to iterate through that map, and say 226 00:11:41,680 --> 00:11:44,260 I want to get everything inside of that map 227 00:11:44,260 --> 00:11:49,870 and understand what the keys and values are, I can use that same for-in syntax. 228 00:11:49,870 --> 00:11:51,640 So I'm going to say for. 229 00:11:51,640 --> 00:11:55,600 And then the type of the object I'm going to use to iterate 230 00:11:55,600 --> 00:11:58,340 is going to be a Map.Entry. 231 00:11:58,340 --> 00:12:00,520 So again, this is just saying, I want to get 232 00:12:00,520 --> 00:12:05,660 a pair of key and value-- sort of a key and its corresponding value. 233 00:12:05,660 --> 00:12:08,320 And to do that, I'm going to say airports.entrySet(). 234 00:12:08,320 --> 00:12:11,440 And that's just going to give me a list of those pairs. 235 00:12:11,440 --> 00:12:13,900 So once I have that list it's really easy. 236 00:12:13,900 --> 00:12:17,032 I could just say .getKey() or .getValue(). 237 00:12:17,032 --> 00:12:19,240 And those are going to do exactly what you'd expect-- 238 00:12:19,240 --> 00:12:23,960 either get the key or the value for each entry in that map. 239 00:12:23,960 --> 00:12:28,810 So if I were to run this code, what this would do is this would print out first, 240 00:12:28,810 --> 00:12:32,320 "SFO" followed by a colon followed by "San Francisco." 241 00:12:32,320 --> 00:12:34,930 That would be the first iteration of the for loop. 242 00:12:34,930 --> 00:12:39,940 Then the second iteration of the for loop would be "BOS" colon "Boston." 243 00:12:39,940 --> 00:12:42,180 So basically, just walk through that map, 244 00:12:42,180 --> 00:12:45,610 and we can do whatever we want with each key and value. 245 00:12:45,610 --> 00:12:50,890 So list and map are actually something called classes in Java. 246 00:12:50,890 --> 00:12:55,900 And when we're writing Java, we're going to be writing a lot of our own classes. 247 00:12:55,900 --> 00:12:59,950 You can think about a class as basically a struct that 248 00:12:59,950 --> 00:13:03,100 can also have functions attached to it. 249 00:13:03,100 --> 00:13:05,680 And when we write a function inside of a class, 250 00:13:05,680 --> 00:13:08,450 we're going to call that a method. 251 00:13:08,450 --> 00:13:12,440 So here's the syntax for creating a class in Java. 252 00:13:12,440 --> 00:13:15,560 So we're going to start by saying public class. 253 00:13:15,560 --> 00:13:20,180 This "public" says that other classes inside of your application 254 00:13:20,180 --> 00:13:22,340 can also use this class. 255 00:13:22,340 --> 00:13:25,070 You might also say something like private class 256 00:13:25,070 --> 00:13:27,650 if you wanted to say, this class is private, 257 00:13:27,650 --> 00:13:30,860 I don't want any other classes in my application to use it. 258 00:13:30,860 --> 00:13:36,210 But public is a reasonable default. Then class, then the name of your class. 259 00:13:36,210 --> 00:13:38,990 So here we're creating a class called Person. 260 00:13:38,990 --> 00:13:42,583 Notice that the class is uppercase, just like list and map were. 261 00:13:42,583 --> 00:13:45,500 And that's just sort of a Java convention to start all of your classes 262 00:13:45,500 --> 00:13:47,420 with an upper case letter. 263 00:13:47,420 --> 00:13:49,970 Then we'll use an open brace to start the class. 264 00:13:49,970 --> 00:13:53,000 Now, this class has one field. 265 00:13:53,000 --> 00:13:55,310 And this field is of type String. 266 00:13:55,310 --> 00:13:58,460 And we're calling it name. 267 00:13:58,460 --> 00:14:03,310 Last, we're going to create what's called a constructor for the class. 268 00:14:03,310 --> 00:14:06,950 And a constructor is basically a special method 269 00:14:06,950 --> 00:14:12,170 that's going to be called when you create a new instance of that class. 270 00:14:12,170 --> 00:14:16,450 So here my constructor takes one parameter. 271 00:14:16,450 --> 00:14:20,200 It's a parameter of type String, and we called it name. 272 00:14:20,200 --> 00:14:25,060 And all my body does is take the parameter that's passed in by the user 273 00:14:25,060 --> 00:14:26,620 and save it. 274 00:14:26,620 --> 00:14:29,950 So that way, later on in other methods of the class 275 00:14:29,950 --> 00:14:33,610 it can use whatever the user passed in to start. 276 00:14:33,610 --> 00:14:37,600 So by saying this.name equals name, I'm saying, 277 00:14:37,600 --> 00:14:41,020 I want this name field that I declared on the second line 278 00:14:41,020 --> 00:14:46,960 here, I want the value of that to be equal to whatever the user passed in. 279 00:14:46,960 --> 00:14:50,170 And now to use my class, we can look at this last line 280 00:14:50,170 --> 00:14:52,720 here, where is starts with the type. 281 00:14:52,720 --> 00:14:54,130 My type now is Person. 282 00:14:54,130 --> 00:14:58,220 I basically defined a new type by creating that class. 283 00:14:58,220 --> 00:15:01,120 The name of that class is person now, but with a lowercase letter, 284 00:15:01,120 --> 00:15:03,490 since again, it's just sort of a convention 285 00:15:03,490 --> 00:15:06,040 that objects or instances of a class will 286 00:15:06,040 --> 00:15:07,900 start with lowercase letters and the classes 287 00:15:07,900 --> 00:15:10,030 themselves will start with uppercase. 288 00:15:10,030 --> 00:15:13,300 Then I'm saying I want to create a new, and then Person again. 289 00:15:13,300 --> 00:15:17,380 And by passing in "Tommy," I'm going to invoke this constructor 290 00:15:17,380 --> 00:15:20,420 with the value "Tommy." 291 00:15:20,420 --> 00:15:23,750 So I mentioned that classes can also have methods. 292 00:15:23,750 --> 00:15:25,812 And so here's an example of that. 293 00:15:25,812 --> 00:15:28,520 Let's say that we still have that constructor in that field name, 294 00:15:28,520 --> 00:15:30,450 just as before. 295 00:15:30,450 --> 00:15:34,430 And now I'm going to declare a method on this class. 296 00:15:34,430 --> 00:15:36,110 So we're starting by saying public. 297 00:15:36,110 --> 00:15:38,870 Again, that's so that other classes using 298 00:15:38,870 --> 00:15:43,650 an instance of this class or an object can call this method. 299 00:15:43,650 --> 00:15:46,470 Next is the return type of the method. 300 00:15:46,470 --> 00:15:49,650 It's just void, which means we're not going to return anything. 301 00:15:49,650 --> 00:15:51,600 And then the name of the method. 302 00:15:51,600 --> 00:15:53,570 So here you'll notice this is also camel case, 303 00:15:53,570 --> 00:15:56,600 so starting with a lowercase letter, and then every new word 304 00:15:56,600 --> 00:15:58,550 will start with an uppercase letter. 305 00:15:58,550 --> 00:16:00,980 This method doesn't take any parameters. 306 00:16:00,980 --> 00:16:02,570 And its body is just one line. 307 00:16:02,570 --> 00:16:05,360 We're just, again, going to print something out to the screen. 308 00:16:05,360 --> 00:16:09,450 And we're going to say, "I'm" and then that name variable. 309 00:16:09,450 --> 00:16:13,790 So you'll notice here that we don't need to pass in again that name 310 00:16:13,790 --> 00:16:18,110 variable to this method, because we already passed it to the constructor, 311 00:16:18,110 --> 00:16:20,040 and then we saved it. 312 00:16:20,040 --> 00:16:23,930 So by just saying name here, Java knows that this name actually 313 00:16:23,930 --> 00:16:28,080 refers to that name field inside of your class. 314 00:16:28,080 --> 00:16:32,540 So now, to use this method I can create that person just like before-- 315 00:16:32,540 --> 00:16:35,060 say new Person, pass in some value. 316 00:16:35,060 --> 00:16:38,030 And then I can say person.sayHello(). 317 00:16:38,030 --> 00:16:40,850 And that's going to call this method. 318 00:16:40,850 --> 00:16:44,030 So this is just like before when we were looking at lists and maps. 319 00:16:44,030 --> 00:16:50,420 When you said airports.put(), put() is just a method on some map class 320 00:16:50,420 --> 00:16:51,335 somewhere. 321 00:16:51,335 --> 00:16:53,210 And we didn't write that map class ourselves. 322 00:16:53,210 --> 00:16:55,580 Someone else wrote that and provided it to us. 323 00:16:55,580 --> 00:16:59,270 But under the hood that's basically what's happening here. 324 00:16:59,270 --> 00:17:03,060 We could also have what are called static methods. 325 00:17:03,060 --> 00:17:05,780 And these are methods that you can call on a class 326 00:17:05,780 --> 00:17:09,000 without having an instance of it. 327 00:17:09,000 --> 00:17:11,329 So instead of saying public void wave, I've 328 00:17:11,329 --> 00:17:14,630 just said public static void wave here. 329 00:17:14,630 --> 00:17:16,369 We can define the method in the same way. 330 00:17:16,369 --> 00:17:19,099 The body of the method is just like any other method. 331 00:17:19,099 --> 00:17:23,780 But then to call this method, on this last line here you can see I'm saying, 332 00:17:23,780 --> 00:17:27,010 capital Person-- so that refers to the class-- 333 00:17:27,010 --> 00:17:28,440 .wave(). 334 00:17:28,440 --> 00:17:30,770 So here we didn't have an instance of that class. 335 00:17:30,770 --> 00:17:32,060 We're not using an object. 336 00:17:32,060 --> 00:17:35,348 We're just referencing the class itself, and we're calling wave. 337 00:17:35,348 --> 00:17:37,640 And this can be helpful if there's any methods that you 338 00:17:37,640 --> 00:17:41,300 have in a class where it doesn't really make sense to instantiate a class, 339 00:17:41,300 --> 00:17:44,550 or it's just sort of unnecessary to have an object around. 340 00:17:44,550 --> 00:17:48,200 And that's where a static method might come in handy. 341 00:17:48,200 --> 00:17:52,730 One of the more powerful features of classes is inheritance. 342 00:17:52,730 --> 00:17:55,960 And inheritance allows you to say, I have 343 00:17:55,960 --> 00:17:58,510 some base class or some parent class that 344 00:17:58,510 --> 00:18:01,270 defined some fields and some methods. 345 00:18:01,270 --> 00:18:05,020 And I want to take that class and modify it a little bit. 346 00:18:05,020 --> 00:18:07,900 So maybe I have some other class I need to write 347 00:18:07,900 --> 00:18:10,660 that's really similar to that first one, but I 348 00:18:10,660 --> 00:18:14,260 want to change some of the behaviors in this new version 349 00:18:14,260 --> 00:18:16,480 and keep some of the other ones. 350 00:18:16,480 --> 00:18:18,760 So let's walk through an example. 351 00:18:18,760 --> 00:18:21,630 Here we have a class called Vehicle. 352 00:18:21,630 --> 00:18:25,150 And this Vehicle class has two methods. 353 00:18:25,150 --> 00:18:28,450 This first method you can see it returns an integer. 354 00:18:28,450 --> 00:18:31,000 I've just called it wheels(), and it returns 4. 355 00:18:31,000 --> 00:18:35,230 And this says, OK, for every vehicle, it has four wheels. 356 00:18:35,230 --> 00:18:37,390 Then I have another method called go(). 357 00:18:37,390 --> 00:18:42,050 That's just going to print something out to the screen and not return anything. 358 00:18:42,050 --> 00:18:43,070 So nothing new here. 359 00:18:43,070 --> 00:18:46,420 This is just a regular class, and we can instantiate that class 360 00:18:46,420 --> 00:18:51,880 or create an object in the same way, and call these methods in the same way. 361 00:18:51,880 --> 00:18:56,520 But let's say now that I'm writing a class to represent a motorcycle. 362 00:18:56,520 --> 00:19:00,010 And a motorcycle is basically a subclass of a vehicle. 363 00:19:00,010 --> 00:19:03,730 You can think of a motorcycle as a type of vehicle, 364 00:19:03,730 --> 00:19:06,400 meaning it shares many of the same properties, 365 00:19:06,400 --> 00:19:09,670 but some things are a little different. 366 00:19:09,670 --> 00:19:14,390 So to capture that, I'm going to start declaring my class in the same way. 367 00:19:14,390 --> 00:19:16,810 I'm going to say public class Motorcycle. 368 00:19:16,810 --> 00:19:20,390 And then I'm going to say extends Vehicle. 369 00:19:20,390 --> 00:19:23,530 So this in Java is how you're saying this class 370 00:19:23,530 --> 00:19:28,750 inherits from this other parent class or superclass. 371 00:19:28,750 --> 00:19:32,380 And after I say extends Vehicle, now my Motorcycle class 372 00:19:32,380 --> 00:19:36,670 has access to everything that was in that Vehicle class. 373 00:19:36,670 --> 00:19:40,750 So I could say Motorcycle.wheels() and get back 4, Motorcycle.go(), 374 00:19:40,750 --> 00:19:45,650 and it would print out something to the screen, just as I could with Vehicle. 375 00:19:45,650 --> 00:19:49,940 But now what I want to do is override that wheels function. 376 00:19:49,940 --> 00:19:54,380 Because motorcycles have two wheels and not four. 377 00:19:54,380 --> 00:19:58,980 And so to do that, I'm just going to basically re-declare this wheels() 378 00:19:58,980 --> 00:19:59,480 method. 379 00:19:59,480 --> 00:20:01,880 I'm going to say public int wheels(). 380 00:20:01,880 --> 00:20:06,750 And then rather than returning 4, I'm going to return 2. 381 00:20:06,750 --> 00:20:09,080 And you'll notice on this second line I've also 382 00:20:09,080 --> 00:20:10,940 added what's called an annotation. 383 00:20:10,940 --> 00:20:12,620 It says @Override. 384 00:20:12,620 --> 00:20:15,830 This is basically just a signal to the compiler 385 00:20:15,830 --> 00:20:20,680 telling the compiler that I just overrided this method. 386 00:20:20,680 --> 00:20:22,940 If I forget the override, that's OK. 387 00:20:22,940 --> 00:20:26,057 But it's really helpful, because if I had this @Override, 388 00:20:26,057 --> 00:20:27,890 and I tried to override a method that didn't 389 00:20:27,890 --> 00:20:29,990 exist, the compiler would tell me, hey, you're 390 00:20:29,990 --> 00:20:31,920 not actually overriding anything. 391 00:20:31,920 --> 00:20:33,358 You probably messed up somewhere. 392 00:20:33,358 --> 00:20:35,150 So whenever you're overriding a method it's 393 00:20:35,150 --> 00:20:38,960 really helpful just to add this @Override before you do that. 394 00:20:38,960 --> 00:20:41,630 So that's extending classes. 395 00:20:41,630 --> 00:20:46,750 And in Java you can also have something called an interface. 396 00:20:46,750 --> 00:20:51,610 And you can think of an interface as basically a list of methods 397 00:20:51,610 --> 00:20:54,440 that some class has to implement. 398 00:20:54,440 --> 00:20:56,810 And if they don't implement all of the methods 399 00:20:56,810 --> 00:20:59,970 in some interface, the compiler will tell you, hey, 400 00:20:59,970 --> 00:21:01,370 you have an invalid class here. 401 00:21:01,370 --> 00:21:03,500 You're not implementing one of these methods 402 00:21:03,500 --> 00:21:06,530 that you told me that you were going to implement. 403 00:21:06,530 --> 00:21:08,190 So here's an example. 404 00:21:08,190 --> 00:21:10,920 So we'll start our interface with public interface. 405 00:21:10,920 --> 00:21:13,880 And we're going to name this Teacher. 406 00:21:13,880 --> 00:21:15,920 And now, inside of this interface we just 407 00:21:15,920 --> 00:21:18,870 have one method which has a void type. 408 00:21:18,870 --> 00:21:20,030 It's called teach(). 409 00:21:20,030 --> 00:21:23,480 But what's interesting about this is there's no method body here. 410 00:21:23,480 --> 00:21:27,500 We haven't specified at all what it means to teach. 411 00:21:27,500 --> 00:21:32,268 We've just said, in order to be a teacher, you have to know how to teach. 412 00:21:32,268 --> 00:21:34,310 It's basically the same as signing some contract, 413 00:21:34,310 --> 00:21:36,350 saying, if you're going to use this interface, 414 00:21:36,350 --> 00:21:41,000 you promise that you're going to implement this method for me. 415 00:21:41,000 --> 00:21:44,180 So now, to use this interface it's pretty similar. 416 00:21:44,180 --> 00:21:49,540 We can say public class CS50Teacher implements Teacher. 417 00:21:49,540 --> 00:21:51,320 And this says, there's some interface. 418 00:21:51,320 --> 00:21:55,370 I'm implementing every method inside of that interface. 419 00:21:55,370 --> 00:21:59,150 And a class can implement as many interfaces as it wants. 420 00:21:59,150 --> 00:22:03,000 I could have said Teacher comma and then some other interface. 421 00:22:03,000 --> 00:22:06,630 But we can only ever extend one class. 422 00:22:06,630 --> 00:22:09,470 So in Java you'll see really commonly lots of interfaces, 423 00:22:09,470 --> 00:22:12,410 and classes can conform or implement a bunch. 424 00:22:12,410 --> 00:22:14,780 But when you have a base class or superclass, 425 00:22:14,780 --> 00:22:18,330 you can only have one per class. 426 00:22:18,330 --> 00:22:22,780 Now, to implement this interface, same as before. 427 00:22:22,780 --> 00:22:25,470 We're just going to declare a method in our class 428 00:22:25,470 --> 00:22:29,383 with the same name and the same type as that interface. 429 00:22:29,383 --> 00:22:32,550 So I'm going to say, @Override, just to make sure the compiler would tell me 430 00:22:32,550 --> 00:22:35,828 if I'm overriding it incorrectly, public void teach(). 431 00:22:35,828 --> 00:22:38,620 And then this method is probably a little complicated to implement, 432 00:22:38,620 --> 00:22:40,980 so we're not going to worry about that right now. 433 00:22:40,980 --> 00:22:45,100 So now let's return to that line we had before, 434 00:22:45,100 --> 00:22:47,940 where we created a new list of strings. 435 00:22:47,940 --> 00:22:51,260 And hopefully this is going to make a little more sense now. 436 00:22:51,260 --> 00:22:55,220 On the left List is actually an interface. 437 00:22:55,220 --> 00:22:58,220 So we want to say that the type is something 438 00:22:58,220 --> 00:23:03,110 that conforms to the List interface or implements everything 439 00:23:03,110 --> 00:23:05,780 inside of the List interface. 440 00:23:05,780 --> 00:23:08,030 These brackets, remember, are a generic. 441 00:23:08,030 --> 00:23:12,900 It's a way of telling the List interface or this instance of a class, 442 00:23:12,900 --> 00:23:15,538 I want you to know that you're going to contain strings. 443 00:23:15,538 --> 00:23:16,455 And String is a class. 444 00:23:16,455 --> 00:23:20,330 And so we're passing on that class to this other one. 445 00:23:20,330 --> 00:23:23,670 And then the right-hand side, this new ArrayList, 446 00:23:23,670 --> 00:23:28,650 this is actually a class somewhere that implements List. 447 00:23:28,650 --> 00:23:31,790 So that's why the left and the right-hand side are different. 448 00:23:31,790 --> 00:23:36,050 The left-hand side is basically a more general thing saying, 449 00:23:36,050 --> 00:23:38,480 some type of list-- it could be anything. 450 00:23:38,480 --> 00:23:40,730 But as long as it implements a list, we're 451 00:23:40,730 --> 00:23:43,400 OK, because we're only going to be using methods 452 00:23:43,400 --> 00:23:45,980 that are defined in the List interface. 453 00:23:45,980 --> 00:23:50,450 And then the right-hand side, ArrayList is a concrete class 454 00:23:50,450 --> 00:23:54,200 that implements the List interface. 455 00:23:54,200 --> 00:23:58,130 If I wanted to, I could have said ArrayList strings equals new ArrayList. 456 00:23:58,130 --> 00:24:01,550 But by convention you want to use the type that's sort of the most general 457 00:24:01,550 --> 00:24:05,150 and makes the most sense, because you might not want to use an ArrayList. 458 00:24:05,150 --> 00:24:07,280 It might be something else under the hood. 459 00:24:07,280 --> 00:24:11,210 And so you're kind of abstracting away or hiding that detail under the hood 460 00:24:11,210 --> 00:24:14,820 by just saying, I'm only using these methods in a list. 461 00:24:14,820 --> 00:24:17,900 As long as this class has those, it doesn't really matter to me 462 00:24:17,900 --> 00:24:20,820 under the hood what type of list it is. 463 00:24:20,820 --> 00:24:23,800 Java also has this notion of packages. 464 00:24:23,800 --> 00:24:26,380 And that's something we're going to see pretty quickly. 465 00:24:26,380 --> 00:24:31,110 But this basically provides you a way to namespace your class. 466 00:24:31,110 --> 00:24:33,490 So often in a Java application you're going 467 00:24:33,490 --> 00:24:35,970 to have a bunch of different classes spread apart 468 00:24:35,970 --> 00:24:38,220 a bunch of different files. 469 00:24:38,220 --> 00:24:42,510 And packages basically provide you a way of organizing those. 470 00:24:42,510 --> 00:24:46,230 So you might want to do something like, if your app has two screens, put 471 00:24:46,230 --> 00:24:49,590 all of the classes used in one screen inside of one package, 472 00:24:49,590 --> 00:24:53,160 use all the classes contained in another screen inside of another package. 473 00:24:53,160 --> 00:24:55,770 And that way you can sort of more cleanly organize your code. 474 00:24:55,770 --> 00:25:01,140 Sort of equivalent to using folders to organize your files in your computer, 475 00:25:01,140 --> 00:25:03,150 we can more logically organize things instead 476 00:25:03,150 --> 00:25:08,650 of just having a list of 100 classes all sort of at the same level. 477 00:25:08,650 --> 00:25:09,960 So that's package. 478 00:25:09,960 --> 00:25:14,390 And then to use a package, we're going to use this import statement. 479 00:25:14,390 --> 00:25:19,380 So to actually use those List and Map classes in a real application, 480 00:25:19,380 --> 00:25:21,630 I'd first have to import them. 481 00:25:21,630 --> 00:25:25,970 And that's just telling the compiler, I need to use a List class somewhere, 482 00:25:25,970 --> 00:25:30,750 and here is where you can find the definition of that List class. 483 00:25:30,750 --> 00:25:33,948 So List, for example, is located in the package java, 484 00:25:33,948 --> 00:25:36,990 which means it's the Java standard library-- we didn't write any of this, 485 00:25:36,990 --> 00:25:39,620 it's just given to us because we're using Java-- 486 00:25:39,620 --> 00:25:43,260 .util-- which means there's a package somewhere called util, 487 00:25:43,260 --> 00:25:45,210 which is short for utility-- 488 00:25:45,210 --> 00:25:47,580 and then the class name, and it's .List. 489 00:25:47,580 --> 00:25:50,770 That's the name of the class that we want to use. 490 00:25:50,770 --> 00:25:55,770 So once in my file I've added this import java.util.List, 491 00:25:55,770 --> 00:25:59,610 that's going to mean I can use this List class-- or this List interface, 492 00:25:59,610 --> 00:26:03,053 actually-- wherever I need to inside of my code. 493 00:26:03,053 --> 00:26:04,470 So that's a whole bunch of syntax. 494 00:26:04,470 --> 00:26:06,750 But we've basically covered everything we're 495 00:26:06,750 --> 00:26:09,490 going to need to start writing some Android apps. 496 00:26:09,490 --> 00:26:12,430 So now let's take a look at an example. 497 00:26:12,430 --> 00:26:18,900 Let's return to Android Studio and start writing some Java code of our own. 498 00:26:18,900 --> 00:26:24,990 Over here on the left is all of the files contained inside of my project. 499 00:26:24,990 --> 00:26:26,490 And we'll go through these in a bit. 500 00:26:26,490 --> 00:26:28,230 You can see there's a few folders. 501 00:26:28,230 --> 00:26:31,400 The folder we care about right now is a folder called java. 502 00:26:31,400 --> 00:26:35,320 And that's where all of our Java code is going to live. 503 00:26:35,320 --> 00:26:39,310 Then here you can see the package that we created. 504 00:26:39,310 --> 00:26:43,620 So remember, we called that edu.harvard.cs50.javaexample. 505 00:26:43,620 --> 00:26:45,420 That's our package name. 506 00:26:45,420 --> 00:26:48,960 And you can see here that the file that was generated for us 507 00:26:48,960 --> 00:26:51,030 happens to start with that package. 508 00:26:51,030 --> 00:26:53,730 So hopefully that makes sense now. 509 00:26:53,730 --> 00:26:57,735 And then the class that we've created is called MainActivity. 510 00:26:57,735 --> 00:27:04,210 It looks like it extends some other base class called AppCompatActivity. 511 00:27:04,210 --> 00:27:07,660 If I come over here to this import line and click the plus, 512 00:27:07,660 --> 00:27:09,820 we can see that, OK, it looks like we've already 513 00:27:09,820 --> 00:27:14,860 imported this class from somewhere, in particular that AndroidX package which, 514 00:27:14,860 --> 00:27:17,830 remember, is a bunch of support libraries 515 00:27:17,830 --> 00:27:20,672 that we can use to build our application. 516 00:27:20,672 --> 00:27:22,630 And then it looks like we have some method here 517 00:27:22,630 --> 00:27:24,580 that's already defined for us. 518 00:27:24,580 --> 00:27:27,160 It's overriding some method in the base class. 519 00:27:27,160 --> 00:27:29,260 It has that @Override. 520 00:27:29,260 --> 00:27:31,800 The visibility is protected. 521 00:27:31,800 --> 00:27:35,360 And this means something basically between public and private. 522 00:27:35,360 --> 00:27:40,690 This means that if I subclass this class that those subclasses can access this 523 00:27:40,690 --> 00:27:43,950 method, but other classes can't. 524 00:27:43,950 --> 00:27:46,060 Its return type is void. 525 00:27:46,060 --> 00:27:49,060 The method name is onCreate(). 526 00:27:49,060 --> 00:27:51,200 And it takes one parameter. 527 00:27:51,200 --> 00:27:54,820 The parameter has a type Bundle which, from the looks of it, 528 00:27:54,820 --> 00:27:57,940 is something that's also provided to us from Android, since it's 529 00:27:57,940 --> 00:27:59,900 being imported up here. 530 00:27:59,900 --> 00:28:03,530 And the name of this parameter is savedInstanceState. 531 00:28:03,530 --> 00:28:04,030 OK. 532 00:28:04,030 --> 00:28:09,250 So in this next line we're going to be calling the method onCreate() 533 00:28:09,250 --> 00:28:12,440 that's defined in AppCompatActivity. 534 00:28:12,440 --> 00:28:16,690 And this is a really common pattern when you subclass something. 535 00:28:16,690 --> 00:28:19,270 Oftentimes when you're overriding a method, 536 00:28:19,270 --> 00:28:21,430 you don't want to override it completely, 537 00:28:21,430 --> 00:28:24,250 because maybe that method was doing something important. 538 00:28:24,250 --> 00:28:26,980 In this case the method in AppCompatActivity probably 539 00:28:26,980 --> 00:28:28,640 is doing something important. 540 00:28:28,640 --> 00:28:31,480 So you also want to use that definition. 541 00:28:31,480 --> 00:28:34,990 So by saying super.onCreate(), I'm saying, 542 00:28:34,990 --> 00:28:38,580 I want the method that's defined in AppCompatActivity, 543 00:28:38,580 --> 00:28:43,540 and I want to call that before I override it and do something of my own. 544 00:28:43,540 --> 00:28:46,720 So by doing that we're making sure that, not only are we provided by our own 545 00:28:46,720 --> 00:28:50,020 definition for onCreate(), we're also taking advantage of whatever 546 00:28:50,020 --> 00:28:53,000 AppCompatActivity has defined. 547 00:28:53,000 --> 00:28:57,077 So we're passing it that one argument, just savedInstanceState. 548 00:28:57,077 --> 00:28:59,660 And lastly, it looks like we're just calling some other method 549 00:28:59,660 --> 00:29:03,022 that's defined on AppCompatActivity called setContentView(). 550 00:29:03,022 --> 00:29:04,730 And let's not worry about this right now. 551 00:29:04,730 --> 00:29:07,260 We'll get into that a bit later. 552 00:29:07,260 --> 00:29:10,488 But here is where we can start writing some Java code. 553 00:29:10,488 --> 00:29:12,530 And the first Java code that we're going to write 554 00:29:12,530 --> 00:29:15,680 is it really simple sorting hat application. 555 00:29:15,680 --> 00:29:18,290 So in CS50 we have a few different tracks here. 556 00:29:18,290 --> 00:29:20,862 We have one for games, one for web, and one for mobile. 557 00:29:20,862 --> 00:29:22,820 And so we're going to write a really simple app 558 00:29:22,820 --> 00:29:27,350 that just randomly sorts students into one of those tracks. 559 00:29:27,350 --> 00:29:28,710 So let's get started. 560 00:29:28,710 --> 00:29:34,130 The first thing we want to do is create a class to represent a track. 561 00:29:34,130 --> 00:29:35,930 So let's do that. 562 00:29:35,930 --> 00:29:38,120 Let's come back over here to the left. 563 00:29:38,120 --> 00:29:42,203 And I'm going to Control-Click on this package name. 564 00:29:42,203 --> 00:29:43,370 And I'll see this long menu. 565 00:29:43,370 --> 00:29:48,320 But what I care about is creating a new Java Class. 566 00:29:48,320 --> 00:29:50,180 So let's click that. 567 00:29:50,180 --> 00:29:51,630 And they have this nice menu here. 568 00:29:51,630 --> 00:29:53,047 It's going to ask me a few things. 569 00:29:53,047 --> 00:29:54,740 First is the name of the class. 570 00:29:54,740 --> 00:29:57,940 I'm going to call it Track, because I'm creating a new track. 571 00:29:57,940 --> 00:30:00,000 It's just going to be a regular class. 572 00:30:00,000 --> 00:30:04,160 It's not extending anything, it's not implementing any interfaces. 573 00:30:04,160 --> 00:30:07,448 You can say its default visibility is public. 574 00:30:07,448 --> 00:30:08,490 Don't have any modifiers. 575 00:30:08,490 --> 00:30:10,820 We're not worried about that right now. 576 00:30:10,820 --> 00:30:14,100 And the package we want to add that to is just the same package. 577 00:30:14,100 --> 00:30:16,260 No need to create anything else here. 578 00:30:16,260 --> 00:30:19,430 So let's click OK. 579 00:30:19,430 --> 00:30:20,190 And there we go. 580 00:30:20,190 --> 00:30:24,840 So Android Studio has nicely generated this class for me. 581 00:30:24,840 --> 00:30:27,080 And if I come back over here to the left, 582 00:30:27,080 --> 00:30:30,560 we can see that it's inside of that same package as this other class 583 00:30:30,560 --> 00:30:33,710 called MainActivity. 584 00:30:33,710 --> 00:30:34,810 So let's see. 585 00:30:34,810 --> 00:30:38,620 We want each class to have a couple of fields. 586 00:30:38,620 --> 00:30:44,090 Let's call one of the fields a public String name. 587 00:30:44,090 --> 00:30:46,560 And this will represent the name of a track. 588 00:30:46,560 --> 00:30:50,810 And let's call the other field the instructor. 589 00:30:50,810 --> 00:30:54,050 And that's going to be the instructor for the track. 590 00:30:54,050 --> 00:30:59,330 So after creating these two fields, let's now create a constructor. 591 00:30:59,330 --> 00:31:03,520 So remember, the constructor is just going to be the same name as the class. 592 00:31:03,520 --> 00:31:06,462 And you'll notice Android Studio has this really nice autocomplete. 593 00:31:06,462 --> 00:31:09,670 That's something you should definitely take advantage of while you're coding. 594 00:31:09,670 --> 00:31:14,350 So as I started typing "Track," Android Studio recognized, 595 00:31:14,350 --> 00:31:16,270 hey, we've got a class called Track. 596 00:31:16,270 --> 00:31:17,780 Maybe you meant to type that. 597 00:31:17,780 --> 00:31:22,000 So if I just hit Enter, it's going to autocomplete for me. 598 00:31:22,000 --> 00:31:24,910 So now let's add those two parameters-- one called 599 00:31:24,910 --> 00:31:29,230 name, one called instructor-- again, autocomplete being helpful here. 600 00:31:29,230 --> 00:31:31,400 And all we want to do is save those two things. 601 00:31:31,400 --> 00:31:36,547 So we can say, this.name is name, and this.instructor is instructor. 602 00:31:36,547 --> 00:31:39,130 Using autocomplete you can look like you're typing really fast 603 00:31:39,130 --> 00:31:41,720 and impress your friends. 604 00:31:41,720 --> 00:31:42,920 So that's it for this class. 605 00:31:42,920 --> 00:31:45,420 We're not going to put any methods on this quite yet. 606 00:31:45,420 --> 00:31:46,462 We're just going to save. 607 00:31:46,462 --> 00:31:50,860 It's basically a container for these two data types. 608 00:31:50,860 --> 00:31:53,550 So now let's come back to MainActivity. 609 00:31:53,550 --> 00:31:59,070 The first thing we want to do is create a list of tracks. 610 00:31:59,070 --> 00:32:00,520 So let's do that. 611 00:32:00,520 --> 00:32:02,970 Let's say we have a new List. 612 00:32:02,970 --> 00:32:06,570 Again, Java being helpful here, in this autocomplete, 613 00:32:06,570 --> 00:32:10,620 if I just press Enter we're going to have something really cool happen, 614 00:32:10,620 --> 00:32:15,600 which is that Android Studio automatically added that import for me. 615 00:32:15,600 --> 00:32:18,450 So when I hit Enter, not only did it type the word List, 616 00:32:18,450 --> 00:32:23,590 it also added this import to the top of my file saying, import java.util.List. 617 00:32:23,590 --> 00:32:27,240 And again, that import is necessary for me to use this class. 618 00:32:27,240 --> 00:32:30,160 So Android Studio is just being helpful with that. 619 00:32:30,160 --> 00:32:30,660 OK. 620 00:32:30,660 --> 00:32:32,040 So we have a list. 621 00:32:32,040 --> 00:32:35,550 Remember, the type of our list is a track, because it's a list of tracks. 622 00:32:35,550 --> 00:32:36,810 That sounds good. 623 00:32:36,810 --> 00:32:40,380 Let's call our variable tracks. 624 00:32:40,380 --> 00:32:43,530 And then same as before, let's just create a new ArrayList. 625 00:32:43,530 --> 00:32:45,960 So we'll say new ArrayList. 626 00:32:45,960 --> 00:32:49,720 If I hit Enter, notice that it's imported automatically. 627 00:32:49,720 --> 00:32:50,350 And that's it. 628 00:32:50,350 --> 00:32:52,975 We don't have to specify the type again in the right-hand side. 629 00:32:52,975 --> 00:32:55,650 We can just do this left right bracket thing. 630 00:32:55,650 --> 00:32:56,460 OK. 631 00:32:56,460 --> 00:32:59,650 So now let's add some tracks to this list. 632 00:32:59,650 --> 00:33:02,940 So remember, we're just going to say tracks.add. 633 00:33:02,940 --> 00:33:08,530 And then we want to create a new track that takes two parameters. 634 00:33:08,530 --> 00:33:10,597 The first one is the name of the track. 635 00:33:10,597 --> 00:33:12,930 And again, Android Studio is just being a little helpful 636 00:33:12,930 --> 00:33:16,380 here by putting in that name prefix. 637 00:33:16,380 --> 00:33:18,430 This actually isn't part of the source code. 638 00:33:18,430 --> 00:33:21,060 This is just something that's displayed in the IDE. 639 00:33:21,060 --> 00:33:25,710 But it's helpful just to remind you what those variable names were called. 640 00:33:25,710 --> 00:33:28,620 So let's create the mobile track. 641 00:33:28,620 --> 00:33:30,200 The instructor is Tommy. 642 00:33:30,200 --> 00:33:32,200 Again, being helpful with these parameter names. 643 00:33:32,200 --> 00:33:34,320 But this is not really part of your source code. 644 00:33:34,320 --> 00:33:37,240 You don't need to type name colon anywhere. 645 00:33:37,240 --> 00:33:39,180 So that's our first track. 646 00:33:39,180 --> 00:33:46,360 Our second track is the web track. 647 00:33:46,360 --> 00:33:48,600 And that's with Brian. 648 00:33:48,600 --> 00:33:55,430 And the final track is the games track. 649 00:33:55,430 --> 00:33:58,490 And that's with Colton. 650 00:33:58,490 --> 00:33:58,990 OK. 651 00:33:58,990 --> 00:34:03,880 So at this point we've now created a class to represent each track, 652 00:34:03,880 --> 00:34:06,580 we've created a list of those tracks, and then 653 00:34:06,580 --> 00:34:12,210 we've added these three instances of those tracks to that list. 654 00:34:12,210 --> 00:34:13,659 That looks good. 655 00:34:13,659 --> 00:34:17,350 So now let's create a list of students. 656 00:34:17,350 --> 00:34:19,980 So rather than saying Track here, I'm going 657 00:34:19,980 --> 00:34:25,530 to say String, since we'll just represent each class as a string. 658 00:34:25,530 --> 00:34:28,590 And you'll notice here that I don't need to import String. 659 00:34:28,590 --> 00:34:31,110 That's actually imported for me automatically. 660 00:34:31,110 --> 00:34:33,870 There's a package in Java called java.lang. 661 00:34:33,870 --> 00:34:36,699 And everything in that package is automatically imported. 662 00:34:36,699 --> 00:34:40,210 So no need to import that manually. 663 00:34:40,210 --> 00:34:43,550 So then we'll call them students. 664 00:34:43,550 --> 00:34:46,409 And again, let's just sort of add some strings here. 665 00:34:46,409 --> 00:34:50,100 We could say new ArrayList. 666 00:34:50,100 --> 00:34:52,920 But there's also a nice shorthand I can use in Java 667 00:34:52,920 --> 00:34:56,580 rather than saying new ArrayList add, add, add. 668 00:34:56,580 --> 00:35:00,492 I can use this other class called Arrays. 669 00:35:00,492 --> 00:35:03,990 It's also in java.util, imported automatically. 670 00:35:03,990 --> 00:35:07,950 And there's this method on the Arrays class called asList(). 671 00:35:07,950 --> 00:35:10,920 And this is an example of a static method. 672 00:35:10,920 --> 00:35:14,310 Notice that I didn't say new Arrays anywhere. 673 00:35:14,310 --> 00:35:19,680 Arrays is just the class itself, and now I'm calling the static method. 674 00:35:19,680 --> 00:35:21,900 And now I can just actually put in a list of strings. 675 00:35:21,900 --> 00:35:22,920 So I can say-- 676 00:35:22,920 --> 00:35:26,250 let's just pick three totally random normal names 677 00:35:26,250 --> 00:35:28,745 like Harry, Ron, and Hermione. 678 00:35:28,745 --> 00:35:32,480 679 00:35:32,480 --> 00:35:33,150 And there we go. 680 00:35:33,150 --> 00:35:39,710 So this is actually going to give me back a list of strings. 681 00:35:39,710 --> 00:35:41,850 So now we want to do two things. 682 00:35:41,850 --> 00:35:46,880 The first thing we want to do is randomly assign students to tracks. 683 00:35:46,880 --> 00:35:50,990 And then after that we want to print out the resulting assignments. 684 00:35:50,990 --> 00:35:54,550 So to do that assignment, we're going to use a map. 685 00:35:54,550 --> 00:36:00,920 And this map is going to map strings, which represent students, to tracks. 686 00:36:00,920 --> 00:36:03,050 So let's declare that map. 687 00:36:03,050 --> 00:36:06,770 So we're going to say Map, imported automatically. 688 00:36:06,770 --> 00:36:10,620 And remember, this time we need to specify two different types. 689 00:36:10,620 --> 00:36:14,420 The first type is the type of our key, which is a string. 690 00:36:14,420 --> 00:36:18,680 And the second type is the type of the value, which is a track. 691 00:36:18,680 --> 00:36:21,830 So we'll call this map assignments. 692 00:36:21,830 --> 00:36:24,710 And then we'll say new HashMap. 693 00:36:24,710 --> 00:36:28,490 Again, Map is the interface here, HashMap is the class. 694 00:36:28,490 --> 00:36:31,960 Don't need to specify anything on the right-hand side. 695 00:36:31,960 --> 00:36:32,750 All right. 696 00:36:32,750 --> 00:36:34,250 So now we have this map. 697 00:36:34,250 --> 00:36:37,430 It's empty, so let's put something in it. 698 00:36:37,430 --> 00:36:39,760 So to do that, we're almost surely going to need 699 00:36:39,760 --> 00:36:41,560 to iterate through all of the students. 700 00:36:41,560 --> 00:36:52,160 So let's use a for loop for that and say, for String student in students-- 701 00:36:52,160 --> 00:36:55,873 so now that's just going to walk through that list one by one. 702 00:36:55,873 --> 00:36:57,790 And so what I want to do with each student is, 703 00:36:57,790 --> 00:37:01,570 I want to figure out how to get a random track. 704 00:37:01,570 --> 00:37:06,430 So luckily Java, again, has a nice class that's part of the standard library 705 00:37:06,430 --> 00:37:07,600 that we can use. 706 00:37:07,600 --> 00:37:12,070 And that class is called Random, also located in java.util. 707 00:37:12,070 --> 00:37:15,370 I can just say Random, let's just call this object random. 708 00:37:15,370 --> 00:37:19,510 And it's going to be a new instance of the Random class. 709 00:37:19,510 --> 00:37:23,270 Constructor doesn't take any arguments, so we can just say that. 710 00:37:23,270 --> 00:37:26,650 And now to generate that random number, I can just say, 711 00:37:26,650 --> 00:37:32,440 my index is going to be random.nextInt()-- 712 00:37:32,440 --> 00:37:36,050 again, autocomplete helping me out here. 713 00:37:36,050 --> 00:37:38,240 And this is going to take one argument. 714 00:37:38,240 --> 00:37:41,810 And this argument is going to be the maximum random number 715 00:37:41,810 --> 00:37:43,220 that you can generate. 716 00:37:43,220 --> 00:37:48,400 And so that's just going to be the size of my list. 717 00:37:48,400 --> 00:37:50,680 So with the List object, I can just say .size(). 718 00:37:50,680 --> 00:37:53,290 That's a method that's defined on List that 719 00:37:53,290 --> 00:37:57,520 will give me back an integer value representing how many students are 720 00:37:57,520 --> 00:37:58,850 in the list. 721 00:37:58,850 --> 00:38:03,820 So now that we have this index, let's use it to get one of our tracks. 722 00:38:03,820 --> 00:38:08,190 So I'm going to say assignments.put, just like before. 723 00:38:08,190 --> 00:38:10,060 And autocomplete here again, reminding me 724 00:38:10,060 --> 00:38:13,930 it's going to take two arguments, a key and to value. 725 00:38:13,930 --> 00:38:15,970 My key is going to be a string. 726 00:38:15,970 --> 00:38:20,080 And that's the current student that I'm iterating. 727 00:38:20,080 --> 00:38:23,160 And the value is going to be one of the tracks. 728 00:38:23,160 --> 00:38:25,720 So to get value out of this ArrayList, I'm 729 00:38:25,720 --> 00:38:30,370 going to say tracks.get, pass in that index. 730 00:38:30,370 --> 00:38:34,680 And that's going to get me an object out of that array. 731 00:38:34,680 --> 00:38:38,830 So notice here that in arrays you can use that square bracket notation. 732 00:38:38,830 --> 00:38:42,940 The ArrayList is just a regular class that has methods defined on it, 733 00:38:42,940 --> 00:38:45,200 and get() is a method. 734 00:38:45,200 --> 00:38:48,190 You'll oftentimes when writing code, much more frequently used 735 00:38:48,190 --> 00:38:50,530 lists and maps compared to raw arrays. 736 00:38:50,530 --> 00:38:53,900 But they're there if you need them. 737 00:38:53,900 --> 00:38:54,680 OK. 738 00:38:54,680 --> 00:38:59,420 So at this point in our code we have defined a list of tracks, 739 00:38:59,420 --> 00:39:01,790 we've assigned students to those tracks. 740 00:39:01,790 --> 00:39:06,440 And now we just want to print everything out to see what the result is. 741 00:39:06,440 --> 00:39:09,410 So to do that, we're going to use another for loop, 742 00:39:09,410 --> 00:39:13,180 this time iterating over the map. 743 00:39:13,180 --> 00:39:14,700 So we're going to say for. 744 00:39:14,700 --> 00:39:18,000 Again, we have Map.Entry. 745 00:39:18,000 --> 00:39:21,240 And this is basically a class defined within the Map class 746 00:39:21,240 --> 00:39:26,940 that represents a single pair of key and values inside of the map. 747 00:39:26,940 --> 00:39:28,840 The type of this is going to be the same. 748 00:39:28,840 --> 00:39:32,340 It's going to be mapping a string to a track. 749 00:39:32,340 --> 00:39:36,690 We'll just call this Entry entry. 750 00:39:36,690 --> 00:39:39,406 And then we can say assignments.entrySet. 751 00:39:39,406 --> 00:39:42,200 752 00:39:42,200 --> 00:39:46,850 So this entrySet is going to return a set, which is very similar to a list. 753 00:39:46,850 --> 00:39:49,730 It just says that everything in this list is unique. 754 00:39:49,730 --> 00:39:52,370 But for all our purposes it's just a list. 755 00:39:52,370 --> 00:39:54,830 And so now that I have this entry, I just 756 00:39:54,830 --> 00:39:57,320 want to print it out to the screen. 757 00:39:57,320 --> 00:40:02,540 So when you're writing Java code, we saw before we have that System.out.println. 758 00:40:02,540 --> 00:40:05,090 And that's a way to just print to standard out. 759 00:40:05,090 --> 00:40:08,440 When you're debugging Android code in Android Studio, 760 00:40:08,440 --> 00:40:11,450 there's another class you can use called Log. 761 00:40:11,450 --> 00:40:14,120 And Log is basically going to allow you to print 762 00:40:14,120 --> 00:40:16,960 to the special area in Android Studio. 763 00:40:16,960 --> 00:40:20,850 You can search over logs and navigate them a bit more easily. 764 00:40:20,850 --> 00:40:22,820 So we're going to use that. 765 00:40:22,820 --> 00:40:24,680 So we're going to say Log. 766 00:40:24,680 --> 00:40:27,580 It's this class defined in android.util. 767 00:40:27,580 --> 00:40:30,520 And then there's a bunch of methods to find on this object. 768 00:40:30,520 --> 00:40:32,960 And a bunch of them have a single letter. 769 00:40:32,960 --> 00:40:37,510 So there is e() for error, there is d() for debug, there's i() for info-- 770 00:40:37,510 --> 00:40:40,770 that basically just says what level what we logging at. 771 00:40:40,770 --> 00:40:43,040 So like, are we making a debug print statement, 772 00:40:43,040 --> 00:40:45,290 does this log represent an error statement? 773 00:40:45,290 --> 00:40:46,750 It doesn't matter too much. 774 00:40:46,750 --> 00:40:49,640 We're just going to use d() for debug. 775 00:40:49,640 --> 00:40:55,640 And we see here that this method takes two arguments-- a tag and a message. 776 00:40:55,640 --> 00:41:00,540 So the tag is a way for you to really easily filter down messages. 777 00:41:00,540 --> 00:41:03,710 So when we launch our Android app, you're to see that a bunch of stuff 778 00:41:03,710 --> 00:41:04,670 is going to be logged. 779 00:41:04,670 --> 00:41:06,770 And much of it is going to be from the system 780 00:41:06,770 --> 00:41:09,060 or from other libraries we're using. 781 00:41:09,060 --> 00:41:11,570 And so we want to easily find our log statements 782 00:41:11,570 --> 00:41:13,680 among all of those other ones. 783 00:41:13,680 --> 00:41:16,560 So if we just create a tag of cs50, that's 784 00:41:16,560 --> 00:41:21,230 going to allow us to search for cs50 and easily find our log statement. 785 00:41:21,230 --> 00:41:22,700 So that's our tag. 786 00:41:22,700 --> 00:41:28,070 And then we want to print out this entry. 787 00:41:28,070 --> 00:41:32,420 And if I call the getKey() method, that's going to give me the student. 788 00:41:32,420 --> 00:41:33,780 So that's the student name. 789 00:41:33,780 --> 00:41:36,790 We can concatenate strings together with plus. 790 00:41:36,790 --> 00:41:39,426 So we can say this "student got." 791 00:41:39,426 --> 00:41:42,013 And now we can say entry.getValue().name. 792 00:41:42,013 --> 00:41:44,780 793 00:41:44,780 --> 00:41:47,240 So this is going to be the name of our track. 794 00:41:47,240 --> 00:41:51,130 And then I can say "with" followed by the name of the instructor, 795 00:41:51,130 --> 00:41:53,530 which would be entry.getValue().instructor. 796 00:41:53,530 --> 00:41:57,460 797 00:41:57,460 --> 00:42:00,910 And I forgot a plus, so I'll add that over there. 798 00:42:00,910 --> 00:42:01,510 OK. 799 00:42:01,510 --> 00:42:06,790 So now let's run our app using the AVD we created earlier to simulate 800 00:42:06,790 --> 00:42:08,980 this Android app. 801 00:42:08,980 --> 00:42:12,610 So up in the top right here you'll see this Play button. 802 00:42:12,610 --> 00:42:14,680 So I can just click this, or as it tells me, 803 00:42:14,680 --> 00:42:20,890 the keyboard shortcut is just Control-R. So let's click that Play button, 804 00:42:20,890 --> 00:42:25,270 which is going to open up our Android emulator. 805 00:42:25,270 --> 00:42:27,100 So here's my emulator. 806 00:42:27,100 --> 00:42:30,800 You can see this Android app was sort of automatically generated for us. 807 00:42:30,800 --> 00:42:35,080 But we're looking for that log output to see what our assignments were. 808 00:42:35,080 --> 00:42:37,870 So if we come down to the bottom here, we 809 00:42:37,870 --> 00:42:40,180 have this thing here that says Logcat. 810 00:42:40,180 --> 00:42:44,810 And this is basically just all of the logs coming in from our application. 811 00:42:44,810 --> 00:42:47,990 So you can see here there's a bunch of stuff being logged by the system. 812 00:42:47,990 --> 00:42:51,100 But if you scroll up, then you can see our assignment here, 813 00:42:51,100 --> 00:42:54,760 which just so happens to be one person in every class. 814 00:42:54,760 --> 00:42:58,430 And this is the result of our log. 815 00:42:58,430 --> 00:43:03,140 So before we finish, let's change one thing about our class. 816 00:43:03,140 --> 00:43:07,475 Rather than exposing these fields as public fields, 817 00:43:07,475 --> 00:43:10,770 a pretty common convention in Java is to provide what 818 00:43:10,770 --> 00:43:14,420 are called getter and setter methods. 819 00:43:14,420 --> 00:43:20,150 So to do that, I'm going to define methods on this track called public 820 00:43:20,150 --> 00:43:23,030 String getName(). 821 00:43:23,030 --> 00:43:25,760 All this is going to do is return the name. 822 00:43:25,760 --> 00:43:30,080 And another getter called getInstructor(). 823 00:43:30,080 --> 00:43:33,920 And that's just going to return the instructor. 824 00:43:33,920 --> 00:43:37,270 And so now, rather than exposing these fields publicly, 825 00:43:37,270 --> 00:43:41,140 I can instead make them private, which says 826 00:43:41,140 --> 00:43:45,370 that you can't just access these fields, because they're marked as private. 827 00:43:45,370 --> 00:43:48,010 Instead you want to use the getters. 828 00:43:48,010 --> 00:43:51,340 This is a pretty common Java convention, because you might 829 00:43:51,340 --> 00:43:54,370 want to do something inside the getter. 830 00:43:54,370 --> 00:43:57,340 You might have a convention where getting the value of something 831 00:43:57,340 --> 00:44:01,120 isn't just the value, it's that value, maybe it's modified a little bit, 832 00:44:01,120 --> 00:44:04,970 maybe it's doing some calculation on that value. 833 00:44:04,970 --> 00:44:07,630 So you don't really want to expose these fields directly. 834 00:44:07,630 --> 00:44:10,320 It's common to put them behind getters. 835 00:44:10,320 --> 00:44:13,570 And you'll notice in a lot of the code that we've written so far we've already 836 00:44:13,570 --> 00:44:16,730 used getters, like getKey() and getValue(). 837 00:44:16,730 --> 00:44:19,960 So we're just following that same convention. 838 00:44:19,960 --> 00:44:24,220 So now to modify the code that we have here, let's shorten this a bit. 839 00:44:24,220 --> 00:44:27,390 And let's say that we have a Track track. 840 00:44:27,390 --> 00:44:30,370 And that's going to be equal to entry.getValue(). 841 00:44:30,370 --> 00:44:32,690 So now we have a Track object. 842 00:44:32,690 --> 00:44:39,130 And now we can say track.getName(), use that method we just defined, 843 00:44:39,130 --> 00:44:44,460 and track.getInstructor(). 844 00:44:44,460 --> 00:44:49,040 You'll notice it if I try to use just track.instructor, 845 00:44:49,040 --> 00:44:51,650 the compiler is going to let me know that I have an error. 846 00:44:51,650 --> 00:44:53,840 You'll see here it's highlighted in red. 847 00:44:53,840 --> 00:44:55,700 And I have this little message that says, 848 00:44:55,700 --> 00:44:59,040 instructor has private access in this class, 849 00:44:59,040 --> 00:45:03,290 which means I can't access it from outside of the class. 850 00:45:03,290 --> 00:45:07,140 So by just saying getInstructor(), now we should be good to go. 851 00:45:07,140 --> 00:45:09,818 Let's run our app one more time. 852 00:45:09,818 --> 00:45:10,610 So we're launching. 853 00:45:10,610 --> 00:45:12,610 And you'll notice all the log is cleared. 854 00:45:12,610 --> 00:45:16,910 And if we just scroll up again, we can see a new random result. 855 00:45:16,910 --> 00:45:19,310 And by the way, this search bar is pretty nice up here. 856 00:45:19,310 --> 00:45:21,660 So I could, for example, search "Tommy." 857 00:45:21,660 --> 00:45:25,220 And I'm going to filter down only the log messages with "Tommy." 858 00:45:25,220 --> 00:45:29,120 So I could say "with" and only get the messages with "with." 859 00:45:29,120 --> 00:45:32,840 So for example, if I changed this tag just from "cs50" to something 860 00:45:32,840 --> 00:45:37,070 like "assignment result," I could then just search for "assignment result" 861 00:45:37,070 --> 00:45:39,332 and only filter down those log messages. 862 00:45:39,332 --> 00:45:41,540 So even though there's a lot of stuff happening here, 863 00:45:41,540 --> 00:45:44,480 that filter can be really helpful to separate out 864 00:45:44,480 --> 00:45:48,320 your log messages from a bunch of stuff the system is logging. 865 00:45:48,320 --> 00:45:51,080 So that's it for our first few lines of Java. 866 00:45:51,080 --> 00:45:56,350 In our next video we're going to write our first Android UI application. 867 00:45:56,350 --> 00:45:57,415