Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lecture 8 b Data Structures b Abstraction b The “Structures” package b Preconditions and postconditions b Interfaces b Polymorphism b Vector class b.

Similar presentations


Presentation on theme: "1 Lecture 8 b Data Structures b Abstraction b The “Structures” package b Preconditions and postconditions b Interfaces b Polymorphism b Vector class b."— Presentation transcript:

1 1 Lecture 8 b Data Structures b Abstraction b The “Structures” package b Preconditions and postconditions b Interfaces b Polymorphism b Vector class b What is a list?

2 2 Abstraction b Data structures should be abstractions: details are hiddendetails are hidden interface separated from implementationinterface separated from implementation b Benefits: manage complexitymanage complexity reusabilityreusability

3 3 Abstract Data Types b ADT: organized collection of informationorganized collection of information operations for managing informationoperations for managing information b The set of operations define the interface to the ADT b As long as the ADT accurately fulfills the promises of the interface, it doesn't really matter how the ADT is implemented b Objects are a perfect programming mechanism to create ADTs because their internal details are encapsulated

4 4 Examples of Structures: b Rational: manipulate fractions (LL p196, B p8) b Die, PairOfDice, Card, DeckOfCards…: you know! (LL) b Wordlist: A list of words (I.e., strings) (B p11) b BankAccount: manage account number and value (B p12) b Association: generalization of BankAccount--key/value pair (B 16) b Store: a bunch of things?… (B 18) int size()int size() boolean isEmpty()boolean isEmpty() void clear()void clear() b Collection: ? (B19) b Vector: a list, similar to array (part of java.util) (LL p302, B p36) b Matrix: manipulate 2-D arrays as matrices (B p43)

5 5 The Collection Interface public interface Collection extends Store { public boolean contains(Object value); // pre: value is non-null // post: returns true iff the collection contains the value public void add(Object value); // pre: value is non-null // post: the value is added to the collection // the replacement policy is not specified. public Object remove(Object value); // pre: value is non-null // post: removes an object "equal" to value within collection. public Iterator elements(); // post: returns an iterator for traversing collection }

6 6 Pre- and Postconditions b Precondition: the conditions under which a method may be called and expected to produce correct results b Postcondition: the state of the program once the method has completed, provided the precondition was met b Details - see B. chapter 2

7 7 Interfaces b A Java interface is a collection of abstract methods and constants b An abstract method is a method header without a method body  An abstract method can be declared using the modifier abstract, but because all methods in an interface are abstract, it is usually left off b An interface is used to formally define a set of methods that a class will implement

8 8 Interfaces public interface Doable { public void doThis(); public int doThat(); public void doThis2 (float value, char ch); public boolean doTheOther (int num); } interface is a reserved word None of the methods in an interface are given a definition (body) A semicolon immediately follows each method header

9 9 Interfaces b An interface cannot be instantiated b Methods in an interface have public visibility by default b A class formally implements an interface by stating so in the class headerstating so in the class header providing implementations for each abstract method in the interfaceproviding implementations for each abstract method in the interface b If a class asserts that it implements an interface, it must define all methods in the interface or the compiler will produce errors.

10 10 Interfaces public class CanDo implements Doable { public void doThis () { // whatever } public void doThat () { // whatever } // etc. } implements is a reserved word Each method listed in Doable is given a definition

11 11 Interfaces b A class that implements an interface can implement other methods as well b See Speaker.java (LL p236) Speaker.java b See Philosopher.java (LL p237) Philosopher.java b See Dog.java (LL p238) Dog.java b A class can implement multiple interfaces b The interfaces are listed in the implements clause, separated by commas b The class must implement all methods in all interfaces listed in the header

12 12 Polymorphism via Interfaces b An interface name can be used as the type of an object reference variable Doable obj;  The obj reference can be used to point to any object of any class that implements the Doable interface  The version of doThis that the following line invokes depends on the type of object that obj is referring to: obj.doThis();

13 13 Polymorphism via Interfaces b That reference is polymorphic, which can be defined as "having many forms"  That line of code might execute different methods at different times if the object that obj points to changes b See Talking.java (LL p240) Talking.java b Note that polymorphic references must be resolved at run time; this is called dynamic binding b Careful use of polymorphic references can lead to elegant, robust software designs

14 14 Interfaces b The Java standard class library contains many interfaces that are helpful in certain situations  The Comparable interface contains an abstract method called compareTo, which is used to compare two objects  The String class implements Comparable which gives us the ability to put strings in alphabetical order  The Iterator interface contains methods that allow the user to move through a collection of objects easily

15 15 The Vector Class  An object of class Vector is similar to an array in that it stores multiple values b However, a vector stores objectsstores objects does not have the indexing syntax that arrays havedoes not have the indexing syntax that arrays have has methods that can be used to manipulate elementshas methods that can be used to manipulate elements size of vector can change (expand) to fit more elementssize of vector can change (expand) to fit more elements  The Vector class is part of the java.util package b An alternative (equivalent?) implementation is given in Structures package  See Beatles.java (LL p304) Beatles.java Beatles.java

16 16 The Vector Class  The Vector class is implemented using an array b Whenever new space is required, a new, larger array is created, and the values are copied from the original to the new array b To insert an element, existing elements are first copied, one by one, to another position in the array  An implementation of Vector and other structures: www.cs.williams.edu/~bailey/JavaStructures/source/index.html www.cs.williams.edu/~bailey/JavaStructures/source/index.html


Download ppt "1 Lecture 8 b Data Structures b Abstraction b The “Structures” package b Preconditions and postconditions b Interfaces b Polymorphism b Vector class b."

Similar presentations


Ads by Google