Presentation_1370675757603

266 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
266
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Presentation_1370675757603

  1. 1. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsFrom ADTs to Java Source CodeSS 2011Maritta HeiselMaritta.Heisel(AT)uni-due.deUniversity Duisburg-Essen – Faculty of EngineeringDepartment of Computer ScienceWorkgroup Software Engineering1/ 35
  2. 2. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsReminderEvery 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
  3. 3. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsConstructorDifference 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
  4. 4. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsMethods & AttributesMethods:We use methods to implement ADT functions in JavaWe implement our ADT functions as public methods inJavaException: hidden, as well as auxiliary functions areimplemented as private methodsAttributes:remain3/ 35
  5. 5. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsAssertionsAssertions 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 filename4/ 35
  6. 6. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsGeneric TypesGeneric Type in Java expressed through: <T>(T for generic type has become a convention.)Example: class MyStack<T>5/ 35
  7. 7. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachExample PointADTType Point6/ 35
  8. 8. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachExample PointADTType PointJava6/ 35
  9. 9. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachExample PointADTType PointJavaclass MyPoint6/ 35
  10. 10. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachExample Point - ConstructorADTcreate : Real × Real → Point7/ 35
  11. 11. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachExample Point - ConstructorADTcreate : Real × Real → PointJavaMyPoint(double xCoordinate, double yCoordinate){this.xCoordinate = xCoordinate;this.yCoordinate = yCoordinate;}//end MyPoint7/ 35
  12. 12. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachExample Point - Selector get xADTget x : Point → Realget x(create(x, y)) = x8/ 35
  13. 13. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachExample Point - Selector get xADTget x : Point → Realget x(create(x, y)) = xJavapublic double getxCoordinate(){return xCoordinate;}//end getxCoordinate8/ 35
  14. 14. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachExample Point - PredicateADTis origin : Point → Boolis origin(create(x, y)) = true ⇔ x = 0 ∧ y = 09/ 35
  15. 15. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachExample Point - PredicateADTis origin : Point → Boolis origin(create(x, y)) = true ⇔ x = 0 ∧ y = 0Javapublic boolean isOrigin(){if (xCoordinate == 0.0 && yCoordinate == 0.0){return true;}return false;}//end isOrigin9/ 35
  16. 16. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachExample Point - Other FunctionsADTdistance : Point × Point → Realdistance(create(x, y), create(z, w)) = ((x − z)2 + (y − w)2)10/ 35
  17. 17. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachExample Point - Other FunctionsADTdistance : Point × Point → Realdistance(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
  18. 18. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachOther Functions IIOutput 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 toString11/ 35
  19. 19. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachOther Functions IIOutput 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 toString11/ 35
  20. 20. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachExample Nat - Exercise descriptionImplement 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
  21. 21. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachExample Nat - Implementation I// variablesprivate int value;private final int ZERO = 0;// constructor functionsMyNat() {this.value = ZERO;}//end NATpublic void succ() {//successorvalue = value + 1;}//end succ13/ 35
  22. 22. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachExample Nat - Implementation II// selector functionpublic void pred() {//predecessorassert value !=ZERO :"pred(): precondition not satisfied";value = value - 1;}//end pred14/ 35
  23. 23. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachProblems with call-by-value and objectsRemember, 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
  24. 24. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachSimple copy methodprivate MyNat copy(MyNat nat){MyNat copyOfNat = new MyNat();copyOfNat.value = nat.getValue();return copyOfNat;}private int getValue(){return this.value;}16/ 35
  25. 25. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachExample Nat - Implementation cont’d Ipublic MyNat add(MyNat nat) {MyNat aux = copy(this);MyNat aux2 = copy(nat);MyNat sum = new MyNat();//add(zero,i)=iif(aux.value == ZERO) {return aux2;}else{//add(succ(i), j) = succ(add(i, j))aux.pred();sum = aux.add(aux2);sum.succ();return sum;}}//add17/ 35
  26. 26. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachExample Nat - Implementation cont’d IIpublic MyNat mult(MyNat nat) {//multiplicationMyNat aux = copy(this);MyNat aux2 = copy(nat);MyNat prod = new MyNat();//mult(zero, i) = zeroif (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 mult18/ 35
  27. 27. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachImplementing Container Types IWe 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
  28. 28. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachImplementing Container Types IIpublic 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
  29. 29. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachImplementing Container Types IIIpublic 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-1public void toRemove(T element) : deletes the firstoccurence of the provided element, if the element is notcontained, no effect takes place21/ 35
  30. 30. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachImplementing Container Types IVpublic 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 falseotherwisepublic 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
  31. 31. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachExample STACK[T] - Constructor mt stackADTmt stack : STACK[T]23/ 35
  32. 32. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachExample STACK[T] - Constructor mt stackADTmt stack : STACK[T]Javaprivate DatAlg<T> stack;MyStack() {stack = new DatAlg<T>();}// end constructor23/ 35
  33. 33. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachExample STACK[T] - Constructor pushADTpush : T × STACK[T] → STACK[T]24/ 35
  34. 34. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachExample STACK[T] - Constructor pushADTpush : T × STACK[T] → STACK[T]Javapublic void push (T element) {stack.insertElement(element,0);assert top() == element :"push(): postcondition not satisfied";}//end push24/ 35
  35. 35. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachExample STACK[T] - Selector popADTpop : STACK[T] → STACK[T]pre(pop(s)) ⇔ empty(s) = falsepop(push(x, s)) = s25/ 35
  36. 36. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachExample STACK[T] - Selector popADTpop : STACK[T] → STACK[T]pre(pop(s)) ⇔ empty(s) = falsepop(push(x, s)) = sJavapublic void pop() {assert !empty() :"pop(): precondition not satisfied";stack.removeElement(0);}//end pop25/ 35
  37. 37. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachExample STACK[T] - Selector topADTtop : STACK[T] → Tpre(top(s)) ⇔ empty(s) = falsetop(push(x, s)) = x26/ 35
  38. 38. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachExample STACK[T] - Selector topADTtop : STACK[T] → Tpre(top(s)) ⇔ empty(s) = falsetop(push(x, s)) = xJavapublic T top() {assert this.empty() == false :"top(): precondition not satisfied";return stack.getElement(0);}//end top26/ 35
  39. 39. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachExample STACK[T] - Predicate emptyADTempty : STACK[T] → Boolempty(mt stack) = trueempty(push(x, s)) = false27/ 35
  40. 40. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachExample STACK[T] - Predicate emptyADTempty : STACK[T] → Boolempty(mt stack) = trueempty(push(x, s)) = falseJavapublic boolean empty(){return stack.isEmpty();}//end top27/ 35
  41. 41. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachDisplaying the outcome......means providing an output method:public void print() {System.out.println(‘‘Output of Stack: ’’ +stack.toString());}//end print28/ 35
  42. 42. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachInstantiating Generic TypesType: StringInstantiation:MyStack<String> myStack= new MyStack<String>();Calling the method push:myStack.push("Mouse");29/ 35
  43. 43. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachExample of a test class for MyStack Iclass 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. 44. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachExample of a test class for MyStack IISystem.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. 45. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachExample of a test class for MyStack IIIString top = myStack.top();System.out.println("What isthe top element?: " + top);}//end main} //end class32/ 35
  46. 46. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachExample output33/ 35
  47. 47. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachViolating the contract34/ 35
  48. 48. JavaMaritta HeiselReminderNew stuffAssertionsGeneric TypesImplementingADTsExample PointExample NatOverview ofDatAlgExample STACKGeneralApproachGeneral Procedure for implementing ADTs in Java1. 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

×