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
Now you try. Write a class on your own, then we'll walk through it together.
This video doesn't have any notes.
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
Let's take another look at
a graphical representation of a map.
0:00
We need to be able to specify where on
the map a tower or an invader is located.
0:04
We can think of our map as being
divided up into a two-dimensional grid.
0:10
Each grid square is a location where
an object can be placed on the map.
0:14
This allows us to use Cartesian
coordinates to identify grid squares.
0:19
Cartesian coordinates are used to identify
a point on a two-dimensional grid.
0:24
Traditionally variables named X and
Y are used to specify a point.
0:29
X is the distance from
the left-most grid square.
0:35
Y is the distance from
the bottom-most grid square.
0:39
A tower located at the bottom left corner
of the map would be located at x=0, y=0.
0:42
And this tower is placed at point x=3 and
0:48
y=1 or point 3, 1 for short.
0:53
Where is this tower located at?
0:57
That's right.
0:59
It's at point 5,4.
1:00
So a point on the map has both an X and
a Y coordinate.
1:03
Let's model a point using a class.
1:07
We'll need to create a new
file called Point.cs.
1:10
We'll define this class inside
the Treehousedefense namespace.
1:15
So our Point class is going to look
very similar to our map class.
1:24
It will have two fields,
one called X and one called Y.
1:28
Just like our Map class
needs both a width and
1:33
a height, the Point class will
need both an X and a Y coordinate.
1:35
You know everything you need to
know to write the Point class.
1:40
I suggest you pause the video here,
and go ahead and
1:44
do that on your own for practice.
1:47
Then when you come back,
we'll work through it together.
1:49
All right, how do you think you did?
1:55
Let's take a look.
1:58
We'll make a class and call it Point.
1:59
Then we'll have two public fields in
our class, one for X, and one for Y.
2:04
We'll make them both integers, and
they'll both be public because we'll need
2:09
to be able to read them
from other classes.
2:13
Points don't move, so
we need to make both X and Y readonly.
2:16
We'll need to add a constructor
to initialize X and Y.
2:25
It will need to be public and
take an x and y parameter.
2:28
We'll use these parameters to set
the value of the X and Y fields.
2:32
There we have it.
2:45
As I said, the point class looks
almost identical to the Map class.
2:46
Only the names are different.
2:52
What it means to be a point and
2:55
what it means to be a map are still
two very different things though.
2:56
Remember to compile your code to
make sure that you're not getting
3:01
any compiler errors.
3:04
If you do get compiler errors, check
to make sure that your code looks like
3:05
the code written in these videos and
that all of your files are saved.
3:09
Way to go.
3:14
You're writing classes and
you're making objects.
3:15
Objects are a fun way to think about
designing software, don't you think?
3:18
As we learn more about the capabilities
of C# to do object-oriented programming,
3:22
it's going to get even
more interesting and fun.
3:27
We are off to a great start,
but this is just the beginning.
3:30
We've learned about classes and fields.
3:34
Next we'll learn about methods and
their role in objects.
3:36
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