Java AP Computer Science AB: Tradeoffs, Intuition, Object-Oriented Stuff - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

Java AP Computer Science AB: Tradeoffs, Intuition, Object-Oriented Stuff

Description:

Java AP Computer Science AB: Tradeoffs, Intuition, Object-Oriented Stuff Owen Astrachan Department of Computer Science, Duke University Workshop sponsored by NCSSM ... – PowerPoint PPT presentation

Number of Views:195
Avg rating:3.0/5.0
Slides: 37
Provided by: Owen73
Learn more at: https://www2.cs.duke.edu
Category:

less

Transcript and Presenter's Notes

Title: Java AP Computer Science AB: Tradeoffs, Intuition, Object-Oriented Stuff


1
JavaAP Computer ScienceAB Tradeoffs,
Intuition, Object-Oriented Stuff
  • Owen Astrachan
  • Department of Computer Science, Duke University
  • Workshop sponsored by NCSSM/Project NOW and IBM
    Eclipse

2
Why Java and C aren't so different
James Gosling
Bjarne Stroustrup
3
Efficient Programming
  • Designing and building efficient programs
    efficiently requires knowledge and practice
  • Hopefully the programming language helps, its
    not intended to get in the way
  • Object-oriented concepts, and more general
    programming concepts help in developing programs
  • Knowledge of data structures and algorithms helps
  • Tools of the engineer/scientist/programmer/designe
    r
  • A library or toolkit is essential, API or wheel
    re-invention?
  • Programming art, science, engineering? None or
    All?
  • Mathematics is a tool
  • Design Patterns are a tool

4
David Parnas
  • "For much of my life, I have been a software
    voyeur, peeking furtively at other people's dirty
    code. Occasionally, I find a real jewel, a
    well-structured program written in a consistent
    style, free of kludges, developed so that each
    component is simple and organized, and designed
    so that the product is easy to change. "
  • "We must not forget that the wheel is reinvented
    so often because it is a very good idea I've
    learned to worry more about the soundness of
    ideas that were invented only once. "

5
Questions
  • If you gotta ask, youll never know
  • Louis Armstrong Whats Jazz?
  • If you gotta ask, you aint got it
  • Fats Waller Whats rhythm?
  • What questions did you ask today?
  • Arno Penzias

6
Tradeoffs
  • The AB course is about all kinds of tradeoffs
    programming, structural, algorithmic
  • Programming simple, elegant, quick to run/to
    program
  • Tension between simplicity and elegance?
  • Structural how to structure data for efficiency
  • What issues in efficiency? Time, space,
    programmer-time
  • Algorithmic similar to structural issues
  • How do we decide which choice to make, what
    tradeoffs are important?

7
See WordReader.java
  • This reads words, how can we count unique words?
  • WordReader reader new WordReader()
  • ArrayList list new ArrayList()
  • while (reader.hasNext())
  • list.add(reader.next())
  • System.out.println(" words "list.size())
  • What do we know about WordReader?
  • Interfaces implemented?
  • How does it work?

8
Tracking different/unique words
  • We want to know how many times the occurs
  • Do search engines do this? Does the number of
    occurrences of basketball on a page raise the
    priority of a webpage in some search engines?
  • Downside of this approach for search engines?
  • Constraints on solving this problem
  • We must read every word in the file (or web page)
  • We must search to see if the word has been read
    before
  • We must process the word (bump a count, store the
    word)
  • Are there fundamental limits on any of these
    operations? Where should we look for data
    structure and algorithmic improvements?

9
Search measuring performance
  • How fast is fast enough?
  • // post return true if and only if key found
    in a
  • boolean search(String list, String key)
  • for(k0 k lt list.length k)
  • if (ak.equals(key)) return true
  • return false
  • Java details parameters? equals? array code?
  • How do we measure performance of code? Of
    algorithm?
  • Does processor make a difference? PIII, PIV, G4,
    G5, ??

