Sie sind auf Seite 1von 48

Java Maritta Heisel Reminder New stu 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

Reminder
Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs

Every program in Java is a class. A class denes 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

Constructor
Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs

Dierence 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

Methods & Attributes


Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs

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

Assertions
Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs

Assertions are used to enforce pre-/postconditions. Dening 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 lename

4/ 35

Generic Types
Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs

Generic Type in Java expressed through: <T> (T for generic type has become a convention.) Example: class MyStack<T>

5/ 35

Example Point
Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

ADT Type Point

6/ 35

Example Point
Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

ADT Type Point Java

6/ 35

Example Point
Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

ADT Type Point Java class MyPoint

6/ 35

Example Point - Constructor


Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

ADT create : Real Real Point

7/ 35

Example Point - Constructor


Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

ADT create : Real Real Point Java MyPoint(double xCoordinate, double yCoordinate) { this.xCoordinate = xCoordinate; this.yCoordinate = yCoordinate; }//end MyPoint

7/ 35

Example Point - Selector get x


Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

ADT get x : Point Real get x (create (x , y )) = x

8/ 35

Example Point - Selector get x


Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

ADT get x : Point Real get x (create (x , y )) = x Java public double getxCoordinate() { return xCoordinate; }//end getxCoordinate

8/ 35

Example Point - Predicate


Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

ADT is origin : Point Bool is origin(create (x , y )) = true x = 0 y = 0

9/ 35

Example Point - Predicate


Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

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

Example Point - Other Functions


Java Maritta Heisel

ADT distance : Point Point Real

Reminder New stu Assertions Generic Types Implementing ADTs


Example Point Example Nat Overview of DatAlg Example STACK General Approach

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

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

10/ 35

Example Point - Other Functions


Java Maritta Heisel

ADT distance : Point Point Real

Reminder New stu Assertions Generic Types Implementing ADTs


Example Point Example Nat Overview of DatAlg Example STACK General Approach

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

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

10/ 35

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 distance

Other Functions II
Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

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

Other Functions II
Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

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

Example Nat - Exercise description


Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

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

Example Nat - Implementation I


Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

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

Example Nat - Implementation II


Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

// selector function public void pred() {//predecessor assert value !=ZERO : "pred(): precondition not satisfied"; value = value - 1; }//end pred

14/ 35

Problems with call-by-value and objects


Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

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 dierent: We need to work with copies of our original objects to avoid undesired side-eects.

15/ 35

Simple copy method


Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

private MyNat copy(MyNat nat){ MyNat copyOfNat = new MyNat(); copyOfNat.value = nat.getValue(); return copyOfNat; } private int getValue(){ return this.value; }

16/ 35

Example Nat - Implementation contd I


Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

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

Example Nat - Implementation contd II


Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

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

Implementing Container Types I


Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

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

Implementing Container Types II


Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

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

Implementing Container Types III


Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

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 rst occurence of the provided element, if the element is not contained, no eect takes place

21/ 35

Implementing Container Types IV


Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

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

Example STACK[T] - Constructor mt stack


Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

ADT mt stack : STACK [T ]

23/ 35

Example STACK[T] - Constructor mt stack


Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

ADT mt stack : STACK [T ] Java private DatAlg<T> stack; MyStack() { stack = new DatAlg<T>(); }// end constructor

23/ 35

Example STACK[T] - Constructor push


Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

ADT push : T STACK [T ] STACK [T ]

24/ 35

Example STACK[T] - Constructor push


Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

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

Example STACK[T] - Selector pop


Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

ADT pop : STACK [T ] STACK [T ] pre(pop (s )) empty (s ) = false pop (push(x , s )) = s

25/ 35

Example STACK[T] - Selector pop


Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

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

Example STACK[T] - Selector top


Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

ADT top : STACK [T ] T pre(top (s )) empty (s ) = false top (push(x , s )) = x

26/ 35

Example STACK[T] - Selector top


Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

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

Example STACK[T] - Predicate empty


Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

ADT empty : STACK [T ] Bool empty (mt stack ) = true empty (push(x , s )) = false

27/ 35

Example STACK[T] - Predicate empty


Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

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

Displaying the outcome...


Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

...means providing an output method:

public void print() { System.out.println(Output of Stack: stack.toString()); }//end print

28/ 35

Instantiating Generic Types


Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

Type: String Instantiation: MyStack<String> myStack = new MyStack<String>(); Calling the method push: myStack.push("Mouse");

29/ 35

Example of a test class for MyStack I


Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

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

Example of a test class for MyStack II


Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

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

Example of a test class for MyStack III


Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

String top = myStack.top(); System.out.println("What is the top element?: " + top); }//end main } //end class

32/ 35

Example output
Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

33/ 35

Violating the contract


Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

34/ 35

General Procedure for implementing ADTs in Java


Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
Example Point Example Nat Overview of DatAlg Example STACK General Approach

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

Das könnte Ihnen auch gefallen