1 00:00:00,220 --> 00:00:04,670 The first thing we're going to need to make a game of Solitaire are some cards. 2 00:00:04,670 --> 00:00:07,610 So let's kick this off by making a Card class and 3 00:00:07,610 --> 00:00:10,800 for now we're going to create it as a Java class. 4 00:00:10,800 --> 00:00:16,060 So let's create a new Java class and name it Card. 5 00:00:18,090 --> 00:00:21,228 Next, we need to specify the attributes of a card. 6 00:00:21,228 --> 00:00:24,569 Each card in our deck has three attributes that we care about. 7 00:00:24,569 --> 00:00:29,812 The value like ace, two or three, which we'll be representing as an integer. 8 00:00:29,812 --> 00:00:33,367 The suit, diamonds, hearts, clubs, or spades. 9 00:00:33,367 --> 00:00:36,600 And whether the cards face up or face down. 10 00:00:36,600 --> 00:00:40,670 Let's start by declaring a new field for each of these attributes and 11 00:00:40,670 --> 00:00:41,580 let's make them private. 12 00:00:43,150 --> 00:00:46,480 So, private int value. 13 00:00:46,480 --> 00:00:48,453 This will be for like ace, two or three. 14 00:00:48,453 --> 00:00:53,539 private String suit, 15 00:00:53,539 --> 00:01:00,240 and private boolean faceUp. 16 00:01:00,240 --> 00:01:04,963 Then, let's create a constructor to populate our three new fields. 17 00:01:04,963 --> 00:01:09,648 So public Card and it'll take in a value, 18 00:01:12,620 --> 00:01:16,388 A suit and a faceUp, 19 00:01:19,327 --> 00:01:24,157 And inside the constructor, we'll set these to our fields. 20 00:01:24,157 --> 00:01:27,480 this.value = value, 21 00:01:29,444 --> 00:01:33,767 this.suit = suit and 22 00:01:33,767 --> 00:01:38,776 this.faceUp = faceUp. 23 00:01:38,776 --> 00:01:42,817 All right, we've got our fields and we're initializing them and the constructor. 24 00:01:42,817 --> 00:01:45,667 But with each of these as a private field, 25 00:01:45,667 --> 00:01:51,206 we don't have a way to access anything about our card from outside of this class, 26 00:01:51,206 --> 00:01:54,960 which makes for a pretty useless card class. 27 00:01:54,960 --> 00:01:58,373 Let's fix that by adding getters and setters to our fields. 28 00:01:58,373 --> 00:02:03,196 Starting with our value field, let's put our cursor on 29 00:02:03,196 --> 00:02:08,337 the word value and then use Alt+Enter to add only a getter. 30 00:02:08,337 --> 00:02:12,900 Once a card has been created, its value should never change. 31 00:02:12,900 --> 00:02:15,920 A two never magically becomes a king, right? 32 00:02:15,920 --> 00:02:18,460 So, we don't need to add a setter here. 33 00:02:18,460 --> 00:02:23,210 Another way to say something doesn't change is to use the word immutable. 34 00:02:23,210 --> 00:02:29,820 So if I say this value field is immutable, you know that it should never change. 35 00:02:29,820 --> 00:02:31,960 A two will always be a two. 36 00:02:31,960 --> 00:02:36,560 On the other hand, if something can change, we would call it mutable. 37 00:02:36,560 --> 00:02:38,675 Moving on to the suit field, 38 00:02:38,675 --> 00:02:42,914 let's use Alt+Enter to generate a getter here as well. 39 00:02:45,134 --> 00:02:48,860 Just like the value of a card, the suit is also immutable. 40 00:02:48,860 --> 00:02:51,514 And so we don't need to add a setter here either. 41 00:02:51,514 --> 00:02:56,219 Finally for our face up field, let's use Alt+Enter and 42 00:02:56,219 --> 00:02:58,775 generate a getter and setter. 43 00:02:58,775 --> 00:03:03,231 Since a card can change from face up to face down or vice versa, 44 00:03:03,231 --> 00:03:07,270 we need to include a setter to let us make those changes. 45 00:03:08,410 --> 00:03:11,900 It also means face up is mutable. 46 00:03:11,900 --> 00:03:14,950 All right, that finishes up our Card class. 47 00:03:14,950 --> 00:03:19,130 In the next video, we'll start turning this Java code into Kotlin code.