10
Tradeoffs in reading and counting
  • Read words, then sort, determine unique words?
  • frog, frog, frog, rat, tiger, tiger, tiger, tiger
  • If we look up words as we're reading them and
    bump a counter if we find the word, is this
    slower than previous idea?
  • How do we look up word, how do we add word
  • Are there kinds of data that make one approach
    preferable?
  • What is best case, worst case, average case?
  • How do we reason about tradeoffs, what tools
    needed?

11
What is big-Oh about?
  • Intuition avoid details when they dont matter,
    and they dont matter when input size (N) is big
    enough
  • For polynomials, use only leading term, ignore
    coefficients
  • y 3x y 6x-2 y 15x 44
  • y x2 y x2-6x9 y 3x24x
  • The first family is O(n), the second is O(n2)
  • Intuition family of curves, generally the same
    shape
  • More formally O(f(n)) is an upper-bound, when n
    is large enough the expression cf(n) is larger
  • Intuition linear function double input, double
    time, quadratic function double input, quadruple
    the time

12
More on O-notation, big-Oh
  • Big-Oh hides/obscures some empirical analysis,
    but is good for general description of algorithm
  • Allows us to compare algorithms in the limit
  • 20N hours vs N2 microseconds which is better?
  • O-notation is an upper-bound, this means that N
    is O(N), but it is also O(N2) we try to provide
    tight bounds. Formally
  • A function g(N) is O(f(N)) if there exist
    constants c and n such that g(N) lt cf(N) for all
    N gt n

13
Big-Oh calculations from code
  • Search for element in ArrayList
  • What is complexity of code (using O-notation)?
  • What if array doubles, what happens to time?
  • for(int k0 k lt a.size() k)
  • if (a.get(k).equals(target)) return true
  • return false
  • Complexity if we call N times on M-element
    vector?
  • What about best case? Average case? Worst case?

14
Big-Oh calculations again
  • Consider coding problem 2 first element to occur
    3 times
  • What is complexity of code (using O-notation)?
  • for(int k0 k lt a.length k)
  • int count 1
  • for(int j0 j lt k k)
  • if (aj.equals(ak)) count
  • if (count gt 3) return ak
  • return "" // no one on probation
  • What if we initialize counter to 0, loop to lt k
    ?
  • What is invariant describing value stored in
    count?
  • What happens to time if array doubles in size?

15
Big-Oh calculations again
  • Add a new element at front of ArrayList,
    complexity?
  • Only count element assignments, not list growth
  • a.add(0,newElement) // one statement!
  • a.add(newElement) // make room for it
  • for(int ka.size()-2 k gt0 k--)
  • a.set(k1,a.get(k)) // shift right
  • a.set(0,newElement)
  • If we call the code above N times on an initially
    empty vector, whats the complexity using big-Oh?
  • What about growing the ArrayList? How does this
    work?
  • If vector doubles in size? If vector increases by
    one?

16
Some helpful mathematics
  • 1 2 3 4 N
  • N(N1)/2, exactly N2/2 N/2 which is O(N2)
    why?
  • N N N . N (total of N times)
  • NN N2 which is O(N2)
  • N N N . N N N (total of 3N
    times)
  • 3NN 3N2 which is O(N2)
  • 1 2 4 2N
  • 2N1 1 2 x 2N 1 which is O(2N )
  • Impact of last statement on adding 2N1 elements
    to a vector
  • 1 2 2N 2N1 2N2-1 4x2N-1 which is
    O(2N)
  • log(1) log(2) log(3) log(N)
  • log(N!) roughly N log (N)

17
Running times _at_ 106 instructions/sec
N O(log N) O(N) O(N log N) O(N2)
10 0.000003 0.00001 0.000033 0.0001
100 0.000007 0.00010 0.000664 0.1000
1,000 0.000010 0.00100 0.010000 1.0
10,000 0.000013 0.01000 0.132900 1.7 min
100,000 0.000017 0.10000 1.661000 2.78 hr
1,000,000 0.000020 1.0 19.9 11.6 day
1,000,000,000 0.000030 16.7 min 18.3 hr 318 centuries
18
Sequential search revisited
  • What is postcondition of code below? How would it
    be called initially?
  • Another overloaded function search with 2
    parameters?
  • boolean search(String v, int index,
  • String target)
  • if (index gt v.length) return false
  • else if (vindex.equals(target)) return true
  • else return search(v,index1,target)
  • What is complexity (big-Oh) of this function?

