 All right, let's start with chapter 12 and I'm going to go with what the book has and we're going to design a class that is good for representing a playing card and It's going to have a rank and a suit and the question is what types should they be now? We could have a string such as a spade for suits and queen for ranks But the problem is as they say in the book You can't easily compare them to see which has a higher rank or suit for example The word king comes before queen in the alphabet, but king is a higher rank so what we decided to do is we're going to use integers for the ranks and suits and We're going to say clubs is suit zero diamonds is one hearts is two spades is three and Then for the numerical ranks two through ten It's the corresponding integer ace is low and then jack queen and king are 11 12 and 13 And here's by the way the umL diagram. They didn't put one in the book, but I'm giving you one now So we have a private integer rank and suit and then we have a constructor where you give it a rank and suit And that creates the card for you We have a getter and not a setter and this comes in a little bit later in the chapter We have to decide would we like someone to be able to change The rank and suit as part of the program and the answers No, the three of hearts should probably always remain the three of hearts. You really don't want it to change So we have getters, but not setters We'll have a two string method and an equals so we can compare to see if two cards are the same or not When we're playing the game by the way, we're also going to need to be able to compare cards And let me just check to see how the book defines that The books definition is the compare to method Which is going to be a instance method. Okay, so it's not going to be static and we're going to have compare to Card Other and that's going to return an integer They use card this this and that I'm using this and other but it's the same idea and We'll go into that in a bit more detail So now let's start implementing this and I believe I have part of it implemented already here Having copied it from the book Some of this we've already done before We have our attributes first and then here's our constructor Here's the two string method what we're going to do is we'll create a string array That has the ranks which is null because there is no card with a value of zero and then ace through ten Jack Queen King For our suits we decided the clubs was zero So we have an entry in element zero of this array Then when we want to convert a card to a string we say take the rank name for the card rank with the word of and then follow it by the suit that the card belongs to and We turn that for string there To check to see if two cards are equal we want to check that the ranks are the same and The suits are the same I Haven't done the getters and centers yet Let's just test this out to see that this is all working I guess I'll have a public class card tester and I'm gonna save this in the same folder to avoid Oops, I don't want to use the students file. It's an example files. Hello earth to Eisenberg and We're gonna call this card tester Java and what we're gonna do here is Check to see that the constructor and other methods of the card class Work as advertised And I guess I should put my name in there on the date just for the heck of it So let's make a card Call it test card and that's going to be a new card That's going to be a three and see if clubs is diamonds hearts is two and Then we should be able to say system that out That print line of test card and let's compile that oh Yeah, put a blank between new and card that would help And let's run it and three of hearts cool. It's working now Again, there's nothing wrong with what we've written so far, but there is something that bugs me a little bit name like every time I create a Every time I want to print a card. I'm recreating these arrays because remember these are local to the two string method Okay, I've got an idea Let's take these and make it a public Variable then everything will work fine now it's allocated only once instead of every time I print out the value of the card and if I come here and I compile this again and Run it. It still works great Again, nothing wrong with it, but there's again something that's bugging me Each card has its own rank Different cards can have different ranks. That makes sense Each card has its own suit different cards might have different suits But these variables here are going to be the same for every card that we create each card does not need its own copy of This array each card does not need a separate copy of this array What we would like to say is no no no this does not belong to each instance We want to belong to the class as a whole and that's what we use static for now when we allocated this These two arrays belong to the class as a whole and they will not be Allocated per instance Let's make it even better Let's say we want this to be final Which means that these names are never going to be changing and In that case we have to by convention Uppercase their names and let's compile that and when we come here notice that we don't have to change anything in our test program It still works exactly the way it did before Now remember when I said the three comma two I had to think oh wait a minute Was that you know clubs spades diamonds hearts? I couldn't remember what it was Let's do some more fun with static Let's make Excuse me a second here. Sorry had to cough there Let's make a public final int Clubs is zero Public final int Diamonds is one hearts is two and Public final int spades is three This is not in the book. I'm just doing this because I think it's going to be nicer Now that I have oh and these can be static also by the way Again, I don't have to create a new version of all of these variables for every new Card that I create. This is shared among all of the cards Let's compile that and then when I'm going into my card tester program. I can say card heart And that makes things a lot more readable and I don't have to remember which number Corresponds to which value everything still works great Let's do card two will be a new card of three Card dot clubs and then let's ask if they're the same. We're going to test our equals method And let's check to see that equality works on things that really are equal By comparing card two to itself So we should get a false and a true when we run this Well, that's exciting Yeah, how about system dot out front line That will work a lot better Excellent so so far so good Again, we have the getters and setters or the getters. Oh, we haven't written the getters. Hello time to write those Okay, we have a public int Get rank Which will return this dot rank We'll get a public and get suit Which will return this dot suit Hey, we've almost got this as an immutable class and I'll put a note in here There are no setters in this class Once a car or it is created It's ranked and suit never change I'm going to look in the book here They have also something that needs to be done here to make this happen and that is We want these also to be final If you make them final then nobody can change it not even our own class can change it later on Come here and make sure that everything still compiles and runs properly Cool now the next thing we want to do is we want to be able to do a comparison and that's going to be a public int compare to card that Namely, which is the other card and now we have a decision to make How do we compare two cards? Do we compare the rank first and then the suit or do we compare the suit first and then the rank and Let's see what the discussion in the book says here because in a card game You want to see if one card is higher or lower than another We can't use the less than and greater than but we're going we have our compare to method And Says some types are totally ordered and that means you can compare any two values until which is bigger So integer and strings are totally ordered Some types are unordered which means there's no meaningful way To say one element is bigger than another like true and false. Okay. Well, which one's bigger true or false That doesn't make any sense at all Playing cards are what are called partially ordered Which means sometimes we can compare cards and sometimes not highlight that there we go Sometimes we can compare cards and sometimes not Yeah, so the three of clubs is definitely higher than the two of clubs the three of diamonds is higher than the three of clubs because Diamonds are a higher suit than clubs are But which is better the clubs to three of clubs are the two of diamonds And so we just have to make an arbitrary choice and as the book says When you buy a new set deck of cards that comes sorted with all the clubs together then all the diamonds and so on So we're gonna say that suit is more important and here is Our code and now if I were to paste this directly it would come out Misaligned notice how this is not indented properly So again, what do I want to do when I find something like this? The answer is I Want to go to the Java online formatter is and Then I will Paste the code in here totally unindented that I don't care. I will beautify and There it is indented properly Plug it in Now I have to indent it to where I want it, but that's no problem. I select The lines and press the tab key So here's how it works If this suit of this card is less than this other card suit then I return a negative one It's less if it's greater. I return one And otherwise the suits must be equal. That's the only other choice Because the suits are equal If I let's do this here, then I return a Negative one if less than positive one of greater and If neither of those the case I'll return zero. That's one way to do it and We'll leave it this way There's a faster way to do it, but this is this is readable. It's understandable. So Well, let's let it go. In fact, let's compare these two So we're going to have test card Compare to card two gives plus test card Dot compare to a card to so we're going to compare the three of hearts to the three of clubs And let's see which one is and the answer gives us a one because the test card The three of hearts is more important than the three of clubs Let's make a card card three is new card of two card dot Spades and then let's do system the out about print line. In fact Let's make this better here That way we can see what everything really is and let's try card three here So the three of hearts is less than the two of spades because spades are more important than hearts And we do suit first rather than rank Okay, wonderful. We now have our card implemented Now, let's make a deck of cards by creating an array of cards And what we're going to do is create an array of 52 cards And we may as well do this in let's make this Save this as card array dot job it So now we're going to Create an array of card objects and Tested out hard array And we're going to pop this in here Now what this does is this creates an array of 52 references card objects All initially set to null in fact Let's try this system out dot print line of cards sub zero and see what happens and sure enough it shows up as a null We'd like to initialize the car there are the This array and create 52 card objects to fill it up. Let's see what the book has here and the way they do it is For suit suit less than or equal to three and rank from one to 13 Okay, that's Interesting hmm, I'm sorry. I'm thinking about something that I could do but I Would get us a little bit off-traction Here we go. Let's copy this So this index tells us where we're going to put the card We'll have the suit running from zero up to and including three. I would actually Well, you know, we could do not I could do this I could say Card spades because that happens to be a three, but let's leave it as a three and And then the ranks go from one to 13 and let's go and compile that and Run it Well, that doesn't help us very much. How do we know that it's been initialized correctly? The answer is for Do I want to use I let's use a regular for loop here for int index is zero Well, I can't do I think I'm gonna run into problem if I do that Anybody think they're gonna see the same problem that I'm seeing Give you a couple of seconds to think about it and if we compile this it's gonna tell us. Oh, no variable index was already defined So I don't need to say int index. I Can reuse the same variable and let's go here and there we go card zeros ace of clubs All the way through the king of clubs, then we have our diamonds the hearts and spades And again, they're cards number zero through 51 because remember arrays always start at zero for their index numbers Okay, this is a decent place to stop right now What I'm going to do tomorrow is I'm going to continue on with 12.7 and 12.8 how we search through an array of cards And I might even I might not use the card deck on this one. I might do something different Let me give that some thought let me look at what the book is doing here. Yeah, they're doing cards here Okay, I see what they're doing and I think I'm gonna go with what the ball of what the heck I'll go with the book And I'll see you all for tomorrow's mini lecture