Don't fear the OOP - PowerPoint PPT Presentation

About This Presentation
Title:

Don't fear the OOP

Description:

By Johannes – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 29
Provided by: Johannes71
Learn more at: https://sep.stanford.edu
Category:
Tags: oop | don | fear | hair | sample

less

Transcript and Presenter's Notes

Title: Don't fear the OOP


1
By Johannes "Jos" Dianovich Claerbout (PowerPoint
version prepared by Pierre Dagher)
2
A presentation that shows you why Coding Java (or
any other object-oriented programming) is just
like writing a trashy Western novel.
or
How to understand Java by looking at pretty
colors.
3
  • A SIMPLE ANALOGY
  • In this presentation, the analogy is simple.
  • Think of a java programmer as a writer, composing
    a stock novel.
  • All of the characters and settings are
    "off-the-shelf", and need be only modified
    slightly to fit into a new book.
  • All that's left to write a bestseller is to come
    up with a plot that pulls all those pre-existing
    elements together.

4
LEARNING BY COLORS Three levels Green is for
the most basic introduction. It helps make things
clearer. Yellow is a step closer to
object-oriented programming away from the
intricacies of Java code. And finally, red is the
real thing. It is for you daredevils who want to
be able to program in Java, but just want to ease
into it slowly. In short, The green text gives a
"plain English" version of the code. The yellow
uses that English in a way that more closely
resembles the format of code. And the red is the
actual code that would be necessary for the
program to work. Although this presentation
operates mostly through analogy, innuendo, and
intrigue, those words that appear in boldface are
the actual terms used by Java programmers.
5
MEET EUNICE Eunice writes very popular Westerns.
Her books are very consistent. Her secret is to
write the whole story onto one sheet of
paper. How can she do that? Simple. Everything
she writes on the page is just a sequence of
events, or plot. Setting, character development,
and how her characters interact is all taken care
of elsewhere. How does she do that? Let's find
out.
6
THE WESTERN TOWN
The first thing that Eunice needs is a setting.
So she pulls down a binder she has marked
"Western Towns", opens to the first page, and
reads the first few lines
Every Western town has a few key ingredients
stables, saloons, sheriffs, and a couple
troublemakers. A standard town would have three
stables and be located West of the Mississippi
sometime around 1850.
  • This description
  • Establishes the key ingredients of a Western
    town.
  • Gives default values that may occur in a typical
    town.

7
WesternTown has a certain number of stables
has a certain number of saloons has a certain
number of sheriffs has a certain number of
troublemakers is located somewhere exists at
a certain time   a typical WesternTown would
have number of stables 3 location
Western America time period 1850
  1. First line Eunice is defining a Western town.
  2. Then she declares what sorts of variables (those
    things which define how the town looks) that a
    class of Western towns might have.
  3. Then she constructs a sample town, with default
    values for her variables.

8
public class WesternTown int
stables int saloons int sheriffs int
troublemakers String location int
time public WesternTown() stables
3 location "Western America" time
1850
  • Eunice is defining a class, not the town itself
    (that would be an object).
  • A class is like a recipe without any
    measurements. It says what elements should be in
    a Western Town, but does not say in what amounts.
  • Eunice will eventually create (instantiate) a
    town object in her book.
  • Her editor will look to the Western Towns class
    to determine how that town object should look
    like.
  • The constructor that follows contains default
    values for some of her variables.

9
THE MAIN PLOT
The Gunfight at the Old West Saloon This story
takes place in a Western town called Sweaty Post.
Sweaty Post has one sheriff, two saloons, and
five troublemakers. .
Main Gunfight Western Town sweatyPost is a new
Western Town the number of saloons in
sweatyPost is two the number of sheriffs is
one the number of troublemakers is five
public class Gunfight public static void
main (String arguments) WesternTown
sweatyPost new WesternTown sweatyPost.saloo
ns 2 sweatyPost.sheriffs 1
sweatyPost.troublemakers 5
10
HUMANS
Now for the villain Eunice picks up another
binder. This is marked Humans. This is what she
finds in the introduction
"All humans start out with two legs, two arms,
two eyes, a nose, a mouth and a hair color. They
are either male or female, have a name, have a
hair color, and have different preferences in
whiskey. If somone asks, humans can respond with
their name or their hair color."
  • Notice Eunice likes to make all body parts
    variables !
  • The sex, name, hair color and whiskey preference
    wil be defined later in the plot.

