Ch.3 Classes - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Ch.3 Classes

Description:

... the beneficiary of a concept called Abstraction (can focus on WHAT, not HOW) ... Abstraction free your mind from the irrelevant and work on the relevant! Ex. ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 22
Provided by: DavidW2
Category:
Tags: classes

less

Transcript and Presenter's Notes

Title: Ch.3 Classes


1
Ch.3 Classes Stepwise Refinement
  • STEP 1 Define a new class of robot (see next
    slide)
  • When designing a new class (whether thats
    robots, cars, bank accounts, etc.), the first
    question we are asking ourselves is What can I
    steal? !! In other words, one big benefit of
    OOD is the concept of code-reuse. There is no
    need to reinvent the wheel. (by the way, that
    doesnt mean to copy your friends lab!! ?)

2
  • import kareltherobot.
  • public class MileWalker extends UrRobot
  • public MileWalker (int st, int av, Direction
    dir, int beeps)
  • super(st, av, dir, beeps)
  • public void moveMile( )
  • move() move() move() move()
  • move() move() move() move()

subclass
superclass
constructor
invokes superclass constructor
note no object names preceding methods (why
not?) - lets look at client code to see why
INHERITANCE
3
Inserting MileWalker into the Inheritance
Hierarchy
UrRobot
If you have an object (say, bob) of type
MileWalker, what methods are available to
bob? What if bob were of type UrRobot?
move()
turnLeft()
pickBeeper()
putBeeper()
turnOff()
MileWalker
the is-A relationship a MileWalker is-A UrRobot
moveMile()
4
STEP 2write application(client) to use new
class(server)(a.k.a. a driver)
  • import kareltherobot.
  • public class MileWalkerDriver implements
    Directions
  • public static void main(String args)
  • MileWalker bob new MileWalker(2, 1, East,
    0)
  • bob.moveMile() // new instruction
  • bob. move() // inherited instruction
  • bob.turnOff() // inherited instruction

