Chapter 3 Data types and Abstraction
description
Transcript of Chapter 3 Data types and Abstraction
U n
i v
e r
s i t
y
o f
H
a i l
1ICS 202 2011 spring Data Structures and Algorithms
U n
i v
e r
s i t
y
o f
H
a i l
2
Outline
1. Abstract Data Types
2. Design Patterns
• Class Hierarchy
• Java Objects and the Comparable Interface
• Wrappers for the Primitive Types
• Containers
• Visitors
• Enumerations
• Searchable Containers
• Associations
U n
i v
e r
s i t
y
o f
H
a i l
Lec 1
U n
i v
e r
s i t
y
o f
H
a i l
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
U n
i v
e r
s i t
y
o f
H
a i l
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 don’t need to know how ints
are represented or how the operations are implemented to be able to
use them).
U n
i v
e r
s i t
y
o f
H
a i l
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).
U n
i v
e r
s i t
y
o f
H
a i l
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.
U n
i v
e r
s i t
y
o f
H
a i l
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
U n
i v
e r
s i t
y
o f
H
a i l
9
2. Design Patterns: Class hierarchy
• A Java interface comprises a set of method declaration.
• An interface doesn’t 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.
• It’s 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).
U n
i v
e r
s i t
y
o f
H
a i l
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)
U n
i v
e r
s i t
y
o f
H
a i l
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.
U n
i v
e r
s i t
y
o f
H
a i l
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().
U n
i v
e r
s i t
y
o f
H
a i l
13
2. Design Patterns: Java Objects and the Comparable Interface
• Java objects don’t 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; (<0) if obj1<obj2; (>0) if obj1>obj2
}
U n
i v
e r
s i t
y
o f
H
a i l
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 can’t be
overridden).
• All the methods defined in AbstractObject except the method equals call
the compare method.
U n
i v
e r
s i t
y
o f
H
a i l
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) < 0; }
public final boolean isLE (Comparable object)
{ return compare (object) <= 0; }
public final boolean isGT (Comparable object)
{ return compare (object) > 0; }
public final boolean isGE (Comparable object)
{ return compare (object) >= 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;
}
// ...
}
U n
i v
e r
s i t
y
o f
H
a i l
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 < obj2
}
U n
i v
e r
s i t
y
o f
H
a i l
Lec 2
U n
i v
e r
s i t
y
o f
H
a i l
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 don’t implement the Comparable interface.
U n
i v
e r
s i t
y
o f
H
a i l
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;
}
// ...
}
U n
i v
e r
s i t
y
o f
H
a i l
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 < 0) return -1; else if (diff > 0) return +1; else return 0; } }
U n
i v
e r
s i t
y
o f
H
a i l
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 < arg.value) return -1; else if (value > arg.value) return +1; else return 0; } // ... }
U n
i v
e r
s i t
y
o f
H
a i l
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);
}
// ...
}
U n
i v
e r
s i t
y
o f
H
a i l
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
}
U n
i v
e r
s i t
y
o f
H
a i l
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 } // ... }
U n
i v
e r
s i t
y
o f
H
a i l
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
}
U n
i v
e r
s i t
y
o f
H
a i l
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 NoSuchElementException ; // returns the pointer of the next element of the container}
U n
i v
e r
s i t
y
o f
H
a i l
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
}
U n
i v
e r
s i t
y
o f
H
a i l
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.
U n
i v
e r
s i t
y
o f
H
a i l
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; }
// ...
}
U n
i v
e r
s i t
y
o f
H
a i l
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 + "}";
}
// ...
}
U n
i v
e r
s i t
y
o f
H
a i l
END