11
Human has a certain number of legs has a
certain number of arms has a certain number of
eyes has a certain number of noses has a
certain number of mouths has a name has a
certain sex has a hair color has a strong
preference in whiskey A standard human would
start with two legs two arms two eyes
one nose one mouth When someone asks for
your name Tell them your name When someone
asks for your hairColor Tell them your
hairColor
  • Eunice's human has some interactivity. She can
    ask its name or its hair color, and it will
    respond. This is called a method and is your key
    to a good time. We'll get to it later.

12
public class Human int legs int arms
int eyes int nose int mouth String
hairColor String name String sex String
whiskeyPreference public Human() legs
2 arms 2 eyes 2 nose 1
mouth 1 public String
whatIsYourName() return name public
String whatIsYourHairColor() return
hairColor
13
VILLAINS (they are also humans!)
Eunice found the human description not very
specific. Flipping through the "Humans" binder,
she comes to the chapter entitled "Villains"
Villains are based on the idea of humans. They
are identical, except that they have some
additional qualities, namely a moustache, a hat,
a certain "look", some level of drunkenness, and
a certain quantity of damsels in their
possession. Your standard villain is a male. He
will look mean, start the day out sober, and not
yet have captured any damsels.
In Eunice's binder (class) for humans we find one
of the subsections (subclasses). This particular
subsection about villains, extended the idea of
humans.
Villain extends the idea of Human. A
Villain has a moustache has a hat has a
"look" has some level of drunkenness has a
certain number of damsels tied up For a given
Villain, He is a male He will look mean He
will start out sober He will start the day
without having any damsels tied up
14
public class Villain extends Human String
moustacheColor String hatColor String look
int drunkenness int numberOfDamsels Human
damsel public Villain() sex
"Male" look "Mean" drunkedness 0
numberOfDamsels 0
You may be wondering why Eunice declared "Human
damsel" in this class. Think of it this way. If
the villain is going to tie up a damsel, Eunice's
editor has to know what a damsel is. By declaring
"Human damsel", her editor will know that a
damsel is a type of human.
15
VILLAINS drink
Eunice moves on to some of the methods that
villains employ to achieve their dastardly deeds.
She focuses on how villains drink. So she writes
Whenever the main plot says that a villain drinks
whiskey, his level of drunkenness will go up by
one.
Here Eunice is specifying how one of her
characters acts, rather than just how he/she/it
looks. This is how she can create quite a bit of
character development without ever even touching
her main plot page. You should also note that she
is altering one of her variables here, the
variable "drunkenness".
drinkWhiskey drunkenness increases by one
Notice the pattern than has begun to emerge in
Eunice's writing. As usual, she notes what she is
going to describe (in this case a method called
drinkWhiskey) and then, on the next line, what
that method will do when she calls it in her main
plot (routine). "
public void drinkWhiskey() drunkenness

16
VILLAINS tell
Now, the villains in Eunice's stories are famous
for being able to hold their liquor. Hence, it's
very difficult for an onlooker to gauge how drunk
one of her villains really is. Eunice decides
that it's a good idea to allow her villain to
state how drunk he is, so, she writes a new
method
If someone asks a villain how drunk he is, the
villain will always respond with his level of
drunkenness.
When someone asks how drunk you are tell them
how drunk you are
public int howDrunkAmI() return drunkenness

