13 Adt
13 Adt
Sep 1, 2015
Data types I
Data types II
a set of values
a data representation, which is common to all these
values, and
a set of operations, which can be applied uniformly to
all these values
boolean
char, byte, short, int, long
float, double
a set of values
a data representation
a set of operations
Values
Representation Operations
boolean
true, false
Single byte
&&, ||, !
Twos complement
+, -, *, /,
others
+, -, *, /,
others
Classes in Java
An operator typically
There are many ways you could insert a new node into a list:
As the new first element
As the new last element
Before a given node
After a given node
Before a given value
After a given value
Cognitive load
You probably dont even remember all the Java operators for integers
10
Efficiency
a set of values
a set of operations, which can be applied uniformly to all
these values
12
Data Structures
13
14
15
16
Contracts
Its name
Its parameter types
Its result type, if any
Its observable behavior
18
Implementing an ADT
a data representation
/**
* Each value is a die (singular of dice) with n sides,
* numbered 1 to n, with one face showing.
*/
public class Die
Constructor
/**
* Constructs a die with faces numbered 1 thru numberOfSides.
*/
public Die(int numberOfSides)
Accessor
/**
* Returns the result of the previous roll.
*/
int lastRoll()
Transformer (mutative)
/**
* Returns the result of a new roll of the die.
*/
int roll()
22
Implementation, page 1
import java.util.*;
public class Die {
private int numberOfSides;
private static Random random = new Random();
private int face;
public Die(int numberOfSides) {
this.numberOfSides = numberOfSides;
face = roll(); // construct in a valid state!
}
23
Implementation, page 2
int lastRoll() {
return face;
}
int roll() {
face = random.nextInt(numberOfSides) + 1;
return face;
}
} // class Die
24
Responsibilities
25
Why?
Summary
The End
28