CS62 - Spring 2010 - Lecture 8

  • prove the the sum of 1, 2, ..., n = (n+1)n/2
       - what is the base case?
          - n = 1
             - 2*1/2 = 1
             - verified
       - what is the inductive case?   
          - assume 1, 2, ..., i-1 = i(i-1)/2
          - show that it holds for i, i.e. 1, 2, ..., i = (i+1)i/2
             1, 2, ..., i =
             1, 2, ..., i-1, i =
             i(i-1)/2 + i (by our inductive assumption) =
             i(i-1)/2 + 2i/2 =
             (i(i-1) + 2i)/2 =
             (i^2 -i + 2i)/2 =
             (i^2+i)/2 =
             i(i+1)/2
       - done!
       - why does this work?
          - we know it holds for 1
          - because we know it holds for 1 and we know that if it holds for i-1 then it holds for i, then it holds for 2
          - which means it holds for 3
          - etc, etc up to n

  • Exercise 5.23: Prove that 5^n - 4n - 1 is divisible by 4
       - Let us prove this by induction
       - First, the base case: note that if n = 0, then
          - 5^n - 4n - 1 = 1 + 0 - 1 = 0
          - Since 16 divides 0 evenly, the observation holds for n = 0
       - Now, the recursive case: assume that the observation holds for all values less than n, and specifically n - 1.
       - We have, then:
          - 5^{n - 1} - 4(n - 1) - 1 is divisible by 16
       - now we need to do some manipulation
          - multiply by 5
             5^n - 20(n - 1) - 5
              which is also divisible by 16
          - add 16(n - 1): (a multiple of 16):
             = 5^n − 20(n - 1) + 16(n - 1) - 5
             = 5^n - 4(n - 1) - 5
             = 5^n - 4n + 4 - 5
             = 5^n - 4n - 1
    Clearly, this expression is divisible by 16, so the observation holds for n. By
    induction on , we see the result holds for all n >= 0.

  • how does this relate to recursion?

  • Interfaces
       - Why do we use interfaces?
       - We've discussed a number of sorting algorithms, as we saw in the lab, one way to link them all together is by creating an interface
       - what would go in the interface for sorting methods?
          - interface Sorter<E extends Comparable<E>>{
             public void sort(ArrayList<E> list);
          }
       - When designing interfaces, think about the shared functionality
       - So, how would we use this to implement new sorting methods?
          - public class MergeSort <E extends Comparable<E>> implements Sorter<E>{...
       - A lot of the sorting methods use the swap method. Let's sway we wanted to write classes for three different sorting methods, is there any way we can avoid having to write the swap method in every class?
       
  • abstract classes
       - rather than using an interface, we can write a class, but just leave some of the methods unimplemented. This is called an "abstract" class.
          - you label the class as abstract in the header
          - any methods where you do not provide the implementation, must also be labeled abstract
          - see AbstractSorter class in AbstractClassExamples code
       - to use an abstract class, we just extend the class like we would any class AND you must implement all the abstract methods (like an you would have to with an interface)
       - see SorterExample class in AbstractClassExamples code
       - note you cannot instantiate an abstract class (it's abstract, i.e not a real class)
       - look at StringManipulator class in AbstractClassExamples code
          - notice that we use the manipulate(String s) method even though it's not defined
          - what does the manipulate method do in Reverser?
          - what will be printed out if we run the program?
       - abstract classes are a nice blend of interfaces and built-in functionality. What is the downside?
          - it's a class, so there's no multiple inheritence
       - we can actually use an abstract method in an abstract class and then this requires that the class that extends this must provide the functionality
       - book will name a class "abstract" but not have any abstract methods. Why would it do this?

  • Iterators (java.util.iterator: http://java.sun.com/j2se/1.4.2/docs/api/java/util/Iterator.html)
       - three methods
          - hasNext(): checks if there are more elements to iterate through
          - next(): give us the next element and move the iterator forward one element
          - remove(): removes the previously returned method from the last call to next(). NOTE: this is an optional method. Often, when we're looking at these, we won't implement it.
       - look at arraylistIterator method in IteratorExamples class in IteratorExamples code
       - look at arraylistIteratorConcern in IteratorExamples class in IteratorExamples code
       - look at arraylistIteratorConcern in IteratorExamples class in IteratorExamples code

  • Iterable (http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Iterable.html)
       - just have to implement one method:
          - iterator(), which returns an iterator over this data
       - if a class implements an the Iterable interface, then we can use it in the "foreach" loop:
          Iterable<E> myClass;

          for( E s: myClass ){
             // do something with s
          }
       - show AlphabetSoup class in IteratorExamples code
       - look at alphabetIterable in in IteratorExamples class in IteratorExamples code
       - how could we write our own iterator for AlphabetSoup?
          - show SoupIterator class in IteratorExamples code

  • File I/O
       - File class (http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html)
          - represents a possible file or a directory on disk
             - that file or directory may NOT actually exist
          - what is a path?
             - all the subdirectories leading up to this file or directory
          - what's the difference between getPath() and getAbsolutePath()
             - an absolute path starts from the root directory (in our case '/')
             - getPath() returns the path relative to the directory the user is in (in Eclipse, it's within the workspace)
          - a number of useful commands
             - delete()
             - exists()
             - createNewFile()
             - isFile()
             - isDirectory()
             - listFiles()
             - mkdir()
             - renameTo(...)
          - what is a path separator?
          - why should we use File.pathSeparator vs. just putting in '/'?
       - Two common classes
          - PrintWriter out = new PrintWriter(new FileWriter(...));
          - BufferedReader in = new BufferedReader(new FileReader(...));
          - why the complicated statements?