If you're wondering about that "int" in front of
the "howDrunkAmI()", Eunice had to put that in
because this method has a return value. In order
to let the editor know what sort of value was
being returned, she specified "int" (for integer)
in front of the name of the method.
17
and VILLAINS tie up damsels !
Now, Eunice decides to give the villain the
ability to tie up a damsel. What makes this
method different than her other methods is that
it needs information about someone other that the
villain, namely the damsel to be tied up. To
allow for flexibility (and for a variety of
damsels), Eunice decides to leave the identity of
the damsel blank for now.
When the villain is called to tie up a damsel,
tie up a specified damsel, then add one to the
number of damsels he has tied up. Then print out
"Oh my gosh! (our villain) with (moustache kind),
has tied up (the specified damsel), the pretty
damsel with (her hair color) hair!"
So now you can see the flexibility that Eunice
has in writing her stories she can leave certain
things to be specified only when the plot is
written. By writing that the damsel will be
specified, but not stating here who she is,
that's just what she has done.
tieUpDamsel (name) add one to the number of
damsels this villain has tied up print "Oh my
gosh! (our villain) with (moustache kind), has
tied up (the specified damsel), the pretty
damsel with (her hair color) hair!"
18
public void tieUpDamsel (Humans aDamsel)
this.damsel aDamsel numberOfDamsels
System.out.println("Oh my gosh! "
whatIsYourName() ", the villain with "
moustacheColor " moustache, has tied up
" aDamsel.whatIsYourName() , the
pretty damsel with " aDamsel.whatIsYourHairColor
() hair!")
What comes between the parentheses is called an
argument. It helps make the method more specific.
In the plot, we want to specify who has been tied
up? What is her name? How does she look? Hence,
when Eunice plot says "tieUpDamsel", it will also
identify the damsel being tied up as an argument.
"Humans", which comes before "damsel", tells her
editor that the thing being tied up is a human.
The next line is another attempt by Eunice to
please her persnickety editor, who is even more
picky than she is! Even though she has supplied
the name of the damsel as an argument, her editor
won't let her use it in the method until she has
stated that the "damsel" that she declared as a
"Villain" variable is equal to the value given in
the method's argument.
19
THE VILLAIN, all of the villain !(a review)
Villains are based on the idea of humans. They
are identical, except that they have some
additional qualities, namely a moustache, a hat,
a certain "look", some level of drunkenness, and
a certain quantity of damsels in their
possession. Your standard villain will look mean,
start the day out sober, and not yet have
captured any damsels. Whenever the main plot says
that a villain drinks whiskey, his level of
drunkedness will go up by one. If someone asks a
villain how drunk he is, the villain will always
respond with his level of drunkenness. If the
villain is supposed to tie up a damsel, tie up
the specified damsel, then add one to the number
of damsels he has tied up. Then print out " Oh my
gosh! (our villain) with (moustache kind), has
tied up (the specified damsel), the pretty damsel
with (her hair color) hair!.
20
Villain extends the idea of Humans. Every
villain has a moustache Every villain has a hat
Every villain has a "look" Every villain will
have some level of drunkenness Every villain
will tie up a certain number of damsels For a
given Villain, He is a male He will look
mean He will start out sober He will start
the day without having any damsels tied up
drinkWhiskey drunkedness increases by
one howDrunkAmI tell them how drunk I
am tieUpDamsel (a damsel) add one to the
number of damsels this villain has tied
up print "Oh my gosh! (our villain) with
(moustache kind), has tied up (the specified
damsel), the pretty damsel with (her hair
color) hair! "
21
public class Villain extends Human String
moustacheKind String hatColor String look
int drunkenness int numberOfDamsels
Humans damsel public Villain() look
"Mean" drunkenness 0 numberOfDamsels
0 public void drinkWhiskey()
drunkenness public int howDrunkAmI()
return drunkenness public void
tieUpDamsel (Human aDamsel) this.damsel
aDamsel numberOfDamsels
System.out.println("Oh my gosh! "
whatIsYourName() ", the villain with "
moustacheKind " moustache, has tied up "
aDamsel.whatIsYourName() , the
pretty damsel with" aDamsel.whatIsYourHairColor(
) " hair!")
22
THE MAIN PLOT
All this work with drinking and tieing up damsels
is making Eunice feel a bit run down, so she
decides to stop working on her characters for a
little while, and to work a bit on the main plot.
Accordingly, she sets her pen to the sheet of
paper labeled main routine
Here is the main plot of the Gunfight at the Old
West Saloon There is a Western Town called
Sweaty Post. Sweaty Post has two saloons, one
sheriff, and five troublemakers. There is a male
villain named Maurice. Maurice has a black hat
and a red moustache. Maurice prefers Jack Daniels
whiskey. Mary is a female human. She is blonde.
She prefers her whiskey straight. In our story,
Maurice starts out by drinking whiskey. He then
lets everyone know how drunk he is, and then ties
up a woman named Mary.
23
Here is the main plot of Gunfight at the Old West
Saloon In the novel Gunfight There is a town
called sweatyPost sweatyPost has two saloons
sweatyPost has one sheriff sweatyPost has
five troublemakers   There is a new villain
identified as villainOne villainOne name is
Maurice villainOne has a black hat
villainOne has a red moustache villainOne
prefers Jack Daniels whiskey   There is a new
Human identified as damselOne damselOne is
Mary damselOne is female damselOne is blonde
damselOne prefers her whiskey straight
  villainOne drinks some whiskey villainOne
