Académique Documents
Professionnel Documents
Culture Documents
1/ 35
Reminder
Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
1/ 35
Constructor
Java Maritta Heisel Reminder New stu Assertions Generic Types Implementing ADTs
2/ 35
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
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
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
6/ 35
7/ 35
ADT create : Real Real Point Java MyPoint(double xCoordinate, double yCoordinate) { this.xCoordinate = xCoordinate; this.yCoordinate = yCoordinate; }//end MyPoint
7/ 35
8/ 35
ADT get x : Point Real get x (create (x , y )) = x Java public double getxCoordinate() { return xCoordinate; }//end getxCoordinate
8/ 35
9/ 35
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
((x z )2 + (y w )2 )
10/ 35
((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
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
// 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
// selector function public void pred() {//predecessor assert value !=ZERO : "pred(): precondition not satisfied"; value = value - 1; }//end pred
14/ 35
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
private MyNat copy(MyNat nat){ MyNat copyOfNat = new MyNat(); copyOfNat.value = nat.getValue(); return copyOfNat; } private int getValue(){ return this.value; }
16/ 35
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
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
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
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
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
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
23/ 35
ADT mt stack : STACK [T ] Java private DatAlg<T> stack; MyStack() { stack = new DatAlg<T>(); }// end constructor
23/ 35
24/ 35
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
25/ 35
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
26/ 35
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
ADT empty : STACK [T ] Bool empty (mt stack ) = true empty (push(x , s )) = false
27/ 35
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
28/ 35
Type: String Instantiation: MyStack<String> myStack = new MyStack<String>(); Calling the method push: myStack.push("Mouse");
29/ 35
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
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
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
34/ 35
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