Chapter 3 data types and abstraction
Sponsored Links
This presentation is the property of its rightful owner.
1 / 31

Chapter 3 Data types and Abstraction PowerPoint PPT Presentation


  • 115 Views
  • Uploaded on
  • Presentation posted in: General

Chapter 3 Data types and Abstraction. 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. L ec 1. 1. Abstract Data Types.

Download Presentation

Chapter 3 Data types and Abstraction

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Chapter 3Data types and Abstraction

ICS 202  2011 spring  Data Structures and Algorithms 


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


Lec1


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


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).


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).


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.


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


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).


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)


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.


2. Design Patterns: Java Objects and the Comparable Interface

  • All the Java classes, including arrays, are derived from the base class called Object.

publicclass Object

{

public final Class getClass () ;//

public String toString () ; //

publicboolean equals (Object obj) ; // to test if two objects are equals

publicint 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().


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.

publicinterface 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

}


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.


2. Design Patterns: Java Objects and the Comparable Interface

public abstract class AbstractObject implements Comparable

{

public finalboolean 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

returnfalse;

}

// ...

}


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

elsereturn 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.QueueAsLinkedthen obj1 < obj2

}


Lec 2


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.


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;

}

// ...

}


2. Design Patterns: Wrappers for the Primitive Types

public class Int extends AbstractObject

{

protectedint value;

public Int (int value)

{ this.value = value; }

public int intValue ()

{ return value; }

protectedint compareTo (Comparable object)

{

Int arg = (Int) object;

long diff = (long) value - (long) arg.value;

if (diff < 0)

return -1;

elseif (diff > 0)

return +1;

else

return 0;

}

}


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;

elsereturn 0;

}

// ...

}


2. Design Patterns: Wrappers for the Primitive Types

public class Str extends AbstractObject

{

protected String value;

public Str (String value)

{ this.value = value; }

publicString stringValue ()

{ return value; }

protected int compareTo (Comparable object)

{

Str arg = (Str) object;

return value.compareTo (arg.value);

}

// ...

}


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

}


2. Design Patterns: Containers

public abstractclass 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 () // returnsthe number of items in the container

{

return count;

}

public boolean isEmpty ()

{

return getCount () == 0;

}

publicboolean isFull ()

{

return false; // by default the container has an infinity capacity

}

// ...

}


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

}


2. Design Patterns: Enumerations

public interface Enumeration

{ // a class to access one-by-one all of the objects in a container

boolean hasMoreElements () ; // returnstrue if the container

// has more elements

Object nextElement ( ) throws NoSuchElementException;

// returns the pointer of the next element of the container

}


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

}


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.


2. Design Patterns: Associations

public class Association extends AbstractObject

{

protected Comparable key;

protectedObject 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; }

// ...

}


2. Design Patterns: Associations

public class Association extends AbstractObject

{

protected Comparable key;

protectedObject value;

protectedint 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 + "}";

}

// ...

}


END


  • Login