Chapter 3 Data types and Abstraction - PowerPoint PPT Presentation

Loading...

PPT – Chapter 3 Data types and Abstraction PowerPoint presentation | free to download - id: 6f5754-YmYzN



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Chapter 3 Data types and Abstraction

Description:

ICS 202 2011 spring Data Structures and Algorithms – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 32
Provided by: edus88
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Chapter 3 Data types and Abstraction


1
Chapter 3 Data types and Abstraction
2
Outline
  • Abstract Data Types
  • Design Patterns
  • Class Hierarchy
  • Java Objects and the Comparable Interface
  • Wrappers for the Primitive Types
  • Containers
  • Visitors
  • Enumerations
  • Searchable Containers
  • Associations

3
Lec 1
4
1. Abstract Data Types
  • A variable in a procedural programming language
    is an abstraction.
  • The abstraction comprises a number of
    attributes name, address, value, lifetime,
    scope, type, and size.
  • Each attribute has an associated value (int x
    the attribute name is x and the attribute type
    has value int)
  • Assigning a value to an attribute is called
    binding(covering).
  • The binding can be static or dynamic
  • In Java the type of a variable is determined at
    compiler time static binding however, the value
    of a variable is usually not determined until run
    time dynamic binding

5
1. Abstract Data Types
  • In this chapter we are concerned primarily with
    the type attribute of a variable
  • The type of a variable specifies two sets
  • a set of values, and
  • a set of operations.
  • For int x, we know that x can represent an
    integer in the range -231, 231-1 and that we
    can perform operations on x such as addition,
    subtraction, multiplication, and division.
  • The type int is an abstract data type (we dont
    need to know how ints are represented or how the
    operations are implemented to be able to use
    them).

6
1. Abstract Data Types
  • Object oriented programs need an appropriate
    collection of abstractions, and abstract data
    types to represent the abstracts.
  • The abstract data types requires the
    specification of both a set of values and a set
    of operations on those values.
  • In Java the class construct creates both a set
    of values and an associated(link) set of
    operations(Methods).

7
2. Design Patterns
  • In this section we will show how a set of basic
    abstract data type has been designed as a
    hierarchy of Java classes.
  • This section presents an overview of the class
    hierarchy and lays the ground-work for the
    following chapters.

8
2. Design Patterns Class hierarchy
Concrete Class
Abstract Class
Interface
extends relation between classes and between
interfaces
implements relation between a class and the
interface(s) it implements
9
2. Design Patterns Class hierarchy
  • A Java interface comprises a set of method
    declaration.
  • An interface doesnt supply implementations for
    the methods it declares.
  • In effect, an interface identifies the set of
    operations provided by every class that
    implements the interface.
  • An abstract class in Java is a class that
    defines only part of an implementation.
  • Its not possible to create object instances of
    abstract classes.
  • In Java, an abstract class typically contains
    one or more abstract methods.
  • An abstract method is one for which no
    implementation is given (method with out
    implementation).

10
2. Design Patterns Class hierarchy
  • An abstract class is intended to be used as the
    base class from which other classes are derived.
  • By declaring abstract methods in the base class,
    it is possible to access the implementations
    provided by the derived classes through the
    base-class methods.
  • there for , no need to know how a particular
    object instance is implemented or of which
    derived class it is instance.
  • This design pattern uses the idea of
    polymorphism (having many forms)

11
2. Design Patterns Class hierarchy
  • The main idea is that a Java interface is used
    to define the set of values and the set of
    operations the abstract data type.
  • Then, various different implementations (many
    forms) of the interface can be made.

12
2. Design Patterns Java Objects and the
Comparable Interface
  • All the Java classes, including arrays, are
    derived from the base class called Object.

public class Object public final Class
getClass () // public String toString ()
// public boolean equals (Object
obj) // to test if two objects are equals
public int hashCode () //
  • The equals method is overridden in the Integer
    class as follows if obj1 and obj2 are Integers,
    then obj1.equals(obj2) is true when
    obj1.intValue() is equal to obj2.intValue().

13
2. Design Patterns Java Objects and the
Comparable Interface
  • Java objects dont provide a mean to test
    whether one object is less than or greater
    than another object.
  • The Comparable interface solves this problem.

public interface Comparable boolean
isLT (Comparable object) // less than
boolean isLE (Comparable object) // less than
or equal boolean isGT (Comparable
object) // greater than boolean isGE
(Comparable object) // greater than or equal
boolean isEQ (Comparable object) // equals
boolean isNE (Comparable object) //
not equal int compare (Comparable
object) // obj1.compare(obj2) (0) if
// obj1 obj2 (lt0) if obj1ltobj2
(gt0) if obj1gtobj2
14
2. Design Patterns Java Objects and the
Comparable Interface
  • The abstract class at the top of the class
    hierarchy is called AbstractObject
  • All the other classes in the hierarchy are
    derived from this class.
  • The AbstractObject implements the Comparable
    interface.
  • All the methods defined in AbstractObject are
    final (they cant be overridden).
  • All the methods defined in AbstractObject except
    the method equals call the compare method.

