Algorithms and OOD (CSC 207 2014F) : EBoards

CSC 207.01 2014F: Extra Session 6 (Thursday, 9 October 2014)


Topics to discuss

Can you give us sample grades? Yes. After the first exam.

Predicates

In Scheme, a predicate is a function that returns true or false. even? exact? list? color? = equal? eq?

In Java, a Predicate is an object that (1) implements the Predicate interface and (2) includes a method test(T val).

Predicate is really java.util.function.Predicate and it's a generic interface.

We have three ways of making these kinds of objects.

Examples

    public class Even
        implements Predicate<Integer>
    {
      public boolean test(Integer val)
      {
        return (val % 2) == 0;
      } // test(Integer)
    } // class Even

Somewhere else in my code ...

    Predicate<Integer> even = new Even();

Anonymous inner classes

    Predicate<Integer> even = new Predicate<Integer>() {
      public boolean test(Integer val)
      {
        return (val % 2) == 0;
      } // test(Integer)
    };

Anonymous procedures

    Predicate<Integer> even = (val) -> (val % 2) == 0;
    Predicate<Integer> even = (val) -> { return (val % 2) == 0; }

Anonymous Procedures

Let's look at a more complex example

    @FunctionalInterface
    public interface ArrayManipulator<T>
    {
      public T manipulate(T[] vals, int i);
    } // ArrayManipulator

    ArrayManipulator<Integer> albert = (larray, x) -> { return larray[x]; }

Big O Notation

What is the idea of Big O?

How do you do the analysis?

Let's suppose we just tried to count each time through

1 + 2 + 3 + 4 + ... + n = n(n+1)/2 = n^2/2 + 1/2 is in O(n^2)