Docsity

Abstract Data Types: Immutable Points in Java, Papers of Data Structures and Algorithms

Toggle sidebar
bg1
1
08 1
Abstract Data Types
Problem Sets: PS2 due due Monday, Feburary 2 6
PS3 due Wednesday, March 7
Wellesley College CS230
Lecture 08
Monday, February 26
Handout #16
Note: This lecture includes slides from Lectures
06 & 07 that were not covered in those lectures
08 2
What is an Abstract Data Type (ADT)?
An Abstract Data Type (ADT) is a contract that specifies the
behavior of methods that operate on a data structure without
exposing the representation of the data structure.
In general, there are many different implementations of a given ADT.
In this lecture:
โ€ข We explore ways of specifying and implementing ADTs in Java.
โ€ข We study some sample ADTs:
โ€“ Immutable Points
โ€“ Comparables
โ€“ Iterators
โ€“ Stacks
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Abstract Data Types: Immutable Points in Java and more Papers Data Structures and Algorithms in PDF only on Docsity!

08 1

Abstract Data Types

Problem Sets: PS2 due due Monday, Feburary 26 PS3 due Wednesday, March 7

Wellesley College CS

Lecture 08

Monday, February 26

Handout

Note: This lecture includes slides from Lectures

06 & 07 that were not covered in those lectures

08 2

What is an Abstract Data Type (ADT)?

An Abstract Data Type (ADT) is a contract that specifies the behavior of methods that operate on a data structure without exposing the representation of the data structure. In general, there are many different implementations of a given ADT. In this lecture:

  • We explore ways of specifying and implementing ADTs in Java.
  • We study some sample ADTs:
    • Immutable Points
    • Comparables
    • Iterators
    • Stacks

08 3 Immutable Point Contract // Constructor Method public IPoint (int x, int y); /* Create an immutable point with coordinates x and y */ // Instance Methods public int getX(); // Return the x-coordinate of this point. public int getY(); // Return the y-coordinate of this point public String toString(); // Return string of form (x,y) 08 4 Two Implementations of Immutable Points

public class IPointVars { // Give classes (and constructors) different names

private int x, y; // Represent coords with two int variables.

public IPointVars (int x, int y) {this.x = x; this.y = y;}

public int getX() { return x; }

public int getY() { return y; }

public String toString() { return "(" + getX() + "," + getY() + ")"; }

public class IPointArray {

private int[] ints = new int[2]; // Represent coords with two-slot array.

public IPointArray (int x, int y) {ints[0] = x; ints[1] = y;}

public int getX() { return ints[0]; }

public int getY() { return ints[1]; }

public String toString() { return "(" + getX() + "," + getY() + ")"; }

08 7 An IPoint Abstract Class public abstract class IPoint { public abstract int getX(); public abstract int getY(); public String toString() { return "(" + getX() + "," + getY() + ")"; } } public class IPointVars extends IPoint { โ€ฆ same impl. as before except no toString() method โ€ฆ} public class IPointArray extends IPoint { โ€ฆ same impl. as before except no toString () method โ€ฆ} public static int sumCoords (IPoint p) { return p.getX() + p.getY(); } sumCoords(new IPointVars(1,2)) โ†’ 3 sumCoords(new IPointArray(3,4)) โ†’ 7

Use extends rather than

implements with abstract class

A class must be declared abstract if any

of its methods are declared abstract.

A method without a body

is declared abstract.

An abstract class may have concrete methods; these can use abstract methods. 08 8 Interfaces vs. Abstract Classes Interfaces are used solely to specify a contract (API/ADT)

  • All methods in an interface must be public instance methods (no class

methods or constructor methods)

  • All variables in an interface must be public static final variables with

initializers. E.g.

public static final CLUBS = 0; public static final DIAMONDS = 1; โ€ฆ

  • Interfaces may have subinterfaces:

public interface ColoredIPoint extends IPoint { โ€ฆ }

  • Classes can extend one class but implement any number of interfaces.

public class Frobnitz extends Frob

implements IPoint, Cloneable, Comparable

  • In CS230, we will use interfaces to specify ADTs Abstract Types are used to specify a partially implemented class; they can also be used for an API.
  • Unlike interfaces, abstract classes may include methods with code.
  • Abstract classes may include (non-abstract) static methods.
  • In CS230, we will use abstract classes to specify methods shared among

different implementations of the same interface.

08 9 Interfaces and Abstract Classes in Java Some standard Java Interfaces:

  • Cloneable: public Object clone();
  • Comparable: public int compareTo (T x);
  • java.util.Iterator: public boolean hasNext(); public T next(); public void remove(); (optional)
  • java.util.Iterable: public Iterator iterator();
  • java.util.Collection: โ€ฆ discussed in a later lecture โ€ฆ
  • java.awt.event.ActionListener: โ€ฆ discussed in GUI lectures โ€ฆ Some CS230 Java Interfaces: Stack, Queue, Set, Bag, โ€ฆ Some standard Java Abstract Classes:
  • InputStream
  • java.awt.Component
  • java.awt.AWTEvent 08 10 Cloneable & Comparable Example

public class FancyIPointVars extends IPointVars

implements Cloneable, Comparable {

public FancyIPointVars (int x, int y) { super(x,y); }

public Object clone() { return new FancyIPointVars(getX(), getY()); }

// Compare lexicographically, first by x position, then by y position

public int compareTo (IPoint p) {

if (getX() < p.getX()) {

return -1;

} else if (getX() > p.getX()) {

return 1;

} else {

return getY() โ€“ p.getY();

08 13 Iterator Removal Example

public static void removeShort (Iterator elts) {

while elts.hasNext() {

if (elts.next().length() <= 3) {

elts.remove();

Then removeShort(v); displayIterator(v.iterator()) displays the

following:

aardvark

beetle

elephant

08 14 Vector Filtering Gotcha!

How could we remove short strings from a vector without an iterator?

Public static void removeShort (Vector v) {

08 15 Stack Interface import java.util.; // imports Iterator and Iterable /* Interface to mutable stacks with elements of type T. A stack is a last-in-first-out (LIFO) collection with duplicates. WARNING: This CS230 Stack is an interface, and is different from the class named java.util.Stack. */ public interface Stack extends Iterable { public void push(T elt); // Push elt on top of the stack. public T pop(); // Remove and return top element from stack. // Throw RuntimeException if stack is empty. public T top(); // Return top element of stack without removing it. // Throw RuntimeException if stack is empty. public boolean isEmpty(); // Return true if this stack is empty; else false public int size(); // Return the number of elements in this stack public void clear(); // Remove all elements from this stack public Stack copy(); // Return a shallow copy of this stack. public Iterator iterator(); // Return an iterator that yields this stack's // elements from top to bottom. } 08 16 Stack Examples 1 Stack s = new Stack; s.isEmpty() โ†’ s.size() โ†’ s.top() โ†’ s.push(โ€œBโ€); s.isEmpty() โ†’ s.size() โ†’ s.top() โ†’ s.push(โ€œCโ€); s.isEmpty() โ†’ s.size() โ†’ s.top() โ†’ s.push(โ€œBโ€); s.isEmpty() โ†’ s.size() โ†’ s.top() โ†’ s.push(โ€œAโ€); s.isEmpty() โ†’ s.size() โ†’ s.top() โ†’ s.pop() โ†’ s.isEmpty() โ†’ s.size() โ†’ s.top() โ†’ s.pop() โ†’ s.isEmpty() โ†’ s.size() โ†’ s.top() โ†’ s.pop() โ†’ s.isEmpty() โ†’ s.size() โ†’ s.top() โ†’

08 19