 Welcome to Introduction to AP Computer Science Unit 5. Unit 5 is about writing classes. So I'm going to show you how to write a class today. So let's start out. I'm going to make a little class called Unit 5. And I'm going to do my standard public static void main string args. Again, by now this should be coming second nature. And basically what we're going to do is we're going to look at how to make a class and how classes work with objects. So 5.1 basically says that classes define objects which have attributes and behaviors. And I could put a U there for our British and Canadian friends if you'd like. But I'm American so we'll go with behaviors with no U. Now don't let the nomenclature, don't let the vocabulary kind of scare you off here. A class is just a blueprint or a description of an object. So an object is an element or something in a program that does something. And I'm going to show you what that means here in a little bit. To do that I'm going to create a new class called card.java, the card class. And first let me save this one. This is Unit 5. And I'm going to save that into my teacher. For now I'll just save that to my desktop. And for testing purposes. And so what I'm going to do is I'm actually going to make a new file. And I'm going to call this class card. Now in this case what I mean is a playing card. So like an ace of spades for example. So we're going to go ahead and talk about attributes. So what attributes does a playing card have? So for our game purposes it would have a name. And so I'm going to say private string name. Private string suit. And private private int value. So let's say we're playing blackjack. So the name of my card would be ace or a I could use. Suit could be would be spades or s I'll use s for spades. And the value of that card when ace can either be 11 or 1. Let's just use 11 now to make it easy. I could also do something like a king of spades which the value would be 10. Now we have here private and I'm going to explain that a little bit later in one of the other sections. But basically what private means is that these values cannot be directly accessed from outside this class. So if I'm going to say that real quick. So make sure everybody's on the same page and use my desktop and save. So basically what that means is I cannot directly access this private attribute from this other class. So my class is unit 5. I can't directly access the strings name. And we'll talk a little bit about that later. So again I haven't actually created a card here. What I've done is I've started to create the card class. So what I need to do is to actually create the card. Now we've seen this before when we did strings. This section is 5.2. This is called constructors. And a constructor obviously constructs something. So we've seen this before. So for example string name equals new string and say Bob mold my favorite singer. So we have our class here. We have our object name in this case it's name which is a new string. And we're passing this value Bob mold it's a string that's going to construct our object. Now this is a built-in object string. So what we're going to do is we're going to do the exact same thing but with our own card class we just created. So let's say I wanted to create a card. I need to make a constructor. So the constructor looks like this. So I'm going to say 5.2 constructors. Okay so I'm going to say public. It's got to be public because we want to be able to construct it from outside. And I'm going to call it card. Notice it is capitalized. So the class name and the constructor name are the same. And in this particular case I'm not going to pass any parameters or values to the constructor. So what I'll do is I'm going to say system.out.println. I am a new card. Now if I compile this and run it see what happens. No main methods etc etc etc. So when we try to run a Java class it looks for that main method. But this is not that type of class. So I'm going to go back to my unit 5 Java where I do have a main method. I'm going to run it and see what happens. Nothing which is what we expect. Because there's nothing actually going on here. It's just comments. So what I want to do is I want to create a new card. So just like we had string something equals new string. I'm going to do the exact same format. So card. I'm going to type up here not down there. So card. And I'm going to call my card. I guess card. Equals new card. Thesis. So what happens? When I create this card. It's going to look for this card class. Now I have them in the same folder. So that's going to be important. And notice I'm not sending any values here. So I go here. And what's going to happen is it's going to look for a constructor that matches that signature. So here there's no parameters. Here there's no parameters. So it's going to call this function. I'm going to go ahead and run that and see what happens. I'm going to go back to unit five Java. So you can see how creating this card called the constructor method. And it printed out I am a new card. Now this isn't super helpful for what we want to do with the card. Just kind of demonstrating the idea. So what we want to do actually is make a little bit more of a useful constructor. So I'm going to go ahead. I'm going to say I'm going to make a new constructor. And notice I can have two constructors. They have the same name. But they're going to have different signatures meaning they have different number of attributes or different number of parameters and maybe a different type of parameters. So in this case what I'm going to do is I'm going to say string my name, string my suit, and int my value. So what I'm going to do here is I'm going to say name equals my name, suit equals my suit, and value equals my value. Now these variable names, your name, suit, and value are the same that were created up here. Now notice these were created outside of any type of method. So they are going to be the attributes that apply to the card class. Or I should say objects of the card class. So what we did here is I'm going to pass the name, I'm going to pass the suit, I'm going to pass the value, and then assign it here. Now notice if you're coming from a different programming language such as Python, in Python here you would have to put something like self. Now I'll get to this a little bit later, but in Java it's actually this instead of self. But because everything is object oriented, it's going to assume that it's part of the object class definition. So it will use these automatically. You don't have to use this in this case. Now I'll leave it at that for now since this is an introduction. So then what I can do is in my new one I can say system.out.println. I am a space plus name, plus of, plus suit, plus with a value of, plus, value is getting very long, plus, and I like to put a period at the end to make it nice and complete, like that. So I'm going to save that, and I'm going to go back to my unit five. It's a little confusing because we're jumping back and forth. And I'm going to say my new card, it was, if I recall correctly, it was named. So this is going to be, we'll do king, and we'll do spades, which is an s, and the value is 10. So I'm going to run that. Okay, I am a k of s with a value of 10. So what I've done is I've actually created a card object, and its attributes are defined right here in the class definition. It's pretty cool if you ask me. So that's the first thing. So we've constructed it. We haven't done much with it, but we've constructed it. One of the next things that the unit is about is it wants to make sure that you do the following. So 5.3, document your code with comments. Now, everything you're doing should probably seem really logical to you right now, but if someone else has to come in and change your code or understand your code, comments are very, very helpful for maintaining a solid and maintainable code base. Now, with small programs, you might not feel it's that important, but trust me, even if you come away from something and come back, let's say you do something on a Friday, come back on Monday, you'll probably forget a few things. So make sure that you comment your code where necessary. If anything's not completely obvious, make sure you comment that. Okay, moving on. Access or methods. Okay, so this is 5.4. An access or method allows you to actually get a value from some type of object. So let me try this following. System.out.print. So we've got our card. Let's say card.name plus card.suit plus card.value. And I'm just going to leave it like that. Well, it's not going to work anyway. So I'm going to run this and I'm going to show you what happens. And I'll get to this a little bit later as well. So you see we have an error. Name has private access in card. So I can't access that value directly because over here, we put private. Now, I could change this to public. Let's run that again. So save that. Go back to unit 5. Run it. You can see ks10. So you can do that. However, it's not good programming practice. We want to make sure things are private. And there are some good reasons for that. But one good reason is that you're an AP student is that you will lose points if you're not making your class values and attributes private. So what we need to do is we need to use something called an access or method. And if you've done this before, we're using something called a getter, which is kind of an interesting name. So we've got setters and getters. We're going to talk about getters right now. So I'm going to say 5.4 access or methods. So notice I am in the card class. I'm going to be in access or methods. It's got to be public. And since in this case we're looking at the name, I'm going to be returning a string. I'm going to use getname. So note that format. It is get and then the variable name. So up here we have name. So down here we have getname. And all we're going to do is we're going to return name. So I'm going to save that. I'm going to go back to unit 5. And I'm going to go to getname. So I can pop my little function, or a method call I should say, into there. So I'm going to run that. Okay, so I got the name and that's the K. So it is returning the value from my object. So I can go ahead and I'm kind of lazy. So I'm going to go ahead and paste that. And we've got namesuit. I'm going to return suit. In this case, what are we going to put there? It's not a string, so we've got to put what? Hopefully you said int and get value. Return value. I'm going to go back to Java 5. And what I'm going to say here is plus a little space in there. Plus card.get suit. Plus space. And plus card.get value. So I'm writing my own getters. I'm writing my own accessor methods. And here I'm just using them. Let's run that. Got an error. Card.get suit. Okay, forgot the parentheses. Happens all the time. Okay, so remember this came from the constructor. So as soon as we created the object, it prints that out. We don't have to do that. This is just something we did to illustrate how it works. And then here, we used our accessor methods to get the values. Now we're printing them out. K, S, and 10. So accessor methods are one thing. Now what we want to do is we want to do something called a mutator method. So what we want to avoid, the thing that we're trying to avoid is something like this. So card.suit equals d for diamonds. We don't want to do that. Because again, we're trying to keep... Actually, one of the principles of object-oriented design is encapsulation. So we want to provide an interface for the programmer to change the values of an object. If we do this, then I could put my suit. I could put diamonds. Spades, hearts, and clubs. So I could say that, and that doesn't make any sense because it can only have one of those. So what we use to do this is a mutator method. So our accessor methods were called getters. Our mutator methods are called setters. So I'm going to go over to card.java and I'm going to do my setter methods. So 5.5, mutator methods. So in this case, watch what I do. So I'm going to say public again because we want to be able to do it from outside this particular class. Void because I'm not returning anything in this case. So set name. And here I have to actually pass a value. And what should we call this one? String. I'll call this new name. That kind of gives us an idea of what it actually does. We don't have to do it that way, and I have to say name equals new name. And again, I don't need to return anything. I just need to do it. And again, because I'm kind of lazy, I'm just going to go ahead and copy that. And some IDs will actually automatically do this for you. It's pretty cool. So this one was set suit. And I'll call this new suit. And I'll call this set value. String new value. And miss that, didn't I? So I'm going to go back to now my unit five. And I'm going to change my card. I'm going to say card dot set name. I'm going to change it to a queue. I'm going to get a queen. Card dot set suit. I'm going to get hearts. And card dot set. I actually don't need to change the value because it's the same, but we'll just go ahead and test it anyway. Make it consistent. And I'm going to go ahead and run that. Set value. Incompatible type. String cannot be converted to int. So I made a mistake over here. So this should be int new value because I'm passing it on an int. So it says little things. Java, it tells you where the errors are. Okay. Oops, I didn't print it out so we don't really know what it is. But before I print that out, I mean I could just copy this line here. But what I want to do is I want to show you something called to string. This is a special Java method that attaches to all objects. And what I can do is I make something called a to string method. This is actually, I should have put this in section 5.4. So I'm going to scroll up and go ahead and do that there. So I'm going to call public. And it is going to be void. And it is called, sorry, public string and to string. So what I'm going to do is I'm going to actually return what I want to be printed. So watch what happens here. So name, plus name, plus quote space, suit, quote, plus the, plus space value, and plus, don't forget the space is there, and value. Okay, so what I'm doing is the to string method. Watch what happens. Here's how I use this. If I go back to my unit 5 and I say system.out.println and I just put in here card. Now card is not a string. It's not a string object. So, but what happens is it calls the to string method. And that, where did I put that? Yeah, so it's going to return this string with those values. So I can just print, I can use the print method, the print line method in this case to print out the information. So let's try that. So there we go. Suit didn't work. What did I do wrong? Let's put it in, suit. Let's go back and, no main methods I know. Go back to unit 5. Okay, so I changed it from a king of space, which is value of 10, to a queen of hearts, which has a value of 10 as well. So we have our accessor methods and we have our mutator methods. And now we know our to string methods. Okay, next up is writing methods. Okay, 5.6. So we can make methods that refer to this particular object. Okay, so for example, let's say I wanted to make a method in my card class that prints the card out on the screen. So what I'm going to do is I'm going to make 5.6 writing methods. Now this method applies to objects of the class, public, so we're going to be able to access it from outside, void print card. So what we're going to do, and watch this, is system.out.println and I'm going to do plus, dash, dash, plus. And then I'm going to copy that to make life easy. And then I'm going to do the same here. And here instead of pluses, I'm going to put a pipe, a vertical pipe, copy that, oops, and copy that. So what I can do, now watch this, is in my unit 5 class. So 5.6, that's going to be writing methods. Oops. So what I'll do is I'll say card.print card. Let's see what happens. Okay, that's kind of cool, but not super exciting because we don't know what kind of card it is. So what I'll do over here, is I'll go ahead and do this. Quote, plus, plus, quote. And in here, I'm going to put my suit. And then in the middle, I'm going to put quote, plus, plus, quote. I'm going to put my name. And over here in the bottom right, I'm going to put quote, plus, plus, quote. And I'm going to put my value. Now you might not have that on a card, but let's try it, see what it looks like. Oops, got to run unit 5 over here. So you can see. Okay, let's get rid of the value. That's going to mess things up. That was my mistake. Because a 10 is going to cause problems. So let's just do this one like this. Keep it with a dash. Keep it with a dash. Save it. I'm going to go back to here, run it. That's what I do wrong. I forgot to save it, didn't I? And go back to here. One more time. Now why are you doing this to me, computer? Save, save. Okay, finally. So this is a queen of hearts. Actually, I don't really like the way that looks. So what I'm going to do is I'm going to go ahead and back here. I'm going to put the suit down here again. I think that'll look very nice. There. I think it's just a little delay, but I'm going to let that go. So that should basically work the way we want it to work. Okay. So basically what you can see is we can add methods, or basically these are the, how do you say it, the various behaviors of that object. So the object has a method to print itself out essentially. Let's go back to here. That's annoying me. Okay. So now it's done. And I'm going to go ahead and run that again. There you go. So it is a queen of hearts. Not the greatest graphics in the world, but you know, kind of old school. Okay. So that is a method that applies to a particular object of a class. But we also have something called static methods. Okay. And probably we've been working with these already, but in case you haven't, depending on what class you're taking, if you're taking my class, you've probably done this before. But if not, we're going to make a static method. Now we have seen this already. Public static void main. Okay. So this static means it is not attached to the objects of the class. It's actually attached to the class itself. So I'm going to go back over here. And I'm going to say public static void. Say print card. Okay. And then here, I'm going to say string name. Oops. No quotation marks. String name. And string suit. Now you're probably wondering what's different between this print card and this print card. The first thing is static. Static is a big difference. So because this is not static, it only applies to that particular object. So I might have card one, card two, card three. It will print that out. This particular method, it's static. So it is attached to the class itself. And what that means is I'm going to do this. I'll show you what that's going to mean. So I'm going to copy this. So I'm going to use suit, name, and suit. Perfect. Okay. So because this is static, what I'll do over here in unit five, watch what I do here. Capital card, print card. And let's say we're going to do ace of spades. So I have not created a particular card object. I just want to print out what an ace of spades would look like. So because this is a string and this is a string. Okay. And this is the class name. Notice that. So card.java, card. So it is attached to that class. So let's run that. See what happens. Okay. So there is my ace of spades. So queen of hearts and ace of spades. So what this lets us do is it lets us do, makes little functions that aren't attached to an object. We've already seen this with the math class and things like that and string classes. So that we've already done, but this is kind of how it works. Okay. So notice it's not attached to a particular object. Sorry. That was 5.7. Okay. So that is that. Yeah. We'll leave it at that one. And that's 5.7. Okay. And then the next one that we got to look at is something called scope. I'm going to go to in detail here, but I'm going to talk about it just in brief. So if I say, now I talked about this earlier in the video a little bit. When we created our card class, we created several attributes and we made them private. And I showed you what would happen, the difference if we made them public. Okay. So when you create classes for AP Computer Science, make sure you are using private attributes. And if you want to change them, you need to use the accessor methods, or if you want to change it, you need to use the mutator method. If you want to access them, you need to use the accessor method. So the sets and get. Okay. But what we want to do in this case, okay. Oops. So we want to talk about, excuse me, we want to talk about the scope and access. So access is what we've really just done already. So access, I cannot, I cannot do this following. I cannot say card.name equals, say t for 10. 10. And then card.whatSuit equals d. So let's say 10 of diamonds. We can't do that because this is private and this is private. So because I'm not inside this class, I can't access that information directly. That's what access means. So there's public and there's private, which we'll be using. There's also a third one called protected, which we don't really need to worry about for the AP. So I'm not going to talk about that here. But we do need to know the difference between public and private because that's very, very important distinction. Now scope is a little bit more difficult to understand just because, how can I put it? Let me try this real quick before I go into system.outs.println, card.name. So let's run that and see what happens. So we saw this before. So line 31, put view line numbers. It's a little bit easier. Sorry about that. Name has private access. So I can't access this directly. I have to use my get name method. So I'm going to run that just to show you that as a reminder. So that's basically what access means. So what can you access? Now I can access this method because we declared it to be a public method up here. Because it's public. Now if I change this to private, I could not access that. I would get an error. Oops, and I had to spell it correctly. But you get the idea. So that's that. Now scope is a little bit more complicated in a way because it's not clearly defined. But I'll show you like a quick example just to get the idea into your head. So let's say in my constructor, I did this. Oops. Again, I wouldn't do this because this doesn't make any sense. But I say int x equals 10 system.out.println. See x is x. Okay, so let me run this real quick. Just make sure it's working. No main methods. Yep. Sorry about that. I'm going to go back and run that. So if I go back up here. So x is 10. Now notice I didn't declare whether it was public or private. Okay. Because it's inside this method. So because it's inside the method. Okay. It's scope. The only place it exists is inside this method. So if I tried to print that x from anywhere else, it's not going to work. So for example, if I went over to here and say unit 5 Java and said system.out.println x. And I did something like card.x. Okay, and I run this. The compiler is going to flag an error, I imagine. Okay. So error cannot find symbols. So it's pointing here. So it's telling you, it knows what card is. It doesn't know what x is. Okay. It's because we did not define it out here outside of the method. Okay. So the only things that we can access are the ones that are inside or outside of a method. This one was defined inside of this method. So it only exists inside this method. That's why here I can print it, but I cannot print it from anywhere else. Once this method is done and finished, the x disappears from memory. That's how we keep the memory leaks and from happening. Okay. So it actually Java is very kind to you. It's called automatic garbage collection. It realizes that, hey, we're basically done with this thing. We're not going to use again. I don't need to maintain that in memory. Okay. So that is 5.8 scope and access. Access.