ShmoopTube
Where Monty Python meets your 10th grade teacher.
Search Thousands of Shmoop Videos
AP Computer Science Videos 112 videos
AP Computer Science 1.2 GridWorld Case Study and APIs. What is the direction of the actor?
AP Computer Science 1.4 Standard Algorithms. How many times will mystery be called for mystery(n) for n > 1?
AP Computer Science 2.3 Classes and Objects. Which of the following is correct implementation of the Country class?
AP Computer Science 3.2 Classes and Objects 183 Views
Share It!
Description:
AP Computer Science: Classes and Objects Drill 3, Problem 2. Which of the following is a correct implementation of the getPaddlerName accessor method?
Transcript
- 00:00
Sorry And here's your shmoop du jour brought to you
- 00:05
by access or methods access or methods I didn't even
- 00:09
access refrigerator They're mine Yeah shmoop drinking song Which of
- 00:22
the following is a correct implementation of the get paddler
- 00:25
name access or method And hear the potential answers thinking
Full Transcript
- 00:31
thinking like back All right here we go This question
- 00:39
is more or less a matter of syntax But wait
- 00:42
What is an excess or method anyway And what's the
- 00:44
point of using one Well it's essentially just a small
- 00:47
method written to retrieve the data from a private variable
- 00:51
But why bother Isn't that needlessly complicated Why not just
- 00:55
make our variables public and reader Change him wherever we
- 00:58
want Well because that's like flying without a net If
- 01:01
we funnel every request for a variables data through access
- 01:04
or mutate er methods we get another place to put
- 01:06
code to act on that date each and every time
- 01:09
it's accessed or modify Imagine this you're creating a huge
- 01:13
game where players are in gold by defeating monsters and
- 01:16
later they can return to town and buy items with
- 01:18
their goal We'll call it world of shmoop craft Yeah
- 01:22
very retold in there with you in the area of
- 01:24
code where monsters were fought and gold is granted we'll
- 01:27
be adding a small number of gold to the players
- 01:29
Gold amount variable every time a monster is defeated And
- 01:33
when players go to the shop to buy things we
- 01:35
subtract the necessary Amount from the same variable No access
- 01:38
or or mutate or methods to check up on us
- 01:41
were totally naked here Good Now let's say the game
- 01:45
has been released to the public and thousands of people
- 01:48
are playing games with each other And the mischievous lord
- 01:50
beef is has discovered something interesting about your game If
- 01:54
he stands in a certain spot holds the attack buttoned
- 01:56
down he can slay the spider queen with one swipe
- 01:59
over and over and over forever believes a roll of
- 02:02
quarters on the keyboard to hold the button down and
- 02:04
lets it run overnight amassing a huge fortune and completely
- 02:08
throwing off the in game economy Well everyone else is
- 02:11
harder and fortune is worthless in lord be fist ruins
- 02:13
the game Thanks for nothing lord beef How could access
- 02:17
or um you tater methods have prevented this catastrophe Well
- 02:20
a lot of ways actually when using accessory mutate or
- 02:23
methods tto handle the bold amount variable One possibility would
- 02:26
be insert some logic that checks whether the amount of
- 02:28
gold has been modified in the last few seconds and
- 02:31
if so does not allow it to be modified again
- 02:34
I don't keep anyone's gold amount from dramatically skyrocketing in
- 02:37
case of some un forcing repeating issue somewhere or just
- 02:40
spitballing here since they're likely be a variable story number
- 02:43
of bad guys each player has slayed a check would
- 02:46
be implemented in the accessory mutate Er methods there if
- 02:49
any player or beef is included manages to somehow sleigh
- 02:52
and otherwise impossible amount of enemies in one day say
- 02:55
a thousand a special condition could be triggered Handle it
- 02:59
Maybe it even rig something amazing like an automated text
- 03:02
in the middle of the night saying you need to
- 03:04
wake up and take care of lord be fist but
- 03:06
imagine explaining that one to your spouse Really though there
- 03:10
are probably thousands of creative ways you could use accessory
- 03:13
indicator methods to protect the sanity of your variables It
- 03:16
might seem tedious to implement those methods but they'll save
- 03:19
you in the long run Hart's back to our question
- 03:21
which is the correct way to set up in access
- 03:23
or method to get the variable paddler name option is
- 03:27
a routine that simply prince paddler name to the console
- 03:29
that's no good We want to return the variable so
- 03:31
we can work With it option c also just prince
- 03:34
paddler name both and we're out b d any all
- 03:38
returned the variable but b is being declared as a
- 03:41
private methods No dice there Option d doesn't specify the
- 03:44
words string in its constructor so nothing be option is
- 03:48
right on the money It's public it specify string and
- 03:50
it returns the variable and in a statement that's never
- 03:53
been uttered in the entire history of humanity Good job
- 03:56
everyone We've saved the freestyle canoeing competition from the scores 00:04:00.7 --> [endTime] of lord beef
Related Videos
AP Computer Science 1.2 GridWorld Case Study and APIs. What is the direction of the actor?
AP Computer Science 1.4 Standard Algorithms. How many times will mystery be called for mystery(n) for n > 1?
AP Computer Science 2.3 Classes and Objects. Which of the following is correct implementation of the Country class?
AP Computer Science 3.4 Inheritance, Abstraction, and Polymorphism. Which of the following will satisfy the conditional if statement for boo, str,...
AP Computer Science 4.2 Standard Algorithms. What kind of algorithm is the following?