SlideShare a Scribd company logo
1 of 48
Download to read offline
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
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
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
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
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
Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Generic Types
Generic Type in Java expressed through: <T>
(T for generic type has become a convention.)
Example: class MyStack<T>
5/ 35
Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Example Point
ADT
Type Point
6/ 35
Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Example Point
ADT
Type Point
Java
6/ 35
Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Example Point
ADT
Type Point
Java
class MyPoint
6/ 35
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
7/ 35
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Example output
33/ 35
Java
Maritta Heisel
Reminder
New stuff
Assertions
Generic Types
Implementing
ADTs
Example Point
Example Nat
Overview of
DatAlg
Example STACK
General
Approach
Violating the contract
34/ 35
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

More Related Content

What's hot

A (too) Short Introduction to Scala
A (too) Short Introduction to ScalaA (too) Short Introduction to Scala
A (too) Short Introduction to ScalaRiccardo Cardin
 
Refactoring - improving the smell of your code
Refactoring - improving the smell of your codeRefactoring - improving the smell of your code
Refactoring - improving the smell of your codevmandrychenko
 
Semmle Codeql
Semmle Codeql Semmle Codeql
Semmle Codeql M. S.
 
Java - Concurrent programming - Thread's advanced concepts
Java - Concurrent programming - Thread's advanced conceptsJava - Concurrent programming - Thread's advanced concepts
Java - Concurrent programming - Thread's advanced conceptsRiccardo Cardin
 
Effective Java - Generics
Effective Java - GenericsEffective Java - Generics
Effective Java - GenericsRoshan Deniyage
 
Java- Concurrent programming - Synchronization (part 1)
Java- Concurrent programming - Synchronization (part 1)Java- Concurrent programming - Synchronization (part 1)
Java- Concurrent programming - Synchronization (part 1)Riccardo Cardin
 
04. constructor & destructor
04. constructor & destructor04. constructor & destructor
04. constructor & destructorHaresh Jaiswal
 
Ekeko Technology Showdown at SoTeSoLa 2012
Ekeko Technology Showdown at SoTeSoLa 2012Ekeko Technology Showdown at SoTeSoLa 2012
Ekeko Technology Showdown at SoTeSoLa 2012Coen De Roover
 
The SOUL Tool Suite for Querying Programs in Symbiosis with Eclipse
The SOUL Tool Suite for Querying Programs in Symbiosis with EclipseThe SOUL Tool Suite for Querying Programs in Symbiosis with Eclipse
The SOUL Tool Suite for Querying Programs in Symbiosis with EclipseCoen De Roover
 
2. Design patterns. part #2
2. Design patterns. part #22. Design patterns. part #2
2. Design patterns. part #2Leonid Maslov
 
Introduction to objective c
Introduction to objective cIntroduction to objective c
Introduction to objective cMayank Jalotra
 
Java Generics
Java GenericsJava Generics
Java Genericsjeslie
 
Detecting aspect-specific code smells using Ekeko for AspectJ
Detecting aspect-specific code smells using Ekeko for AspectJDetecting aspect-specific code smells using Ekeko for AspectJ
Detecting aspect-specific code smells using Ekeko for AspectJCoen De Roover
 
重構—改善既有程式的設計(chapter 9)
重構—改善既有程式的設計(chapter 9)重構—改善既有程式的設計(chapter 9)
重構—改善既有程式的設計(chapter 9)Chris Huang
 

What's hot (19)

A (too) Short Introduction to Scala
A (too) Short Introduction to ScalaA (too) Short Introduction to Scala
A (too) Short Introduction to Scala
 
Java Script Workshop
Java Script WorkshopJava Script Workshop
Java Script Workshop
 
Memory management in c++
Memory management in c++Memory management in c++
Memory management in c++
 
Memory Management In C++
Memory Management In C++Memory Management In C++
Memory Management In C++
 
Refactoring - improving the smell of your code
Refactoring - improving the smell of your codeRefactoring - improving the smell of your code
Refactoring - improving the smell of your code
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
 
