Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 2 - Wednesday CS221.

Similar presentations


Presentation on theme: "Week 2 - Wednesday CS221."— Presentation transcript:

1 Week 2 - Wednesday CS221

2 Last time What did we talk about last time? Exceptions Threads OOP

3 Assignment 1

4 Project 1

5 Questions?

6 Interfaces

7 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

8 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); }

9 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!" );

10 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 ); }

11 Generics

12 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

13 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

14 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 + " )";

15 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 ); }

16 JCF Java Collections Framework

17 Container interfaces Collection Iterable List Queue Set Map SortedSet
SortedMap

18 Container classes LinkedList ArrayList Stack Vector HashSet TreeSet
HashMap TreeMap

19 Tools Collections Arrays sort() max() min() replaceAll() reverse()
binarySearch()

20 Big Oh Notation Impromptu Student Lecture

21 Computational Complexity

22 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

23 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) is O(n2) 2n n is O(2n) If the function of n is polynomial, we say that it is efficient or tractable

24 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 log n O(n) f(n) = 5050 O(1)

25 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

26 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)

27 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]);

28 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]; }

29 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; }

30 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(); }

31 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?

32 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

33 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) Exponential O(2n) 1.27 x 1030 Factorial O(n!) 9.33 x 10157

34 What is log? The log operator is short for logarithm
Taking the logarithm means de-exponentiating something log =7 log 10 𝑥 =𝑥 What's the log 1,000,000 ?

35 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) =

36 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)

37 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

38 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

39 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

40 Quiz

41 Upcoming

42 Next time… Computing complexity Abstract data types (ADTs)

43 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


Download ppt "Week 2 - Wednesday CS221."

Similar presentations


Ads by Google