Session 17 PowerPoint PPT Presentation

presentation player overlay
1 / 19
About This Presentation
Transcript and Presenter's Notes

Title: Session 17


1
Session 17
  • CountedAccumulator
  • (Introducing Inheritance)

2
Test 1
  • Monday, March 2nd
  • Chapters 1-5 in the book
  • Test format closed book, closed notes

3
Study Hints
  • 1. Go through each chapter and make a list of
    the vocabulary terms that we have discussed.
    Could you explain what each means and how they
    relate to each other?
  • 2. Review the slides I used during class and
    consider the points I have emphasized either
    directly on the slides or in addition to the
    slides.

4
Study Hints
  • 3. Review the variants of the programs we have
    worked with (Accumulator, DOME, etc). I will
    provide you with any code I expect you to
    use/modify, but you dont want to waste test time
    reviewing how the code actually works.
  • 4. Read through the questions at the end of
    the chapters. Play the "second guess the
    instructor game. That is, think about what you
    reviewed in the previous steps and think about
    how well each question would fit into these
    topics. Some of my questions will come from
    these questions. Others will be modifications to
    these questions based more on what I have chosen
    to emphasize.

5
Activity
  • We need a new kind of object, an Accumulator that
    counts how many operations it executes. Lets
    call this class CountedAccumulator.
  • Write the code for the CountedAccumulator class.
  • It responds to all the same messages as a regular
    Accumulator and also responds to an
    operationsExecuted() message, by returning its
    count.

6
Adding Behavior to a Class
  • Any time that we need to add behavior to a class
    we have at least three options
  • Add code to the class itself, keeping the
    original class.
  • Copy all the old code into a new class and add
    code to this new class.
  • Create a subclass that extends the original
    class' behavior.

7
Pros and Cons
  • Add code to the class itself, keeping the
    original class.
  • Pros Quick. Convenient. Simple.
  • Cons May change the behavior of the class.
    Thus, it isnt always an option.

8
Pros and Cons
  • Copy all the old code into a new class and add
    code to this new class.
  • Pros Quick. Convenient. Simple.
  • Cons Duplicated code. Error trap! Error trap!

9
Pros and Cons
  • Create a subclass that extends the original
    class' behavior.
  • Pros Doesnt break existing code. Virtually
    eliminates duplicate code. Provides the most
    flexibility.
  • Cons Slightly more time consuming.

10
Developing an Extended Class
  • There are typically four steps in developing an
    extended class.
  • declare the class
  • declare the new data
  • create the constructors  
  • adjust the methods

11
Developing an Extended Class
  • declare the class
  • public class CountedAccumulator extends
    Accumulator

12
Developing an Extended Class
  • declare the new data
  • private int numberOfOperations

13
Developing an Extended Class
  • create the constructor        
  • public CountedAccumulator ()
  • super()
  • numberOfOperations0

14
A solution
  • Technically, at this point we can compile this
    and use CountedAccumulator (although at this
    point it still behaves like an Accumulator).
  • We need to set up the AddingFrame so that it
    works with a Counted Accumulator rather than a
    regular Accumulator.  We do this in the AdderApp
    class for simplicity.
  • Accumulator a new CountedAccumulator()
  • AddingFrame f new AddingFrame(a)

15
Developing an Extended Class
  •  adjust the methods
  • Leave inherited methods alone
  • Modify/Override inherited methods
  • Add completely new methods

16
Developing an Extended Class
  •   Leave inherited methods alone
  • clear() and prepareForNextNumber() are both
    inherited from Accumulator and there is no need
    to change them.

17
Developing an Extended Class
  • Modify/Override inherited methods
  • plus() and minus() are inherited, but they don't
    do what we want them to. 
  • We can make them do more without completely
    replacing the code however.
  • public void plus()
  • super.plus()
  • numberOfOperations

18
Developing an Extended Class
  • Add completely new methods
  • We need an accessor method for numberOfOperations
  • public void operationsExecuted()
  •         return numberOfOperations

19
A Solution
  • After putting it all together we develop a
    version of CountedAccumulator
  • CODE 04_CountedAccumulator.java
Write a Comment
User Comments (0)
About PowerShow.com