19
Recurrences
  • boolean search(String v, int index,
  • String target)
  • if (index gt v.length) return false
  • else if (vindex.equals(target)) return true
  • else return search(v,index1,target)
  • What is complexity? justification?
  • T(n) time to search an n-element array
  • T(n) T(n-1) 1
  • T(0) 1
  • instead of 1, use O(1) for constant time
  • independent of n, the measure of problem size

20
Solving recurrence relations
  • plug, simplify, reduce, guess, verify?
  • T(n) T(n-1) 1
  • T(0) 1
  • T(n) T(n-k) k find the pattern!
  • Now, let kn, then T(n) T(0)n 1n
  • get to base case, solve the recurrence O(n)

T(n-1) T(n-1-1) 1
T(n) T(n-2) 1 1 T(n-2)2
T(n-2) T(n-2-1) 1
T(n) (T(n-3) 1) 1 1 T(n-3)3
21
Consider merge sort for linked lists
  • Given a linked list, we want to sort it
  • Divide the list into two equal halves
  • Sort the halves
  • Merge the sorted halves together
  • Whats complexity of dividing an n-node list in
    half?
  • How do we do this?
  • Whats complexity of merging (zipping) two sorted
    lists?
  • How do we do this?
  • T(n) time to sort n-node list 2 T(n/2) O(n)
    why?

22
sidebar solving recurrence
  • T(n) 2T(n/2) n T(XXX) 2T(XXX/2)
    XXX
  • T(1) 1
  • (note n/2/2
    n/4)
  • T(n) 22T(n/4) n/2 n
  • 4 T(n/4) n n
  • 42T(n/8) n/4 2n
  • 8T(n/8) 3n
  • ... eureka!
  • 2k T(n/2k) kn
  • let 2k n
  • k log n, this yields 2log n T(n/2log n)
    n(log n)
  • n T(1) n(log n)
  • O(n log n)

23
Complexity Practice
  • What is complexity of Build? (what does it do?)
  • ListNode Build(int n)
  • if (0 n) return null
  • ListNode first new ListNode(n,Build(n-1)
    )
  • for(int k 0 k lt n-1 k)
  • first new ListNode(n,first)
  • return first
  • Write an expression for T(n) and for T(0), solve.

24
Recognizing Recurrences
  • Solve once, re-use in new contexts
  • T must be explicitly identified
  • n must be some measure of size of input/parameter
  • T(n) is the time for quicksort to run on an
    n-element vector
  • T(n) T(n/2) O(1) binary search O(
    )
  • T(n) T(n-1) O(1) sequential search O(
    )
  • T(n) 2T(n/2) O(1) tree traversal O(
    )
  • T(n) 2T(n/2) O(n) quicksort O(
    )
  • T(n) T(n-1) O(n) selection sort O(
    )
  • Remember the algorithm, re-derive complexity

log n
n
n
n log n
n2
25
Nancy Leveson Software Safety
  • Founded the field
  • Mathematical and engineering aspects
  • Air traffic control
  • Microsoft word
  • "C is not state-of-the-art, it's only
    state-of-the-practice, which in recent years has
    been going backwards"
  • Software and steam engines once extremely
    dangerous?
  • http//sunnyday.mit.edu/steam.pdf
  • THERAC 25 Radiation machine that killed many
    people
  • http//sunnyday.mit.edu/papers/therac.pdf

26
Java Object Basics
  • Class is an object factory
  • Class can have (static) methods
  • Defines interface programmers depend on
  • Part of inheritance hierarchy (every class is-an
    Object)
  • May define getter and setter methods for
    accessing state
  • Usually defines methods for behavior
  • An object is an instance of a class
  • Attributes aka state, usually private, sometimes
    accessible
  • Behavior invoked by calling methods
  • Allocated by calling new