tells us how drunk he is villainOne ties up a
damsel named Mary
24
public class Gunfight public static void
main(String arguments) WesternTown
sweatyPost new WesternTown() sweatyPost.saloo
ns 2 sweatyPost.sheriffs 1
sweatyPost.troublemakers 5   Villain
villainOne new Villain() villainOne.name
Maurice villainOne.hatColor "black"
villainOne.moustacheColor "red"
villainOne.whiskeyPreference "Jack Daniels"
  Human damselOne new Human()
damselOne.name "Mary" damselOne.sex
"female" damselOne.hairColor "blonde"
damselOne.whiskeyPreference "Straight"
  villainOne.drinkWhiskey() System.out.print
ln ("I had drink number "villainOne.howDrunkAmI()
", said " villainOne.whatIsYourName() "
the villain.") villainOne.tieUpDamsel(damselOne
)
25
THE END RESULT !
c\eunicejavac Gunfight.java c\eunicejava
Gunfight I had drink number 1, said Maurice the
villain. Oh my gosh! Maurice, the villain with
red moustache, has tied up Mary, the pretty
damsel with blonde hair! c\eunice
26
Thats her book ? (You may ask!)
  • So whats so special about Eunices way of
    writing? Why OOP? Why JAVA?
  • It is not what is happening in the main routine,
    but rather what happens behind the scenes.
  • While some of the object traits were specified at
    the time of creation, most of their traits were
    specified back in the binders, or classes.
  • This allowed Eunice to say a good deal while only
    saying a little bit in her main routine.
  • The main routine turns out to be nothing more
    than a collection of references to objects, which
    in turn are references to classes.
  • Any change Eunice wants to make to the setting,
    character traits and development, she can do it
    easily by making a change in one place.
  • Eunice has a keen idea for the future of
    Westerns. She sees interactivity. And if, instead
    of being bound in pages, her characters live in
    objects and classes, Eunice is free to create a
    Virtual Sweaty Post. Readers (on her website)
    could be prompted for their own actions and her
    characters could respond in a variety of ways,
    according to what was written in their classes.
    Using Java, Eunice could finally bring the Old
    West back to life.

27
And who is this Editor ?
You've seen many references to Eunice's editor in
New York. Who is this fellow? Well, he knows OOP
and the world of Westerns in and out. That is why
her editor takes all of Eunice's scripts, reads
them, and then returns them with all the errors
that she has made (remember, we said that he was
a bit persnickety). On the computer, this is
called a compiler. All Java programs must be
compiled before they can be run. The compiler
will patiently (and repeatedly) tell you
everything that you did wrong. And then you get
to go back and do it again. Hey, that's the
literary life.
28
Acknowlegement This presentation is based on,
,a tutorial on
the Web by Johannes "Jos" Dianovich
Claerbout. Jos wrote his tutorial in 1997. In
1999, at the age of 25, he suffered a sudden and
fatal cardiac arrest. Jos was an athlete. He
enjoyed a perfect health. Access his tutorial
and celebrate his life at http//sep.stanford.edu
/sep/jon/family/jos/ Pierre Dagher Montreal,
Quebec Canada
Write a Comment
User Comments (0)
About PowerShow.com