1. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
From ADTs to Java Source Code
SS 2011
Maritta Heisel
Maritta.Heisel(AT)uni-due.de
University Duisburg-Essen – Faculty of Engineering
Department of Computer Science
Workgroup Software Engineering
1/ 35
2. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Reminder
Every program in Java is a class.
A class defines three components:
Constructor: special method for creating and initializing
objects
Methods: behavior (set of operations)
Attributes: local state (set of values of a certain type)
1/ 35
3. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Constructor
Difference between ADT constructor and constructor used
in Java :
Not all elements can be generated by applying only the
constructor.
Only initial elements can be created (e.g. empty
containers)
Recursive ADT-constructors will be implemented as
(ordinary) methods.
2/ 35
4. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Methods & Attributes
Methods:
We use methods to implement ADT functions in Java
We implement our ADT functions as public methods in
Java
Exception: hidden, as well as auxiliary functions are
implemented as private methods
Attributes:
remain
3/ 35
5. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Assertions
Assertions are used to enforce pre-/postconditions.
Defining an assertion in Java :
assert boolean Expression; or
assert boolean Expression: Expression;
Example: assert this.empty() == false; or
assert this.empty() == false:
"The precondition is not satisfied";
Necessary to use additional command option to enable
assertion output (default is disabled)
Enabling the assertions through adding “ea” to the java
command for the interpreter: java -ea filename
4/ 35
11. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Example Point - Constructor
ADT
create : Real × Real → Point
Java
MyPoint(double xCoordinate, double yCoordinate)
{
this.xCoordinate = xCoordinate;
this.yCoordinate = yCoordinate;
}//end MyPoint
7/ 35
12. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Example Point - Selector get x
ADT
get x : Point → Real
get x(create(x, y)) = x
8/ 35
13. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Example Point - Selector get x
ADT
get x : Point → Real
get x(create(x, y)) = x
Java
public double getxCoordinate()
{
return xCoordinate;
}//end getxCoordinate
8/ 35
14. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Example Point - Predicate
ADT
is origin : Point → Bool
is origin(create(x, y)) = true ⇔ x = 0 ∧ y = 0
9/ 35
15. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Example Point - Predicate
ADT
is origin : Point → Bool
is origin(create(x, y)) = true ⇔ x = 0 ∧ y = 0
Java
public boolean isOrigin()
{
if (xCoordinate == 0.0 && yCoordinate == 0.0)
{
return true;
}
return false;
}//end isOrigin
9/ 35
16. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Example Point - Other Functions
ADT
distance : Point × Point → Real
distance(create(x, y), create(z, w)) = ((x − z)2 + (y − w)2)
10/ 35
17. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Example Point - Other Functions
ADT
distance : Point × Point → Real
distance(create(x, y), create(z, w)) = ((x − z)2 + (y − w)2)
Java
public double distance(MyPoint point)
{
double tempxCoordinate;
double tempyCoordinate;
tempxCoordinate = Math.pow(this.xCoordinate
- point.getxCoordinate(),2);
tempyCoordinate = Math.pow(this.yCoordinate
- point.getyCoordinate(),2);
return Math.sqrt(tempxCoordinate+tempyCoordinate);
}//end distance10/ 35
18. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Other Functions II
Output the result using the toString method:
writing a new output method
public String toString()
{
String string = new String();
string = "(" + Double.toString(xCoordinate)
+", "
+ Double.toString(yCoordinate) + ")";
return string;
}//end toString
11/ 35
19. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Other Functions II
Output the result using the toString method:
writing a new output method
public String toString()
{
String string = new String();
string = "(" + Double.toString(xCoordinate)
+", "
+ Double.toString(yCoordinate) + ")";
return string;
}//end toString
11/ 35
20. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Example Nat - Exercise description
Implement the following functions of the ADT Nat:
Nat (constructor)
succ (constructor); implement it as a procedure. You are
allowed to use the expression + 1.
pred (selector); implement it as a procedure. You are
allowed to use the expression − 1.
add (other function); implement it as a function. You can
only use methods of the class itself.
mult (other function); implement it as a function. You can
only use methods of the class itself.
12/ 35
21. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Example Nat - Implementation I
// variables
private int value;
private final int ZERO = 0;
// constructor functions
MyNat() {
this.value = ZERO;
}//end NAT
public void succ() {//successor
value = value + 1;
}//end succ
13/ 35
22. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Example Nat - Implementation II
// selector function
public void pred() {//predecessor
assert value !=ZERO :
"pred(): precondition not satisfied";
value = value - 1;
}//end pred
14/ 35
23. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Problems with call-by-value and objects
Remember, Java postulates it does only call-by-value.
Unfortunately, this is not the whole truth.
It works for basic types such as int.
With objects, it is a little different:
We need to work with copies of our original objects to
avoid undesired side-effects.
15/ 35
24. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Simple copy method
private MyNat copy(MyNat nat){
MyNat copyOfNat = new MyNat();
copyOfNat.value = nat.getValue();
return copyOfNat;
}
private int getValue(){
return this.value;
}
16/ 35
25. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Example Nat - Implementation cont’d I
public MyNat add(MyNat nat) {
MyNat aux = copy(this);
MyNat aux2 = copy(nat);
MyNat sum = new MyNat();
//add(zero,i)=i
if(aux.value == ZERO) {
return aux2;
}
else{//add(succ(i), j) = succ(add(i, j))
aux.pred();
sum = aux.add(aux2);
sum.succ();
return sum;
}
}//add
17/ 35
26. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Example Nat - Implementation cont’d II
public MyNat mult(MyNat nat) {//multiplication
MyNat aux = copy(this);
MyNat aux2 = copy(nat);
MyNat prod = new MyNat();
//mult(zero, i) = zero
if (aux.value == ZERO) return aux;
else{//mult(succ(i), j) = add(j, mult(i, j))
aux.pred();
prod = aux.mult(aux2);
return prod.add(aux2);
} }//end mult
18/ 35
27. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Implementing Container Types I
We supply you with a class named DatAlg providing the
following functionality:
DatAlg() : constructor:
This method constructs a new strucuture able to handle
elements of generic type (denoted by T).
The default size is set to 10.
DatAlg(int length) : constructor: This method constructs
a new structure of length size able to handle elements of
generic type (denoted by T);
size is a natural number greater or equal to 0.
19/ 35
28. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Implementing Container Types II
public void insertElement(T element, int pos): The
method inserts the element at position pos.
The parameter element is a piece of data of generic type.
The parameter pos is a natural number between 0 and
size-1.
The elements are shifted one position to the right, starting
from the former element at position pos. The size of the
structure is automatically increased by 1 if the number of
elements after insertion exceeds the current size.
public void addElement(T element, int pos) : adds
element at position pos without shifting.
pos is a natural number between 0 and size-1.
20/ 35
29. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Implementing Container Types III
public void removeElement(int pos): This method deletes
the element at the provided position pos.
The parameter pos denotes the position of the element to
be deleted.
pos must be a natural number between 0 and size-1
public void toRemove(T element) : deletes the first
occurence of the provided element, if the element is not
contained, no effect takes place
21/ 35
30. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Implementing Container Types IV
public boolean isEmtpy(): returns true if the
corresponding container is empty, returns false otherwise.
public boolean contained(T element): returns true if the
element is contained in the structure, returns false
otherwise
public T getElement(int pos):
The method returns the element at the provided position
pos.
pos must be a natural number between 0 and size-1.
public void toString() : prints the elements of the current
container.
22/ 35
31. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Example STACK[T] - Constructor mt stack
ADT
mt stack : STACK[T]
23/ 35
32. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Example STACK[T] - Constructor mt stack
ADT
mt stack : STACK[T]
Java
private DatAlg<T> stack;
MyStack() {
stack = new DatAlg<T>();
}// end constructor
23/ 35
33. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Example STACK[T] - Constructor push
ADT
push : T × STACK[T] → STACK[T]
24/ 35
34. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Example STACK[T] - Constructor push
ADT
push : T × STACK[T] → STACK[T]
Java
public void push (T element) {
stack.insertElement(element,0);
assert top() == element :
"push(): postcondition not satisfied";
}//end push
24/ 35
35. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Example STACK[T] - Selector pop
ADT
pop : STACK[T] → STACK[T]
pre(pop(s)) ⇔ empty(s) = false
pop(push(x, s)) = s
25/ 35
36. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Example STACK[T] - Selector pop
ADT
pop : STACK[T] → STACK[T]
pre(pop(s)) ⇔ empty(s) = false
pop(push(x, s)) = s
Java
public void pop() {
assert !empty() :
"pop(): precondition not satisfied";
stack.removeElement(0);
}//end pop
25/ 35
37. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Example STACK[T] - Selector top
ADT
top : STACK[T] → T
pre(top(s)) ⇔ empty(s) = false
top(push(x, s)) = x
26/ 35
38. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Example STACK[T] - Selector top
ADT
top : STACK[T] → T
pre(top(s)) ⇔ empty(s) = false
top(push(x, s)) = x
Java
public T top() {
assert this.empty() == false :
"top(): precondition not satisfied";
return stack.getElement(0);
}//end top
26/ 35
39. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Example STACK[T] - Predicate empty
ADT
empty : STACK[T] → Bool
empty(mt stack) = true
empty(push(x, s)) = false
27/ 35
40. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Example STACK[T] - Predicate empty
ADT
empty : STACK[T] → Bool
empty(mt stack) = true
empty(push(x, s)) = false
Java
public boolean empty()
{
return stack.isEmpty();
}//end top
27/ 35
41. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Displaying the outcome...
...means providing an output method:
public void print() {
System.out.println(‘‘Output of Stack: ’’ +
stack.toString());
}//end print
28/ 35
42. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Instantiating Generic Types
Type: String
Instantiation:
MyStack<String> myStack
= new MyStack<String>();
Calling the method push:
myStack.push("Mouse");
29/ 35
43. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Example of a test class for MyStack I
class TestMyStack {
public static void main( String[] args ) {
MyStack<String> myStack =
new MyStack<String>();
System.out.println("Performing ’push’: ");
myStack.push("Mouse");
myStack.push("Tiger");
myStack.print();
30/ 35
44. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Example of a test class for MyStack II
System.out.println("Performing ’pop’
and showing result: ");
myStack.pop();myStack.print();
System.out.println("Performing another
’push’: ");
myStack.push("Elephant");
myStack.print();
31/ 35
45. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Example of a test class for MyStack III
String top = myStack.top();
System.out.println("What is
the top element?: " + top);
}//end main
} //end class
32/ 35
48. Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
General Procedure for implementing ADTs in Java
1. Create a class and name it after the type of the ADT.
2. Implement the non-recursive ADT-constructor as
constructor method in Java with the same name as the
class.
3. Implement the recursive ADT-constructor functions,
selector functions, predicates, and other functions as
methods. Where applicable add the preconditions and
postconditions through assertions.
4. Implement an output function.
5. Create a second class (test class) in the same directory as
your ADT implementation.
6. Write the main method of the test class:
Instantiate the generic type, if needed.
Provide input possibility , if applicable.
Provide a method call to every method contained in the
ADT implementation.
35/ 35