27
java.util.Collection
  • Interface for collections of objects
  • See API for details, AP subset requires only a
    few of the methods for subinterfaces List and
    Set, and for separate interface Map
  • Which interface and class should be used?
    Tradeoffs.
  • Why choose ArrayList vs LinkedList?
  • Why choose HashSet vs TreeSet?
  • What about classes, interfaces, and methods not
    in the subset?
  • Collection interface compared to Collections
    class
  • Collection.addAll(Collection) // object method
  • Collections.sort(Collection) // static method

28
Collection hierarchy, tradeoffs?
add size iterator
contains remove
listIterator
getFirst getLast addFirst addLast removeFirst remo
veLast
get(int) set(int,o) add(int,o) remove(int)
Collection method Collection/optional
29
Arrays and the AP subset
  • One and two-dimensional arrays in subset
  • Two-dimensional arrays are AB only topic
  • int grid new int610
  • int rows int.length
  • int cols int0.length
  • Initialization in subset, e.g., int list
    1,2,3,4,5
  • No java.util.Arrays methods in subset
  • sort, binarySearch, fill, asList,

30
ArrayList and the AP subset
  • Inheritance hiearchy (List in java.util) is AB
    only
  • Iterator and ListIterator are AB only
  • Downcast from Object to expected type is in
    subset
  • list.add(new String(hello))
  • String s (String) list.get(0)
  • Required methods
  • size(), get(int), set(int, Object),
  • add(Object), add(int, Object), remove(int)
  • NOT required, but very useful
  • remove(Object), addAll(Collection), clear()

31
What is an Iterator?
  • What problems do Iterators address?
  • Access elements independently of implementation
  • Client programs written in terms of generic
    component
  • public void print(Collection c)
  • Iterator it c.iterator()
  • while (it.hasNext())
  • System.out.println(it.next())
  • How do you add all elements of Set to a List?

32
What is an interface?
  • Indication to programmers and code that a class
    implements some specified functions, most likely
    with requirements on behavior
  • Iterator is an interface what do we expect from
    classes that implement the Iterator interface?
  • Comparable are some objects incomparable? Why?
  • Why isnt Equatable an interface? Where is
    .equals() ?
  • A class can implement multiple interfaces
  • Comparable, Cloneable, Tickable,
  • Think twice before developing inheritance
    hierarchy
  • Single inheritance, problems with
    protected/private data

33
What is Comparable?
  • String a hello
  • String b zebra
  • int x a.compareTo(b) // what values assigned?
  • int y b.compareTo(a)
  • int z a.compareTo(a)
  • Contract compareTo() should be consistent with
    equals()
  • Whats the simple way to write equals for
    Comparable?
  • See also java.util.Comparator
  • Not in subset, useful for sorting on other
    criteria

34
TreeNode and Comparable
  • APCS AB TreeNode class stores Objects
  • Why not Comparable objects rather than generic
    ones?
  • When building search trees, what do we do?
  • public TreeNode add(TreeNode root, Object obj)
  • if (root null) return new TreeNode(obj,null,n
    ull)
  • Comparable rootValue (Comparable)
    root.getValue()
  • if (rootValue.compareTo(obj) lt 0)
  • root.setRight(add(root.getRight(),obj))
  • else
  • root.setLeft(add(root.getLeft(), obj))
  • return root

35
Collections and Comparable
  • The TreeSet class only stores Comparable objects
  • Function to add requires Object, why not
    Comparable?
  • public boolean add(Object o)
  • Comparable comp (Comparable) o
  • // more code here
  • The AP PriorityQueue class has Object parameters
  • Priority requires a Comparable object
  • public Object peekMin()
  • // value returned will implement Comparable

36
Who is Alan Perlis?
  • It is easier to write an incorrect program than
    to understand a correct one
  • Simplicity does not precede complexity, but
    follows it
  • If you have a procedure with ten parameters you
    probably missed some
  • If a listener nods his head when you're
    explaining your program, wake him up
  • Programming is an unnatural act
  • Won first Turing award

http//www.cs.yale.edu/homes/perlis-alan/quotes.ht
ml
Write a Comment
User Comments (0)
About PowerShow.com