presentation_1370675757603

48
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

Upload: alexander-nevidimov

Post on 15-Jul-2015

151 views

Category:

Documents


0 download

TRANSCRIPT

Java

Maritta Heisel

Reminder

New stuff

Assertions

Generic Types

ImplementingADTs

From ADTs to Java Source CodeSS 2011

Maritta HeiselMaritta.Heisel(AT)uni-due.de

University Duisburg-Essen – Faculty of EngineeringDepartment of Computer ScienceWorkgroup Software Engineering

1/ 35

Java

Maritta Heisel

Reminder

New stuff

Assertions

Generic Types

ImplementingADTs

Reminder

Every program in Java is a class.

A class defines three components:

Constructor: special method for creating and initializingobjectsMethods: 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

ImplementingADTs

Constructor

Difference between ADT constructor and constructor usedin Java :

Not all elements can be generated by applying only theconstructor.Only initial elements can be created (e.g. emptycontainers)

Recursive ADT-constructors will be implemented as(ordinary) methods.

2/ 35

Java

Maritta Heisel

Reminder

New stuff

Assertions

Generic Types

ImplementingADTs

Methods & Attributes

Methods:

We use methods to implement ADT functions in Java

We implement our ADT functions as public methods inJava

Exception: hidden, as well as auxiliary functions areimplemented as private methods

Attributes:

remain

3/ 35

Java

Maritta Heisel

Reminder

New stuff

Assertions

Generic Types

ImplementingADTs

Assertions

Assertions are used to enforce pre-/postconditions.Defining an assertion in Java :assert boolean Expression; orassert boolean Expression: Expression;

Example: assert this.empty() == false; orassert this.empty() == false:

"The precondition is not satisfied";

Necessary to use additional command option to enableassertion output (default is disabled)

Enabling the assertions through adding “ea” to the javacommand for the interpreter: java -ea filename

4/ 35

Java

Maritta Heisel

Reminder

New stuff

Assertions

Generic Types

ImplementingADTs

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

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

Example Point

ADTType Point

Javaclass MyPoint

6/ 35

Java

Maritta Heisel

Reminder

New stuff

Assertions

Generic Types

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

Example Point

ADTType Point

Java

class MyPoint

6/ 35

Java

Maritta Heisel

Reminder

New stuff

Assertions

Generic Types

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

Example Point

ADTType Point

Javaclass MyPoint

6/ 35

Java

Maritta Heisel

Reminder

New stuff

Assertions

Generic Types

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

Example Point - Constructor

ADT

create : Real × Real → Point

JavaMyPoint(double xCoordinate, double yCoordinate)

{this.xCoordinate = xCoordinate;

this.yCoordinate = yCoordinate;

}//end MyPoint

7/ 35

Java

Maritta Heisel

Reminder

New stuff

Assertions

Generic Types

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

Example Point - Constructor

ADT

create : Real × Real → Point

JavaMyPoint(double xCoordinate, double yCoordinate)

{this.xCoordinate = xCoordinate;

this.yCoordinate = yCoordinate;

}//end MyPoint

7/ 35

Java

Maritta Heisel

Reminder

New stuff

Assertions

Generic Types

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

Example Point - Selector get x

ADT

get x : Point→ Real

get x(create(x , y)) = x

Javapublic double getxCoordinate()

{return xCoordinate;

}//end getxCoordinate

8/ 35

Java

Maritta Heisel

Reminder

New stuff

Assertions

Generic Types

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

Example Point - Selector get x

ADT

get x : Point→ Real

get x(create(x , y)) = x

Javapublic double getxCoordinate()

{return xCoordinate;

}//end getxCoordinate

8/ 35

Java

Maritta Heisel

Reminder

New stuff

Assertions

Generic Types

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

Example Point - Predicate

ADT

is origin : Point→ Bool

is origin(create(x , y)) = true ⇔ x = 0 ∧ y = 0

Javapublic 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

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

Example Point - Predicate

ADT

is origin : Point→ Bool

is origin(create(x , y)) = true ⇔ x = 0 ∧ y = 0

Javapublic 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

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

Example Point - Other Functions

ADT

distance : Point × Point→ Real

distance(create(x , y), create(z ,w)) =√

((x − z)2 + (y − w)2)

Javapublic 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 distance

10/ 35

Java

Maritta Heisel

Reminder

New stuff

Assertions

Generic Types

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

Example Point - Other Functions

ADT

distance : Point × Point→ Real

distance(create(x , y), create(z ,w)) =√

((x − z)2 + (y − w)2)

Javapublic 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

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

Other Functions II

Output the result using the toString method:

writing a new output methodpublic 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

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

Other Functions II

Output the result using the toString method:

writing a new output methodpublic 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

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

Example Nat - Exercise description

Implement the following functions of the ADT Nat:

Nat (constructor)

succ (constructor); implement it as a procedure. You areallowed to use the expression + 1.

pred (selector); implement it as a procedure. You areallowed to use the expression − 1.

add (other function); implement it as a function. You canonly use methods of the class itself.

mult (other function); implement it as a function. You canonly use methods of the class itself.

12/ 35

Java

Maritta Heisel

Reminder

New stuff

Assertions

Generic Types

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

Example Nat - Implementation I

// variablesprivate int value;

private final int ZERO = 0;

// constructor functionsMyNat() {

this.value = ZERO;

}//end NAT

public void succ() {//successorvalue = value + 1;

}//end succ

13/ 35

Java

Maritta Heisel

Reminder

New stuff

Assertions

Generic Types

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

