CS51 - Spring 2010 - Lecture 26

  • http://www.xkcd.com/829/

  • admin
       - Final exam: Wednesday, Dec. 15 at 9am in this room
       - No more TA office hours
       - As you're finishing up TP2
          - remember style is more important than functionality
          - go through your code one last time before you submit and check it for coding style!
          - turning it in late
             - you can turn it in late for a cost of 10% per day late
             - in general, I don't suggest this
             - think about if you will get more than ~10 points by taking an extra day

  • Sorting (Sort code)
       - Merge sort
          - first, look at the merge method
             - how can we use this method to sort numbers?
             - how could we use this method to sort two numbers?
             - can we repeat this idea? - what is the runtime?
          - what is the runtime?
             - look at the layers
             - each layer processes n items
             - how many layers are there?
                - each time we split the data in half
                - n/2^i = 1?
                - log(n) levels
             - O( n log n )

  • Built-in sorting
       - Objects must implement Comparable (requires compareTo method)
          - called comparison based sorting
       - Arrays.sort(Object[] a) -- uses merge sort
       - Collections.sort(List list) -- uses quicksort

  • Binary search
       - why is sorting useful?
          - useful to identy trends, patterns, etc.
          - can be useful for assisting in finding particular elements
       - Given a sorted list of elements, how can we find if a particular element exists in that list?
          - naive:
             - start at beginning and work your way through
             - running time:
                - O(n) - linear
                - we're not taking advantage of the fact that it is sorted
          - better method
             - let x be what we're looking for
             - pick an an element in the sorted list, call it y
             - compare x and y
             - if x == y
                - we're done
             - if x < y
                - continue looking at all elements in the list with indices less than y
             - if x > y
                - continue looking at all elements in the list with indices greater than y
       - how should we select x (i.e. the point to compare against)?
          - mid-point if we don't have any other information
       - what is the running time?
          - each time we're throwing away half of the data
          - eventually, we'll either find it or get down to the point where we only have one element left
          - how many tries will it take to get to this last point?
             - log(n) -- like above
          - how much work do we do each time we look at an element?
             - constant - O(1)
          - overall run-time
             - O(log(n))

  • final review
       - closed book, closed notes, etc.
       - comprehensive, though, I'll bias it towards the later material in the course
       - I will give you copies of:
          - Objectdraw quick reference
          - GUI cheat sheet
          - Streams cheat sheet
       - types of questions
          - code this up problems
          - what does this code do problems
          - short answer questions
       - topics (roughly)
          - basic programming
             - variables
                - instance
                - local
                - constants
             - methods
             - classes
             - parameters
             - loops
             - commenting
          - basic data types
             - booleans
             - ints and doubles
          - other programming concepts
             - active objects
             - defining and manipulating multiple classes
             - GUIs
             - randomness
             - colors
             - images
             - audio
             - interfaces
          - Recursion
             - structural
             - procedural
          - Inheritance
          - Arrays
             - one dimensional
             - two dimensional
          - Strings
          - Exceptions
          - I/O
             - file
             - streams
             - networking
          - Sorting/asymptotics

  • Practice problems:

  • Given the class definitions below:
    public class A{
       public void method1(){
       ...
       }
    }
    public class B extends A{
       public void method1(String a){
       ...
       }
       
       public void method2(){
       ...
       }
    }
    public class C extends B{
       public void method1(){
       ...
       }
    }

    private A someA;
    private B someB;
    private C someC;

  • - what assignments are legal?
  • - Is someC.method2() a legal call?
  • - Which classes method1 is called for:
  • ---- someB.method1();
  • ---- someC.method1("this is a string");

  • What would the call mystery(5) return?
    public String mystery(int num){
       if( num <= 1 ){
          return "1";
       }else{
          String returnMe = "";

          for( int i = 0; i < num; i++ ){
             returnMe += num
             // returnMe = returnMe + num;
          }

          return returnMe + mystery(num-1);
       }
    }

  • What does the following method return with the input: [1, 2, 3, 4, 5, 6, 7, 8]?

    // length of a is assumed to be even
    public int mystery(int[] a){
       int returnVal = 0;

       for( int i = 0; i < a.length/2; i++ ){
          returnVal += a[i] * a[a.length-i-1];
       }

       return returnVal;
    }

  • what does the method below do?

    public int[] mystery(int[][] a){
       int[] returnMe = new int[a.length];
       
       for( int i = 0; i < a.length; i++ ){
          int s = 0;

          for( int j = 0; j < a[i].length; j++ ){
             s += a[i][j];
          }

          if( s > 100 ){
             returnMe[i] = s;
          }else{
             returnMe[i] = 0;
          }
       }

       return returnMe;
    }

  • Why should you use the StringBuffer class?

  • One of the methods below will not compile. Indicate which one and explain why.

    public void methodA(){
       ...
       throw new IOException();
    }

    public void methodB(){
       ...
       throw new NumberFormatException();
    }