5
Misc. Note(dont sweat this its not computer
science)
  • These 4 method invokations may be necessary and
    may be placed first within the main() of the
    driver
  • World.reset()
  • World.readWorld(c\\first.kwld")
  • World.setDelay(50)
  • World.setVisible(true)
  • Alternatively you can place them in a static
    block (no need to understand what a static
    block is) within your driver class

6
STEP 3
  • Put each class in its own file making sure that
    the file name matches the class name Exactly
    (convention class names begin with a capital
    letter, method names begin with a lowercase
    letter if an identifier combines several words,
    use the interCap technique. We will follow
    convention.)
  • Well now demo the whole process in BlueJ

7
Now You Try!
  • we want a BetterTurnerRobot class
  • turnRight, turnAround, stepBackward

8
Now, try something a bit more!
  • Design an HBot class on paper right now (yes, it
    should do the same thing as our non-class version
    in Ch.2)
  • In 10 minutes, well pass the wireless keyboard
    mouse and well build/test the class together
  • In addition, where does it go in the Inheritance
    Hierarchy?
  • Why is putting all the same code into one method
    within a class (encapsulation) better than just
    leaving it in the driver i.e., what do we
    gain?(lets informally discuss before I give you
    the fancy cs language)

9
Benefits of Encapsulation
  • So, we just learned that encapsulation promotes
    Code Reuse. By putting the code into a method, we
    no longer need to write that code again. In
    addition, from the clients point of view, she is
    no longer concerned with how to draw the H
    shes the beneficiary of a concept called
    Abstraction (can focus on WHAT, not HOW).
  • In general, if you find yourself doing a
    cut-and-paste, then there is a better way to do
    things i.e., use procedural abstraction and
    abstract out the common code, putting it into a
    method
  • In that light, how should we now modify our
    current 1-method(if thats how you wrote it)
    HBot?

10
Improving HBot
  • Yep, find the common code and create other
    methods.
  • drawLine() and turnRight() might be what you
    choose you might choose others depending on how
    you see the problem being decomposed (stepwise
    refined)
  • Should they have public or private visibility?
  • That depends on whether we believe a client would
    be calling drawLine() and turnRight() lets
    discuss public/private, then you guess/justify
  • Ill argue, No. Ill argue like this the name of
    the class is HBot so I (the client) am trying
    to have some object draw Hs for me how that
    object gets it done is of no concern of
    mine(abstraction) so I dont need (and
    shouldnt be allowed) to see the other
    helper/auxiliary methods. Therefore, they should
    be private, helper-like methods for the class
    use only.

11
Stepwise Refinement
  • technique for writing modules which are concise,
    correct, easy to read/modify/understand
  • Would a general contractor just start building a
    house or would she break up the task into
    foundation, frame, electrical, plumbing, etc.?
    Makes sense, doesnt it. Explain why from the
    contractors view use our cs terms weve been
    learning.
  • write main task first, breaking up the BIG task
    into smaller tasks (using methods) then take
    each method one at a time and also break it up
    --- continue until each method is compact and
    singular in focus (cohesion)
  • Look back at what we just did do you see this
    re-factoring?

12
Practicing Stepwise Refinement
  • Lets write a class called DiamondPlanter
    together using stepwise refinement. Its like
    Harvester except the field is diamond shaped.
    There are always 4 beepers on a diagonal. Assume
    the robot is facing North to begin, has 16
    beepers, and is standing on the corner where the
    bottom of the diamond is to be.
  • What are we asking ourselves first?
  • Now, using some sort of pseudocode, write the top
    level method(call it, plantDiamond() ). While
    writing it, pretend any helper methods youd like
    to use already exist (abstraction) and work
    already (automagically, if you will). After we
    pseudocode plantDiamond(), well take each helper
    method in turn and repeat this stepwise-refinement
    process until we have cohesion.

13
Debriefing DiamondPlanter
  • So, we wrote DiamondPlanter. More than likely we
    wrote a turnRight() and maybe even a turnAround()
    to help us plant. Anyone want to make any
    comments about that?
  • I dont know about you, but I found it ANNOYING
    to write the same thing again! Lets look at the
    Inheritance Hierarchy and see if we can come up
    with a solution. Lets discuss possible solutions
    before going on

14
Improving overall object design
Discuss what our modifications would be to the
HBot and DiamondPlanter classes in terms of
syntax - in terms of concepts weve been
discussing
UrRobot
BetterTurnerBot
turnRight()
turnAround()
HBot
DiamondPlanter
15
Now YOU try! But be efficient!
design a robot class that would be conducive to
solving the following diagrammed situation (robot
should climb and pick up all beepers always
3/stair) also, different clients need to be able
to climb different numbers of stairs
starts off facing East
When designing, keep in mind everything weve
been discussing.
16
Why create a Class?
  • Why reinvent the wheel? Youre allowed to be lazy
    in my class but you have to be smart to be
    lazy!
  • Code Reuse
  • Abstraction free your mind from the irrelevant
    and work on the relevant!
  • Ex. If Im going to write a system to have a bot
    climb stairs in several buildings, Im going to
    use the StairClimber class so I can call
    climbStair() I can work on a bigger/better/harde
    r problem and free my mind from the irrelevant
    details of taking a step and picking beepers

17
Why use Inheritance?
  • You get stuff for free! You can be lazy! (cs
    term?)
  • Use things without knowing/caring how they work!
    (cs term?)
  • Why reinvent the wheel! (cs term?)
  • Localize changes to one class/method
    (localization)

18
Sample Inheritance QuestionsHow many times does
each Bot below move?
  • public class MysteryBot1 extends UrRobot /
    constructor not shown /public void step1()
    move() public void move() super.move()
    super.move()
  • public class MysteryBot2 extends MysteryBot1
    / constructor not shown /public void
    step1() move() public void move()
    super.move() super.move()

MysteryBot1 john new RobotStuff(10, 10 , North,
0)john.step1() // where is the bot now? john
new MoreRobotStuff(10, 10 , North,
0)john.step1() // where are both bots now?
19
Another Inheritance QuestionGive the state
(location and Direction) of each Bot below?
  • public class ABetterBot extends UrRobot /
    constructor not shown /public void step1()
    move() public void step2() turnLeft()
  • public class AnEvenBetterBot extends ABetterBot
    / constructor not shown /public void
    step1() move()
  • super.step1() step2() public
    void step2() turnLeft() super.step2()

ABetterBot ucla new ABetterBot(10, 10 , North,
0)ucla.step1()AnEvenBetterBot usc new
AnEvenBetterBot(10, 10 , North, 0)usc.step1()
20
A final Inheritance QuestionGive the state
(location and Direction) of each Bot below?
  • public class TestBot extends UrRobot /
    constructor not shown /public void step1()
    move()
  • step2() public void step2()
    turnLeft()
  • public class HarderTestBot extends TestBot /
    constructor not shown /public void step1()
    move()
  • super.step1() public void step2()
    turnLeft() super.step2()

TestBot ucla new TestBot(10, 10 , North,
0)ucla.step1()HarderTestBot usc new
HarderTestBot(10, 10 , North, 0)usc.step1()
21
Is-A questions
  • A is-a Letter
  • Letter is-a Symbol
  • Semicolon is-a PunctuationMark
  • _at_ is-a Symbol
  • PunctuationMark is-a Symbol
  • The classes that are inherited (either directly
    or indirectly) by
  • PunctuationMark
  • A
  • Semicolon
  • Symbol
  • _at_
  • Letter
Write a Comment
User Comments (0)
About PowerShow.com