Semmle Codeql
Semmle Codeql Semmle Codeql
Semmle Codeql
 
Java - Concurrent programming - Thread's advanced concepts
Java - Concurrent programming - Thread's advanced conceptsJava - Concurrent programming - Thread's advanced concepts
Java - Concurrent programming - Thread's advanced concepts
 
Effective Java - Generics
Effective Java - GenericsEffective Java - Generics
Effective Java - Generics
 
Java- Concurrent programming - Synchronization (part 1)
Java- Concurrent programming - Synchronization (part 1)Java- Concurrent programming - Synchronization (part 1)
Java- Concurrent programming - Synchronization (part 1)
 
04. constructor & destructor
04. constructor & destructor04. constructor & destructor
04. constructor & destructor
 
Ekeko Technology Showdown at SoTeSoLa 2012
Ekeko Technology Showdown at SoTeSoLa 2012Ekeko Technology Showdown at SoTeSoLa 2012
Ekeko Technology Showdown at SoTeSoLa 2012
 
The SOUL Tool Suite for Querying Programs in Symbiosis with Eclipse
The SOUL Tool Suite for Querying Programs in Symbiosis with EclipseThe SOUL Tool Suite for Querying Programs in Symbiosis with Eclipse
The SOUL Tool Suite for Querying Programs in Symbiosis with Eclipse
 
2. Design patterns. part #2
2. Design patterns. part #22. Design patterns. part #2
2. Design patterns. part #2
 
Introduction to objective c
Introduction to objective cIntroduction to objective c
Introduction to objective c
 
강의자료8
강의자료8강의자료8
강의자료8
 
Java Generics
Java GenericsJava Generics
Java Generics
 
Detecting aspect-specific code smells using Ekeko for AspectJ
Detecting aspect-specific code smells using Ekeko for AspectJDetecting aspect-specific code smells using Ekeko for AspectJ
Detecting aspect-specific code smells using Ekeko for AspectJ
 
重構—改善既有程式的設計(chapter 9)
重構—改善既有程式的設計(chapter 9)重構—改善既有程式的設計(chapter 9)
重構—改善既有程式的設計(chapter 9)
 

Viewers also liked

Working with the Media
Working with the MediaWorking with the Media
Working with the Mediarichmattern1
 
M4/12 Smartphones
M4/12 SmartphonesM4/12 Smartphones
M4/12 SmartphonesJamie Hutt
 
Mindstorm nxt
Mindstorm nxtMindstorm nxt
Mindstorm nxt7826501
 
предизборна платформа (1)
предизборна платформа (1)предизборна платформа (1)
предизборна платформа (1)Maria Dencheva
 
2013 11 12_shop professionalisieren
2013 11 12_shop professionalisieren2013 11 12_shop professionalisieren
2013 11 12_shop professionalisierenDaWanda
 

Viewers also liked (7)

Working with the Media
Working with the MediaWorking with the Media
Working with the Media
 
Primeros problemas-con-texto
Primeros problemas-con-textoPrimeros problemas-con-texto
Primeros problemas-con-texto
 
M4/12 Smartphones
M4/12 SmartphonesM4/12 Smartphones
M4/12 Smartphones
 
Mindstorm nxt
Mindstorm nxtMindstorm nxt
Mindstorm nxt
 
Ignite_October23_RobertBlackwell
Ignite_October23_RobertBlackwellIgnite_October23_RobertBlackwell
Ignite_October23_RobertBlackwell
 
предизборна платформа (1)
предизборна платформа (1)предизборна платформа (1)
предизборна платформа (1)
 
2013 11 12_shop professionalisieren
2013 11 12_shop professionalisieren2013 11 12_shop professionalisieren
2013 11 12_shop professionalisieren
 

Similar to Presentation_1370675757603

Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)David McCarter
 
Java 5 6 Generics, Concurrency, Garbage Collection, Tuning
Java 5 6 Generics, Concurrency, Garbage Collection, TuningJava 5 6 Generics, Concurrency, Garbage Collection, Tuning
Java 5 6 Generics, Concurrency, Garbage Collection, TuningCarol McDonald
 
Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)Hermann Hueck
 