15
2. Design Patterns Java Objects and the
Comparable Interface
public abstract class AbstractObject implements
Comparable public final boolean isLT
(Comparable object) return compare
(object) lt 0 public final boolean
isLE (Comparable object) return
compare (object) lt 0 public final
boolean isGT (Comparable object)
return compare (object) gt 0 public
final boolean isGE (Comparable object)
return compare (object) gt 0 public
final boolean isEQ (Comparable object)
return compare (object) 0 public
final boolean isNE (Comparable object)
return compare (object) ! 0 public
final boolean equals (Object object)
if (object instanceof Comparable)
return isEQ ((Comparable) object)
else return
false // ...
16
2. Design Patterns Java Objects and the
Comparable Interface
public abstract class AbstractObject implements
Comparable protected abstract int
compareTo (Comparable arg) // abstract method
public final int compare (Comparable arg) //
call like obj1.compare(obj2)
if (getClass () arg.getClass ()) //
are obj1 and obj2 instances of

// the same class return
compareTo (arg) // called to do the comparison
else return
getClass().getName().compareTo(
arg.getClass ().getName ()) // comparison
based on the names // of the 2 classes.
Ex. If obj1 is instance of Opus5.StackAsArray and
obj2 is // instance of
Opus5.QueueAsLinked then obj1 lt obj2

17
Lec 2
18
2. Design Patterns Wrappers for the Primitive
Types
  • The primitive types in Java are void, boolean,
    char, short, int long, float, and double.
  • For each primitive type, the Java language
    defines a class that wraps the primitive.
  • The wrapper classes are Void, Boolean, Char,
    Short, Int Long, Float, and Double.
  • The wrapper classes are derived from the Object
    class.
  • They provide an equals method to test for
    equality.
  • The methods isLT, isGT, IsGE, isEQ, isNE are not
    supported by the wrapper classes because they
    dont implement the Comparable interface.

19
2. Design Patterns Wrappers for the Primitive
Types
public class Chr extends AbstractObject //
wrapper class to implement the
//
Comparable interface protected char
value public Chr (char value)
this.value value public
char charValue () return value
protected int compareTo (Comparable
object) Chr arg
(Chr) object return (int)
value - (int) arg.value // ...
20
2. Design Patterns Wrappers for the Primitive
Types
public class Int extends AbstractObject
protected int value public Int (int
value) this.value value
public int intValue () return value
protected int compareTo (Comparable
object) Int arg
(Int) object long diff (long)
value - (long) arg.value if (diff
lt 0) return -1
else if (diff gt 0) return 1
else return 0

21
2. Design Patterns Wrappers for the Primitive
Types
public class Dbl extends AbstractObject
protected double value public Dbl
(double value) this.value value
public double doubleValue ()
return value protected int compareTo
(Comparable object) Dbl
arg (Dbl) object if (value lt
arg.value) return -1
else if (value gt arg.value)
return 1 else return 0
// ...
22
2. Design Patterns Wrappers for the Primitive
Types
public class Str extends AbstractObject
protected String value public Str
(String value) this.value value
public String stringValue ()
return value protected int compareTo
(Comparable object) Str
arg (Str) object return
value.compareTo (arg.value) // ...
23
2. Design Patterns Containers
  • A container is an object that contains within it
    other objects
  • An interface Container is defined to be
    implemented by the various data structure classes

public interface Container extends Comparable
int getCount () // returns the number of
objects in the container boolean isEmpty () //
returns true if the container is empty boolean
isFull () // returns true if the container is
full void purge () // deletes the objects of
the container void accept (Visitor visitor) //
will be discussed later Enumeration
getEnumeration () // will be discussed later
24
2. Design Patterns Containers
public abstract class AbstractContainer
extends AbstractObject implements Container
// base class from which actual container are
derived protected int count // to count
the number of items in the container
public int getCount () // returns the number of
items in the container return count
public boolean isEmpty ()
return getCount () 0
public boolean isFull () return
false // by default the container has an
infinity capacity // ...
25
2. Design Patterns Visitors
public interface Visitor // a visitor is an
object that has two methods visit and
// isDone void
visit (Object object) boolean isDone ()
// to determine whether a visitor has finished
its //
work
26
2. Design Patterns Enumerations
public interface Enumeration // a class
to access one-by-one all of the objects in a
container boolean
hasMoreElements () // returns true if the
container
// has more elements
Object nextElement ( ) throws NoSuchElementExcepti
on // returns the pointer of the next
element of the container
27
2. Design Patterns Searchable Container
public interface SearchableContainer extends
Container boolean isMember (Comparable
object) // to test whether the given
object instance is in the container void
insert (Comparable object) // to put an
object in the container void withdraw
(Comparable obj) // to remove an object
from the container Comparable find
(Comparable object) // to locate an
object in a container and to return its
reference
28
2. Design Patterns Associations
  • An association is an ordered pair of objects.
  • The first element is called the key.
  • The second element is the value associated with
    the given key.
  • Associations are useful for storing information
    in a data base.
  • A data base can be viewed as a container that
    holds key-and-value pairs.

29
2. Design Patterns Associations
public class Association extends AbstractObject
protected Comparable key
protected Object value public
Association (Comparable key, Object value)
this.key key this.value value
public Association (Comparable key) this
(key, null) public Comparable getKey
() return key public
Object getValue () return value
// ...
30
2. Design Patterns Associations
public class Association extends AbstractObject
protected Comparable key
protected Object value protected int
compareTo (Comparable object) //
used to compare associations Association
association (Association) object return
key.compare (association.getKey ( ) )
public String toString ( )
// used to return the textual
representation of the association String result
"Association " key if (value ! null)
result ", " value return result ""
// ...
31
END
About PowerShow.com