Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this video we'll discuss all the requirements of our Card class. We'll then create our Card class using Java!
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
The first thing we're going to need to
make a game of Solitaire are some cards.
0:00
So let's kick this off by
making a Card class and
0:04
for now we're going to
create it as a Java class.
0:07
So let's create a new Java class and
name it Card.
0:10
Next, we need to specify
the attributes of a card.
0:18
Each card in our deck has three
attributes that we care about.
0:21
The value like ace, two or three,
which we'll be representing as an integer.
0:24
The suit, diamonds,
hearts, clubs, or spades.
0:29
And whether the cards face up or
face down.
0:33
Let's start by declaring a new field for
each of these attributes and
0:36
let's make them private.
0:40
So, private int value.
0:43
This will be for like ace, two or three.
0:46
private String suit,
0:48
and private boolean faceUp.
0:53
Then, let's create a constructor
to populate our three new fields.
1:00
So public Card and it'll take in a value,
1:04
A suit and a faceUp,
1:12
And inside the constructor,
we'll set these to our fields.
1:19
this.value = value,
1:24
this.suit = suit and
1:29
this.faceUp = faceUp.
1:33
All right, we've got our fields and we're
initializing them and the constructor.
1:38
But with each of these as a private field,
1:42
we don't have a way to access anything
about our card from outside of this class,
1:45
which makes for
a pretty useless card class.
1:51
Let's fix that by adding getters and
setters to our fields.
1:54
Starting with our value field,
let's put our cursor on
1:58
the word value and
then use Alt+Enter to add only a getter.
2:03
Once a card has been created,
its value should never change.
2:08
A two never magically becomes a king,
right?
2:12
So, we don't need to add a setter here.
2:15
Another way to say something doesn't
change is to use the word immutable.
2:18
So if I say this value field is immutable,
you know that it should never change.
2:23
A two will always be a two.
2:29
On the other hand, if something can
change, we would call it mutable.
2:31
Moving on to the suit field,
2:36
let's use Alt+Enter to generate
a getter here as well.
2:38
Just like the value of a card,
the suit is also immutable.
2:45
And so
we don't need to add a setter here either.
2:48
Finally for our face up field,
let's use Alt+Enter and
2:51
generate a getter and setter.
2:56
Since a card can change from face
up to face down or vice versa,
2:58
we need to include a setter
to let us make those changes.
3:03
It also means face up is mutable.
3:08
All right,
that finishes up our Card class.
3:11
In the next video, we'll start turning
this Java code into Kotlin code.
3:14
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up