Example Nat - Implementation II

// selector functionpublic 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

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

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 toavoid undesired side-effects.

15/ 35

Java

Maritta Heisel

Reminder

New stuff

Assertions

Generic Types

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

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

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

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

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

Example Nat - Implementation cont’d II

public MyNat mult(MyNat nat) {//multiplicationMyNat 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

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

Implementing Container Types I

We supply you with a class named DatAlg providing thefollowing functionality:

DatAlg() : constructor :This method constructs a new strucuture able to handleelements of generic type (denoted by T).The default size is set to 10.

DatAlg(int length) : constructor : This method constructsa new structure of length size able to handle elements ofgeneric 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

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

Implementing Container Types II

public void insertElement(T element, int pos): Themethod 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 andsize-1.

The elements are shifted one position to the right, startingfrom the former element at position pos. The size of thestructure is automatically increased by 1 if the number ofelements after insertion exceeds the current size.

public void addElement(T element, int pos) : addselement 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

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

Implementing Container Types III

public void removeElement(int pos): This method deletesthe element at the provided position pos.The parameter pos denotes the position of the element tobe deleted.pos must be a natural number between 0 and size-1

public void toRemove(T element) : deletes the firstoccurence of the provided element, if the element is notcontained, no effect takes place

21/ 35

Java

Maritta Heisel

Reminder

New stuff

Assertions

Generic Types

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

Implementing Container Types IV

public boolean isEmtpy(): returns true if thecorresponding container is empty, returns false otherwise.

public boolean contained(T element): returns true if theelement is contained in the structure, returns falseotherwise

public T getElement(int pos):The method returns the element at the provided positionpos.pos must be a natural number between 0 and size-1.

public void toString() : prints the elements of the currentcontainer.

22/ 35

Java

Maritta Heisel

Reminder

New stuff

Assertions

Generic Types

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

Example STACK[T] - Constructor mt stack

ADT

mt stack : STACK [T ]

Javaprivate DatAlg<T> stack;

MyStack() {stack = new DatAlg<T>();

}// end constructor

23/ 35

Java

Maritta Heisel

Reminder

New stuff

Assertions

Generic Types

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

Example STACK[T] - Constructor mt stack

ADT

mt stack : STACK [T ]

Javaprivate DatAlg<T> stack;

MyStack() {stack = new DatAlg<T>();

}// end constructor

23/ 35

Java

Maritta Heisel

Reminder

New stuff

Assertions

Generic Types

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

Example STACK[T] - Constructor push

ADT

push : T × STACK [T ]→ STACK [T ]

Javapublic 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

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

Example STACK[T] - Constructor push

ADT

push : T × STACK [T ]→ STACK [T ]

Javapublic 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

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

Example STACK[T] - Selector pop

ADT

pop : STACK [T ] 7→ STACK [T ]

pre(pop(s))⇔ empty(s) = false

pop(push(x , s)) = s

Javapublic void pop() {

assert !empty() :

"pop(): precondition not satisfied";

stack.removeElement(0);

}//end pop

25/ 35

Java

Maritta Heisel

Reminder

New stuff

Assertions

Generic Types

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

Example STACK[T] - Selector pop

ADT

pop : STACK [T ] 7→ STACK [T ]

pre(pop(s))⇔ empty(s) = false

pop(push(x , s)) = s

Javapublic void pop() {

assert !empty() :

"pop(): precondition not satisfied";

stack.removeElement(0);

}//end pop

25/ 35

Java

Maritta Heisel

Reminder

New stuff

Assertions

Generic Types

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

Example STACK[T] - Selector top

ADT

top : STACK [T ] 7→ T

pre(top(s))⇔ empty(s) = false

top(push(x , s)) = x

Javapublic 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

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

Example STACK[T] - Selector top

ADT

top : STACK [T ] 7→ T

pre(top(s))⇔ empty(s) = false

top(push(x , s)) = x

Javapublic 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

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

Example STACK[T] - Predicate empty

ADT

empty : STACK [T ]→ Bool

empty(mt stack) = true

empty(push(x , s)) = false

Javapublic boolean empty()

{return stack.isEmpty();

}//end top

27/ 35

Java

Maritta Heisel

Reminder

New stuff

Assertions

Generic Types

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

Example STACK[T] - Predicate empty

ADT

empty : STACK [T ]→ Bool

empty(mt stack) = true

empty(push(x , s)) = false

Javapublic boolean empty()

{return stack.isEmpty();

}//end top

27/ 35

Java

Maritta Heisel

Reminder

New stuff

Assertions

Generic Types

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

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

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

Instantiating Generic Types

Type: StringInstantiation:MyStack<String> myStack

= new MyStack<String>();

Calling the method push:myStack.push("Mouse");

29/ 35

Java

Maritta Heisel

Reminder

New stuff

Assertions

Generic Types

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

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

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

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

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

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

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

Example output

33/ 35

Java

Maritta Heisel

Reminder

New stuff

Assertions

Generic Types

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

Violating the contract

34/ 35

Java

Maritta Heisel

Reminder

New stuff

Assertions

Generic Types

ImplementingADTs

Example Point

Example Nat

Overview ofDatAlg

Example STACK

GeneralApproach

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 asconstructor method in Java with the same name as theclass.

3. Implement the recursive ADT-constructor functions,selector functions, predicates, and other functions asmethods. Where applicable add the preconditions andpostconditions through assertions.

4. Implement an output function.

5. Create a second class (test class) in the same directory asyour 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 theADT implementation.

35/ 35