Test 2 Review - PowerPoint PPT Presentation

About This Presentation
Title:

Test 2 Review

Description:

... we will want to trap and respond to any mouse action, anywhere within the frame. ... Parent class for numeric wrapper classes: Integer, Long, Double, etc. ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 37
Provided by: markf91
Learn more at: http://www.cs.uni.edu
Category:
Tags: cast | of | parent | review | test | the | trap

less

Transcript and Presenter's Notes

Title: Test 2 Review


1
Session 25
  • Test 2 Review

2
Test 2 Details
  • Tuesday, November 22 in class
  • (Anybody planning on taking it on Friday?)
  • Closed book
  • Closed notes, except for one 8.5 x 11 page of
    notes (front and back is okay)
  • Test is about half coding and half short essay
    questions (similar to Test 1)

3
Test 2 Material
  • Chapter 7 -- Pinball Game
  • Chapter 8 -- Understanding Inheritance
  • Chapter 10 Mechanisms for Software Reuse
  • Chapter 11 Implications of Inheritance
  • Chapter 12 Polymorphism
  • Slides and Handouts from Lecture

4
General Study Hints
  • 1. Go through each chapter and making 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.

5
Study Hints
  • 3. Review the programs we have worked with
    (Pinball Game Versions 1-3, Set example using
    composition, Projectile, Pinball Pallet exercise
    solution, my Tic Tac Toe solution for HW5, 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.

6
Chapter 7 Pinball Game
  • Mouse Events
  • MouseListener interface
  • MouseAdapter
  • Threads
  • main thread listens for events and paints the
    window
  • each ball controlled by an independent thread
  • Vectors -- use of a collector class
  • Exceptions used in PinBallThread
  • PinBallTarget Interface used to achieve
    polymorphism

7
Handling Mouse Events
  • More generally, though, we will want to trap and
    respond to any mouse action, anywhere within the
    frame.
  • Any listener that wishes to monitor mouse
    activity must implement the MouseListener
    interface
  • public interface MouseListener
  • public void mouseClicked ( MouseEvent e )
  • public void mouseEntered ( MouseEvent e )
  • public void mouseExited ( MouseEvent e )
  • public void mousePressed ( MouseEvent e )
  • public void mouseReleased( MouseEvent e )
  • The MouseListener interface is part of the
    package java.awt.event. It specifies what an
    object must do to be a mouse listener within the
    Java Event Model.

8
Mouse Events in the Pin Ball Game
  • In the PinBallGame class, we have the following
    class relationship
  • MouseListener
  • implements
  • MouseAdapter
  • extends
  • MouseKeeper

9
Using Vector
  • import java.util.Vector
  • private Vector balls // holds only objects
  • balls new Vector( ) // create a Vector
  • Using the Vector
  • balls.addElement(newBall) // add element
  • balls.size( ) // return elements
  • // Retrieving an element requires a cast
  • PinBall aBall (PinBall) balls.elementAt (i)

10
Threads of Execution
  • What? How? Why?
  • The Thread class provides methods to start, run,
    sleep, and stop an independent path of
    computation, among other things. The start()
    method manages the overhead of threads for us we
    can simply watch them go! (This is similar to the
    benefits we get from using Frames...)
  • The pinball game separates these responsibilities
    into different objects
  • painting the frame
  • controlling the movement/location of balls

11
More on Threads
  • We can think of separate threads as separate
    programs running concurrently.
  • They dont literally run at the same time (unless
    you have a machine with more than one CPU).
    Instead, one thread gets the CPU for a while,
    then it gets put on hold while another thread
    gets the CPU, and so on.
  • When separate threads are running, sometimes we
    need to worry about two threads taking actions
    that conflict with one another. We can use the
    keyword synchronized to have the JVM help
    maintain order.

12
A Problem Caused bySeparate Threads of Control
13
More on Threads
  • Example The second version of the pin ball game
    keeps track of the score the user earns for
    hitting targets in the field of play. It keeps
    track of the score in an instance variable named
    score
  • private int score 0
  • When a pin ball strikes a target, the target
    tells the pin ball game to add its point total to
    the instance variable by sending an addScore
    message
  • public void addScore( int value )
  • score score value
  • scoreLabel.setText( "score " score )

14
A Problem Caused bySeparate Threads of Control
15
The solution
  • synchronized public void addScore( int value )
  • score score value
  • scoreLabel.setText( "score " score )
  • The keyword synchronized is used to ask Java to
    guarantee that only one thread at a time can be
    executing the addScore() method.

16
PinBall Version 2
  • Adds targets for the PinBalls to bounce off of
    and score on
  • Types of targets
  • Spring
  • Wall
  • Hole
  • ScorePad
  • What do all targets have in common?

17
PinBallTarget Interface
  • interface PinBallTarget
  • public boolean intersects (Ball aBall)
  • public void moveTo (int x, int y)
  • public void paint (Graphics g)
  • public void hitBy (Ball aBall)
  • Why use an interface?
  • we want to process targets uniformly, e.g., check
    if a ball hit it
  • the interface makes them the same type for
    storage in a Vector

18
Hole target
  • structurally similar to a ball
  • round like a ball
  • has a location on the frame like a ball
  • behavioral
  • it must adhere to the interface
  • class Hole extends Ball implements PinBallTarget
  • Inherits moveTo and paint, but supplies
    intersects and hitBy

19
Chapter 8 -- Understanding Inheritance
  • Inheritance
  • Purpose
  • Substitutability
  • Relationship to inheritance
  • Advantages
  • Types of Inheritance
  • Specialization
  • Specification (and abstract classes)
  • Extension
  • Combination
  • Limitation
  • Construction

20
Inheritance
  • What?
  • A mechanism for reusing code in an existing
    class.
  • A mechanism for organizing kinds of objects that
    have the same implementation.
  • How?
  • Create a class that extends another class.
  • Why?
  • Who wants to rewrite code??
  • Reuse provides
  • reliability through continual testing
  • shorter development time
  • ability to build frameworks (dont call us...)
  • You can quickly build an application for
    demonstration purposes.

21
Inheritance
  • In one sense, a subclass is an expansion of its
    superclass.
  • a subclass can add instance variables and
    methods
  • In another sense, a subclass is a contraction of
    its superclass.
  • a subclass defines a subset of instances of its
    superclass
  • In Java, all classes are subclasses, whether we
    say so or not. By default, any class that does
    not have an extends clause extends the class
    Object.
  • Consider our Ball hierarchy Ball, MovableBall,
    BoundedBall

22
Inheritance and Substitutability
  • An object X is substitutable for an object Y if
  • we can use X any place we use Y and
  • the client code not know the difference.
  • An example in practice, from the pinball games
  • The target vector holds any Object. Thats how
    Java Vectors work.
  • We put Springs, Walls, Holes, ..., into the
    vector.
  • When we retrieve objects from the vector, we
    treat them as PinBallTargets.

23
Substitutability
  • The common feature in these cases and the key
    to substitutability is that the objects share a
    common interface.
  • They respond to the same messages.
  • Inheritance and interfaces are mechanisms for
    ensuring the common interface.

24
Substitutability
  • So, why write our programs so that they use
    substitutable objects?
  • extendibility
  • flexibility
  • frameworks that implement a programs control
    while allowing programmers to add new objects to
    the program later
  • Of course, we can achieve these benefits without
    the use of inheritance and interfaces. But the
    compiler wouldnt be able to help us enforce them!

25
Types of Inheritance
  • Specialization
  • Essentially no new methods in the subclass.
  • Most subclass methods override inherited
    methods.
  • example our BoundedBall class
  • common in frameworks

26
Types of Inheritance
  • Specification
  • Superclass provides responsibility but no
    behavior.
  • Implement an interface or extend an abstract
    class.
  • example our event listeners
  • example pinball targets
  • private class MouseKeeper extends MouseAdapter
  • private PinBallTarget element
  • public void mousePressed ( MouseEvent e ) ...
  • public void mouseReleased( MouseEvent e ) ...

27
Types of Inheritance
  • Specification
  • Superclass provides responsibility but no
    behavior.
  • Implement an interface or extend an abstract
    class.
  • example our event listeners
  • example pinball targets
  • private class MouseKeeper extends MouseAdapter
  • private PinBallTarget element
  • public void mousePressed ( MouseEvent e ) ...
  • public void mouseReleased( MouseEvent e ) ...

28
Number - Abstract Class Example
  • Parent class for numeric wrapper classes
    Integer, Long, Double, etc.
  • Subclasses must override abstract methods
  • public abstract class Number
  • public abstract int intValue()
  • public abstract long longValue()
  • public abstract float floatValue()
  • public abstract double doubleValue()
  • public byte byteValue() return (byte)
    intValue
  • public short shortValue()
  • return (short) intValue()
  • // end Number

29
Types of Inheritance
  • Extension
  • Subclass uses most or all inherited methods
    as-is.
  • Subclass adds new behavior and methods.
  • example our MovableBall class

30
Types of Inheritance
  • Combination
  • A class inherits from two or more classes. This
    is called multiple inheritance.
  • Some OOP languages provide it (C). Some
    dont (Java, Smalltalk).
  • Java does support combination through
    interfaces.
  • example Budds Hole class
  • class Hole extends Ball implements PinBallTarget
  • public Hole( int x, int y ) ...
  • public boolean intersects( Ball aBall ) ...
  • public void hitBy ( Ball aBall ) ...

31
Other Types of Inheritance
  • Limitation
  • The subclass primarily has methods that
    override inherited methods.
  • to restrict a behavior (example Square extends
    Rectangle)
  • to remove a behavior (example Set extends
    Vector)
  • Limitation violates the principle of
    substitutability.

32
Other Types of Inheritance
  • Construction
  • The subclass reuses a class because it provides
    needed functionality...
  • ... but it is not necessarily true that an
    instance of the subclass is an instance of the
    superclass.
  • example Javas Stack class (ouch!)
  • Construction may violate the principle of
    substitutability.
  • JUST DONT DO IT.

33
Chapter 10 Mechanisms for Software Reuse
  • ProjectileWorld Framework
  • Java I/O Example of Combining Inheritance and
    Composition
  • Decorator Pattern DeceleratingBall example
  • Novel Forms of Software Reuse
  • Dynamic Composition
  • Inheritance of Inner Classes
  • as in listener classes
  • Unnamed Classes difficult to read, so avoid
    using

34
Chapter 11 Implications of Inheritance
  • Polymorphism
  • Polymorphic Variable
  • Implications of Inheritance/Polymorphism
  • objects reside in the heap
  • reference semantics is used for assignment and
    parameter passing (cloneable interface)
  • equality is identity, i.e., reference comparison
  • automatic garbage collection
  • Memory Layout

35
Chapter 12 Polymorphism
  • Varieties of Polymorphism
  • Pure Polymorphism
  • Overloading / Ad hoc Polymorphism
  • Overloading from Separate Classes
  • Parametric Overloading
  • Overriding
  • Generic
  • Efficiency and Polymorphism

36
Generic
  • Many languages are statically-typed, that is,
    they require us to declare the type of a variable
    in our code. Ada generics and C templates are
    constructs that provide polymorphism of this
    sort, in a statically-typed way. Java 5.0 (aka
    Java 1.5) supports generic classes, too!
  • template ltclass Workergt
  • public class Decorator
  • Worker myInstanceVariable
  • public Worker getValue() return
    myInstanceVariable
  • ...
  • This allows me to create decorators for objects
    of any type
  • Decoratorltintgt
  • DecoratorltBallgt
  • This mechanism enables programmers to create
    nearly-pure polymorphism by declaring the kind of
    object that can be "wrapped". The template gives
    enough information that the compiler can verify
    that the polymorphic behavior will exist at
    run-time.
Write a Comment
User Comments (0)
About PowerShow.com