Java Performance Tuning
Java Performance TuningJava Performance Tuning
Java Performance TuningMinh Hoang
 
Deuce STM - CMP'09
Deuce STM - CMP'09Deuce STM - CMP'09
Deuce STM - CMP'09Guy Korland
 
Design patterns in java script, jquery, angularjs
Design patterns in java script, jquery, angularjsDesign patterns in java script, jquery, angularjs
Design patterns in java script, jquery, angularjsRavi Bhadauria
 
NET Systems Programming Learned the Hard Way.pptx
NET Systems Programming Learned the Hard Way.pptxNET Systems Programming Learned the Hard Way.pptx
NET Systems Programming Learned the Hard Way.pptxpetabridge
 
Dart for Java Developers
Dart for Java DevelopersDart for Java Developers
Dart for Java DevelopersYakov Fain
 
Native Java with GraalVM
Native Java with GraalVMNative Java with GraalVM
Native Java with GraalVMSylvain Wallez
 
Introduction to new features in java 8
Introduction to new features in java 8Introduction to new features in java 8
Introduction to new features in java 8Raffi Khatchadourian
 
NDC 2011, C++ 프로그래머를 위한 C#
NDC 2011, C++ 프로그래머를 위한 C#NDC 2011, C++ 프로그래머를 위한 C#
NDC 2011, C++ 프로그래머를 위한 C#tcaesvk
 
TDD And Refactoring
TDD And RefactoringTDD And Refactoring
TDD And RefactoringNaresh Jain
 
Core java concepts
Core java  conceptsCore java  concepts
Core java conceptsRam132
 

Similar to Presentation_1370675757603 (20)

Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)
 
Java Generics
Java GenericsJava Generics
Java Generics
 
Java 5 6 Generics, Concurrency, Garbage Collection, Tuning
Java 5 6 Generics, Concurrency, Garbage Collection, TuningJava 5 6 Generics, Concurrency, Garbage Collection, Tuning
Java 5 6 Generics, Concurrency, Garbage Collection, Tuning
 
Metaprogramming
MetaprogrammingMetaprogramming
Metaprogramming
 
Lambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter LawreyLambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter Lawrey
 
Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)
 
Java Performance Tuning
Java Performance TuningJava Performance Tuning
Java Performance Tuning
 
Eugene Burmako
Eugene BurmakoEugene Burmako
Eugene Burmako
 
Deuce STM - CMP'09
Deuce STM - CMP'09Deuce STM - CMP'09
Deuce STM - CMP'09
 
Design patterns in java script, jquery, angularjs
Design patterns in java script, jquery, angularjsDesign patterns in java script, jquery, angularjs
Design patterns in java script, jquery, angularjs
 
Clean code
Clean codeClean code
Clean code
 
NET Systems Programming Learned the Hard Way.pptx
NET Systems Programming Learned the Hard Way.pptxNET Systems Programming Learned the Hard Way.pptx
NET Systems Programming Learned the Hard Way.pptx
 
Dart for Java Developers
Dart for Java DevelopersDart for Java Developers
Dart for Java Developers
 
Native Java with GraalVM
Native Java with GraalVMNative Java with GraalVM
Native Java with GraalVM
 
Introduction to new features in java 8
Introduction to new features in java 8Introduction to new features in java 8
Introduction to new features in java 8
 
Introduction to new features in java 8
Introduction to new features in java 8Introduction to new features in java 8
Introduction to new features in java 8
 
NDC 2011, C++ 프로그래머를 위한 C#
NDC 2011, C++ 프로그래머를 위한 C#NDC 2011, C++ 프로그래머를 위한 C#
NDC 2011, C++ 프로그래머를 위한 C#
 
TDD And Refactoring
TDD And RefactoringTDD And Refactoring
TDD And Refactoring
 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
 
Java 5 Features
Java 5 FeaturesJava 5 Features
Java 5 Features
 

