Abstract Data Types
26-Jul-11
Abstract Data Types 26-Jul-11 Data types I We type data--classify - - PowerPoint PPT Presentation
Abstract Data Types 26-Jul-11 Data types I We type data--classify it into various categories-- such as int , boolean , String , Applet A data type represents a set of possible values, such as { ... , -2 , -1 , 0 , 1 , 2 , ... }, or { true
26-Jul-11
We type data--classify it into various categories--
A data type represents a set of possible values, such as
By typing our variables, we allow the computer to
2
By typing our variables, we allow the computer to
Some operations only make sense when applied to
Typing simplifies internal representation
A String requires more and different storage than a
A data type is characterized by:
a set of values a data representation, which is common to all these
3
a set of operations, which can be applied uniformly
Java provides eight primitive types:
boolean char, byte, short, int, long float, double
4
Each primitive type has
a set of values a data representation a set of operations
These are “set in stone”—there is nothing the
5
A class is a data type
The possible values of a class are called objects The data representation is a reference (pointer) to a block
6
The structure of this block is defined by the fields (both inherited
The operations on the objects are called methods
Many classes are defined in Java’s packages You can (and must) define your own, as well
An operator typically
Is written with non-alphabetic characters: +, *, ++,
Is written as prefix, infix, or postfix: -x, x+y, x++
7
Has only one or two arguments, or operands
A method (or function) typically
Is written with letters, and its arguments are enclosed in
Has any (predetermined) number of arguments
The differences between methods and operations are only
Many languages (not including Java) let you define new
When you define a new class and its methods, you are, 8 When you define a new class and its methods, you are,
Suppose a language defines the operator @ to mean “times 3
Would you consider this a good operation to have in the language? What does this suggest about defining classes and their methods?
There are many ways you could insert a new node into a list:
9 Is it a good idea to supply all of these? If not, why not?
Human minds are limited—you can’t remember
You probably don’t even remember all the Java operators for integers
What’s the difference between >> and >>> ? What about between << and <<< ?
10
We want our operators (and methods) to be useful and
A list is just a sequence of values—it could be
Inserting as a new first element is efficient for a linked
Accessing the nth element is efficient for an array
11
Inserting in the nth position is efficient for neither
Do we want to make it easy for the user to be
Do we want the user to have to know the
An Abstract Data Type (ADT) is:
a set of values a set of operations, which can be applied uniformly to all
12
To abstract is to leave out information, keeping
What part of a Data Type does an ADT leave out?
An ADT must obviously have some kind of
The user need not know the representation The user should not be allowed to tamper with the
13
Solution: Make all data private
But what if it’s really more convenient for the user
Solution: Use setters and getters
14
Setters and getters should be named by:
Capitalizing the first letter of the variable (first becomes
Prefixing the name with get or set (setFirst) For boolean variables, you can replace get with is (for
15
For boolean variables, you can replace get with is (for
This is more than just a convention—if and when you
Setters and getters allow you to keep control of your
For example, you decide to define a Point in a plane by its x-y
class Point { public int x; public int y; }
16 Later on, as you gradually add methods to this class, you decide
Sorry, you can’t do that—you’ll break too much code that
If you had used setters and getters, you could redefine them to
Every ADT should have a contract (or
Specifies the set of valid values of the ADT Specifies, for each operation of the ADT:
17
Its name Its parameter types Its result type, if any Its observable behavior
Does not specify:
The data representation The algorithms used to implement the operations
A contract is an agreement between two parties; in this
The implementer of the ADT, who is concerned with making
The applications programmer, who just wants to use the ADT
18
The applications programmer, who just wants to use the ADT
It doesn’t matter if you are both of these parties; the
This separation of concerns is essential in any large
For a general API, the implementer should provide as
But for a specific program, the class author should
In Extreme Programming terms, “You ain’t gonna need it!”
19
In fact, XP practice is to remove functionality that isn’t
Your documentation should not expose anything that the
If you design for generality, it’s easy to add
To implement an ADT, you need to choose:
a data representation
must be able to represent all possible values of the ADT should be private
an algorithm for each of the possible operations
20
must be consistent with the chosen representation all auxiliary (helper) operations that are not in the contract should be
Express the contract as an outline class declaration,
public fields
21
headings of constructors and methods (this is what you would describe in javadoc comments)
Express the implementation as a completed class
You can’t write the outline class declaration directly,
Express the contract as an interface Express the implementation as a class that implements
22
Disadvantage: you can’t describe constructors in an
Nevertheless, this is a good technique, and is used
General description of class
Constructor
23
Accessor
Transformer (mutative)
24
25
Notice:
26
There is no way to define a constructor
However, this should be part of the contract!
We need two names: one for the interface, and one for
Usually more than one class will implement an interface
27
Frequently more than one class will implement an
The interface describes the general (more abstract) case Implementations are designed for specific classes
A class is responsible for its own values
It should protect them from careless or malicious users
Ideally, a class should be written to be generally
The goal is to make the class reusable
28
The goal is to make the class reusable The class should not be responsible for anything
In practice, most classes are application-specific Java’s classes are, on the whole, extremely well
They weren’t written specifically for your program Strive to make your classes more like Java’s!
Originally, I left the word static out of
Then I created a Vector of ten dice (Dies)
29
When I printed the Vector, I got:
Why?
These really were ten different Die objects
Hint: How does Java initialize its random number
A Data Type describes values, representations, and
An Abstract Data Type describes values and
30
An ADT should protect its data and keep it valid
All, or nearly all, data should be private Access to data should be via getters and setters
An ADT should provide:
A contract A necessary and sufficient set of operations
31