Week 2 - Wednesday CS221
Last time What did we talk about last time? Exceptions Threads OOP
Assignment 1
Project 1
Questions?
Interfaces
Interface basics An interface is a set of methods which a class must have Implementing an interface means making a promise to define each of the listed methods It can do what it wants inside the body of each method, but it must have them to compile Unlike superclasses, a class can implement as many interfaces as it wants
Interface definition An interface looks a lot like a class, but all its methods are empty Interfaces have no members except for (static final) constants public interface Guitarist { void strumChord(Chord chord); void playMelody(Melody notes); }
Interface use public class RockGuitarist extends RockMusician implements Guitarist { public void strumChord( Chord chord ) { System.out.print( "Totally wails on that " + chord.getName() + " chord!"); } public void playMelody( Melody notes ) { "Burns through the notes " + notes.toString() + " like Jimmy Page!" );
Usefulness A class has an is-a relationship with interfaces it implements, just like a superclass it extends Code that specifies a particular interface can use any class that implements it public static void perform(Guitarist guitarist, Chord chord, Melody notes) { System.out.println("Give it up " + "for the next guitarist!"); guitarist.strumChord( chord ); guitarist.playMelody( notes ); }
Generics
Generics Allow classes, interfaces, and methods to be written with a generic type parameter, then bound later Java does the type checking (e.g. making sure that you only put String objects into a List<String>) After typechecking, it erases the generic type parameter This works because all classes extend Object in Java Appears to function like templates in C++, but works very differently under the covers Most of the time you will use generics, not create them
Generic classes You can make a class using generics The most common use for these is for container classes e.g. you want a List class that can be a list of anything JCF is filled with such generics
Generic class example public class Pair<T> { private T x; private T y; public Pair(T a, T b ) { x = a; y = b; } public T getX() { return x; } public T getY() { return y; } public void swap() { T temp = x; x = y; y = temp; public String toString() { return "( " + x + ", " + y + " )";
Generic class use public class Test { public static void main(String[] args) { Pair<String> pair1 = new Pair<String>("ham", "eggs"); Pair<Integer> pair2 = new Pair<Integer>( 5, 7 ); pair1.swap(); System.out.println( pair1 ); System.out.println( pair2 ); }
JCF Java Collections Framework
Container interfaces Collection Iterable List Queue Set Map SortedSet SortedMap
Container classes LinkedList ArrayList Stack Vector HashSet TreeSet HashMap TreeMap
Tools Collections Arrays sort() max() min() replaceAll() reverse() binarySearch()
Big Oh Notation Impromptu Student Lecture
Computational Complexity
Running Time How do we measure the amount of time an algorithm takes? Surely sorting 10 numbers takes less time than sorting 1,000,000 numbers Sorting 1,000,000 numbers that are already sorted seems easier than sorting 1,000,000 unsorted numbers We use a worst-case, asymptotic function of input size called Big Oh notation
Big Oh Notation Worst-case because we care about how bad things could be Asymptotic because we ignore lower order terms and constants 15n2 + 6n + 7log(n) + 145 is O(n2) 2n + 3906n10000 + 892214 is O(2n) If the function of n is polynomial, we say that it is efficient or tractable
Examples For running time functions f(n) listed below, give the Big Oh f(n) = 3n2 + 4n + 100 O(n2) f(n) = 15n3 + nlog n + 100 O(n3) f(n) = 1000n + 10000log n O(n) f(n) = 5050 O(1)
Back to CS world We assume that all individual operations take the same amount of time So, we compute how many total operations we'll have for an input size of n (because we want to see how running time grows based on input size) Then, we find a function that describes that growth
Mulitiplication by hand How long does it take to do multiplication by hand? 123 x 456 738 615 492__ 56088 Let’s assume that the length of the numbers is n digits (n multiplications + n carries) x n digits + (n + 1 digits) x n additions Running time: O(n2)
Finding the largest element in an array How do we find the largest element in an array? Running time: O(n) if n is the length of the array What if the array is sorted in ascending order? Running time: O(1) int largest = array[0]; for( int i = 1; i < length; i++ ) if( array[i] > largest ) largest = array[i]; System.out.println("Largest: " + largest); System.out.println("Largest: " + array[length-1]);
Multiplying matrices Given two n x n matrices A and B, the code to multiply them is: Running time: O(n3) Is there a faster way to multiply matrices? Yes, but it’s complicated and has other problems double[][] c = new double[N][N]; for( int i = 0; i < N; i++ ) for( int j = 0; j < N; j++ ) { c[i][j] = 0; for( int k = 0; k < N; k++ ) c[i][j] += a[i][k]*b[k][j]; }
Bubble sort Here is some code that sorts an array in ascending order What is its running time? Running time: O(n2) for( int i = 0; i < length - 1; i++ ) for( int j = 0; j < length - 1; j++ ) if( array[j] > array[j + 1] ) { int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; }
Printing a triangle for( int i = 0; i < n; i++ ) { Here is some code that prints out a triangular shaped set of stars What is its running time? Running time: O(n2) for( int i = 0; i < n; i++ ) { for( int j = 0; j <= i; j++ ) System.out.print("*"); System.out.println(); }
Mathematical issues What's the running time to factor a large number N? How many edges are in a completely connected graph? If you have a completely connected graph, how many possible tours are there (paths that start at a given node, visit all other nodes, and return to the beginning)? How many different n-bit binary numbers are there?
Formal definition of Big Oh Let f(n) and g(n) be two functions over integers f(n) is O(g(n)) if and only if f(n) ≤ c∙g(n) for all n > N for some positive real numbers c and N In other words, past some arbitrary point, with some arbitrary scaling factor, g(n) is always bigger
Hierarchy of complexities Here is a table of several different complexity measures, in ascending order, with their functions evaluated at n = 100 Description Big Oh f(100) Constant O(1) 1 Logarithmic O(log n) 6.64 Linear O(n) 100 Linearithmic O(n log n) 664.39 Quadratic O(n2) 10000 Cubic O(n3) 1000000 Exponential O(2n) 1.27 x 1030 Factorial O(n!) 9.33 x 10157
What is log? The log operator is short for logarithm Taking the logarithm means de-exponentiating something log 10 7 =7 log 10 𝑥 =𝑥 What's the log 1,000,000 ?
Logarithms Formal definition: Think of it as a de-exponentiator If bx = y Then logb y = x (for positive b values) Think of it as a de-exponentiator Examples: log10(1,000,000) = log3(81) = log2(512) =
Log base 2 In the normal world, when you see a log without a subscript, it means the logarithm base 10 "What power do you have to raise 10 to to get this number?" In computer science, a log without a subscript usually means the logarithm base 2 "What power do you have to raise 2 to to get this number?" log 2 8 =8 log 2 𝑦 =𝑦 What's the log 2048? (Assuming log base 2)
Log math logb(xy) = logb(x) + logb(y) logb(x/y) = logb(x) - logb(y) logb(xy) = y logb(x) Base conversion: logb(x) = loga(x)/loga(b) As a consequence: log2(n) = log10(n)/c1 = log100(n)/c2 = logb(n)/c3 for b > 1 log2n is O(log10n) and O(log100 n) and O(logbn) for b > 1
More on log Add one to the logarithm in a base and you'll get the number of digits you need to represent that number in that base In other words, the log of a number is related to its length Even big numbers have small logs If there's no subscript, log10 is assumed in math world, but log2 is assumed for CS Also common is ln, the natural log, which is loge
Log is awesome The logarithm of the number is related to the number of digits you need to write it That means that the log of a very large number is pretty small An algorithm that runs in log n time is very fast Number log10 log2 1,000 3 10 1,000,000 6 20 1,000,000,000 9 30 1,000,000,000,000 12 40
Quiz
Upcoming
Next time… Computing complexity Abstract data types (ADTs)
Reminders Read section 1.2 Finish Assignment 1 Start Project 1 Due Friday by 11:59pm Start Project 1 Due Friday, September 22 by 11:59pm