More from Alexander Nevidimov

чек за платеж за телефон
чек за платеж за телефончек за платеж за телефон
чек за платеж за телефонAlexander Nevidimov
 
чек за платеж за телефон
чек за платеж за телефончек за платеж за телефон
чек за платеж за телефонAlexander Nevidimov
 

More from Alexander Nevidimov (20)

чек за платеж за телефон
чек за платеж за телефончек за платеж за телефон
чек за платеж за телефон
 
чек за платеж за телефон
чек за платеж за телефончек за платеж за телефон
чек за платеж за телефон
 
Presentation_1376917645876
Presentation_1376917645876Presentation_1376917645876
Presentation_1376917645876
 
Presentation_1376678601814
Presentation_1376678601814Presentation_1376678601814
Presentation_1376678601814
 
Presentation_1376311255728
Presentation_1376311255728Presentation_1376311255728
Presentation_1376311255728
 
Presentation_1376222064850
Presentation_1376222064850Presentation_1376222064850
Presentation_1376222064850
 
Presentation_1376220985856
Presentation_1376220985856Presentation_1376220985856
Presentation_1376220985856
 
Presentation_1376220236996
Presentation_1376220236996Presentation_1376220236996
Presentation_1376220236996
 
Presentation_1376218980392
Presentation_1376218980392Presentation_1376218980392
Presentation_1376218980392
 
Presentation_1376168115602
Presentation_1376168115602Presentation_1376168115602
Presentation_1376168115602
 
Presentation_1375882767439
Presentation_1375882767439Presentation_1375882767439
Presentation_1375882767439
 
Presentation_1375882705328
Presentation_1375882705328Presentation_1375882705328
Presentation_1375882705328
 
Presentation_1375280857464
Presentation_1375280857464Presentation_1375280857464
Presentation_1375280857464
 
Presentation_1375280653597
Presentation_1375280653597Presentation_1375280653597
Presentation_1375280653597
 
Presentation_1374052137363
Presentation_1374052137363Presentation_1374052137363
Presentation_1374052137363
 
Presentation_1373778041831
Presentation_1373778041831Presentation_1373778041831
Presentation_1373778041831
 
Presentation_1373190655210
Presentation_1373190655210Presentation_1373190655210
Presentation_1373190655210
 
Presentation_1372848115982
Presentation_1372848115982Presentation_1372848115982
Presentation_1372848115982
 
Presentation_1372103147097
Presentation_1372103147097Presentation_1372103147097
Presentation_1372103147097
 
Presentation_1371997361000
Presentation_1371997361000Presentation_1371997361000
Presentation_1371997361000
 

Presentation_1370675757603

  • 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
  • 6. Java Maritta Heisel Reminder New stuff Assertions Generic Types Implementing ADTs Generic Types Generic Type in Java expressed through: <T> (T for generic type has become a convention.) Example: class MyStack<T> 5/ 35
  • 7. Java Maritta Heisel Reminder New stuff Assertions Generic Types Implementing ADTs Example Point Example Nat Overview of DatAlg Example STACK General Approach Example Point ADT Type Point 6/ 35
  • 8. Java Maritta Heisel Reminder New stuff Assertions Generic Types Implementing ADTs Example Point Example Nat Overview of DatAlg Example STACK General Approach Example Point ADT Type Point Java 6/ 35
  • 9. Java Maritta Heisel Reminder New stuff Assertions Generic Types Implementing ADTs Example Point Example Nat Overview of DatAlg Example STACK General Approach Example Point ADT Type Point Java class MyPoint 6/ 35
  • 10. 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 7/ 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
  • 46. Java Maritta Heisel Reminder New stuff Assertions Generic Types Implementing ADTs Example Point Example Nat Overview of DatAlg Example STACK General Approach Example output 33/ 35
  • 47. Java Maritta Heisel Reminder New stuff Assertions Generic Types Implementing ADTs Example Point Example Nat Overview of DatAlg Example STACK General Approach Violating the contract 34/ 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