ADT and Data Structure Example - PowerPoint PPT Presentation

About This Presentation
Title:

ADT and Data Structure Example

Description:

Although the implementation is different, the user gains the benefit of stronger ... Box Widget box1 = new Box Widget (); Box Gadget box2 = new Box Gadget (); 6 ... – PowerPoint PPT presentation

Number of Views:47
Avg rating:3.0/5.0
Slides: 21
Provided by: bobwi9
Learn more at: https://www.cs.umb.edu
Category:

less

Transcript and Presenter's Notes

Title: ADT and Data Structure Example


1
ADT and Data Structure Example
  • Generics / Parameterized Classes
  • Using a Set
  • Implementing a Set with an Array
  • Example SetADT interface
  • Example ArraySet class
  • ArraySet Underlying Data Structure
  • Reading LC 15.3

2
Java Generics
  • In Java 5.0, Sun added a generic feature that had
    been available in other languages such as C
    (where it is called Templates)
  • Although the implementation is different, the
    user gains the benefit of stronger compiler type
    checking and simpler coding style (with a reduced
    number of explicit casts needed)

3
Java Generics
  • The Java implementation was constrained by
    backward compatibility
  • Code compiled using javac 5.0 and generics had to
    work on java 4.2 virtual machines
  • The technique used was type erasure and one
    copy of the parameterized class code is accessed
    with compiler supplied casts
  • Avoids C code bloat where there are multiple
    copies of the parameterized code

4
Parameterized Classes
  • Defining a parameterized class
  • public class GenericltTgt
  • // use T in attribute declarations
  • private T whatIsThis
  • // use T as a methods parameter type
  • public void doThis(T input)
  • // use T as a methods return type
  • public T doThat( )
  • return new T( )

5
Parameterized Classes
  • Instantiating a parameterized class
  • GenericltStringgt g new GenericltStringgt()
  • Use methods with objects of the actual type
  • g.doThis(Hello)
  • String s g.doThat( )
  • No casting of data types should be required
  • Note Error in text on pgs 40-41 - missing ()
  • BoxltWidgetgt box1 new BoxltWidgetgt()
  • BoxltGadgetgt box2 new BoxltGadgetgt()

6
Parameterized Classes
  • Note The letter used inside ltgt is a dummy and
    can be ltTgt like C or ltEgt like Sun
  • I prefer to use T based on C popularity and
    that is also what our textbook uses
  • Only reference types - not primitive types - can
    be used as a generic type
  • GenericltStringgt g . . . // OK
  • Genericltintgt g . . . // Compile error

7
Parameterized Classes
  • Must use a known class - not dummy letters
  • GenericltTgt g new GenericltTgt() // error
  • Unless in a generic class where T is defined
  • public class AnotherGenericClassltTgt
  • GenericltTgt g new GenericltTgt() // OK

8
Parameterized Classes
  • Dont omit an identified lttypegt in new code
  • Generic g new Generic() // legacy code?
  • Compiler will give incompatible type errors
    without an explicit cast (narrowing)
  • String s g.doThat( ) // error
  • String s (String) g.doThat( ) // OK
  • Compiler will give unchecked warnings
  • g.doThis(Hello) // warning

9
Parameterized Classes
  • Cant instantiate arrays of the generic type
    without using a trick
  • T t new T10 // compile error
  • T t (T ) new Object10 // OK
  • Cant instantiate arrays of a parameterized class
    without using a slightly different trick
  • ArrayListltStringgt a
  • (ArrayListltStringgt) new ArrayList10
  • Casting a new Object10 compiles OK, but throws
    an exception at run time

10
Parameterized Classes
  • Static members can not be generic because there
    is only one copy of the code for all of the
    parameterized class objects instantiated with
    possibly different generic types
  • public class BadClassltTgt
  • private static T count // error
  • public static T method() // error

11
Parameterized Classes
  • Dont invoke static methods of parameterized
    classes with a generic type specified
  • BadClassltIntegergt b
  • new BadClassltIntegergt()
  • // the following is an error
  • int n BadClassltIntegergt.method()
  • // the method must be invoked as
  • int n BadClass.method()

12
Parameterized Methods
  • Individual static methods in a non-parameterized
    class can be identified as generic
  • public class Stacks
  • public static ltTgt StackltTgt
  • reverse (StackltTgt in)
  • // use the generic type T as usual
  • StackltTgt out new ArrayStackltTgt()
  • . . .
  • return out

13
Parameterized Methods
  • To invoke a parameterized method you use the
    parameterized types but still omit generic type
    indicator on the method call
  • StackltIntegergt stack
  • new ArrayStackltIntegergt()
  • . . .
  • StackltIntegergt reverse
  • Stacks.reverse(stack)

14
Using a Set
  • First, we need a defined interface for a
    collection to contain our set of objects
  • See SetADTltTgt interface definition (LC pg 437)
  • Next, we need to define the class that will be
    contained in the collection
  • See BingoBall class definition (LC pg 441)
  • Then, we need to define a class with code to
    create/manipulate BingoBall objects
  • See Bingo class definition (LC pg 442)

15
Using a Set
  • Finally, we need a class that implements the
    SetADTltTgt interface to contain our BingoBall
    objects including two things
  • A selection for an underlying data structure
  • Code to implement the SetADT methods using the
    chosen underlying data structure
  • A UML diagram for the Bingo application is shown
    in the next slide

16
UML Diagram for Bingo Application
extends
ltltinterfacegtgt SetADTltTgt
ltltinterfacegtgt IterableltTgt
implements
Bingo
ArraySetltBingoBallgt
uses
main (args String ) void
uses
BingoBall
NOTE More complete than the UML diagram
provided in LC on page 440. See subsequent
slides for difference.
17
Interface SetADTltTgt
  • SetADTltTgt is an interface defining all the
    methods the Bingo class needs to use
  • void add (T element)
  • T removeRandom( )
  • T remove (T element)
  • SetADTltTgt union(SetADTltTgt set)
  • boolean contains(T target)
  • boolean equals(SetADTltTgt set)
  • boolean isEmpty()
  • int size()
  • IteratorltTgt iterator()
  • String toString()

18
Interface SetADTltTgt
  • SetADT includes the iterator method, but the
    authors code doesnt extend Iterable
  • My preference for definition of the SetADT
    interface would be for it to extend Iterable
  • interface SetADTltTgt extends IterableltTgt
  • Then, the new Java 5.0 for-each loop can be used
    on any object of type SetADT and the ArraySet
    class does not need separate specification of
    implements Iterable
  • I will show some benefits of doing this later

19
ArraySetltTgt Class
  • The ArraySet class needs to implement the SetADT
    interface with a generic type
  • class ArraySetltTgt implements SetADTltTgt
  • It uses an underlying array as a data structure
    (due to the use of the word array in its name)
  • It needs to implement code for all the methods
    defined in the SetADT interface
  • In these slides, I will show some code that is
    different from the code in the book so we can
    discuss alternative ways of writing the code

20
ArraySet Underlying Data Structure
private static Random rand new
Random() private static final int
DEFAULT_CAPACITY 100 private int
count private T contents
Value of count is the next index in array to be
used for adding objects
contents0
contents1
contents2
contents3
contents4
contents5
null
null
null
Elements are kept contiguous
Write a Comment
User Comments (0)
About PowerShow.com