DOC PREVIEW
Duke CPS 100E - Functions

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CompSci 100E 3.1 1 Functions (Static Methods) ! Java function. " Takes zero or more input arguments. " Returns one output value. ! Applications. " Scientists use mathematical functions to calculate formulas. " Programmers use functions to build modular programs. " You use functions for both. ! Examples. " Built-in functions: Math.random(), Math.abs(), Integer.parseInt(). " Our I/O libraries StdDraw.show(), StdAudio.play(). " User-defined functions: main(). CompSci 100E 3.2 2 Anatomy of a Java Function ! Java functions. Easy to write your own. f(x) = !x!input 2.0 1.414213… output CompSci 100E 3.3 3 Flow of Control ! Flow of control. Functions provide a new way to control the flow of execution of a program. "pass-by-value" CompSci 100E 3.4 4 Libraries ! Library. A module whose methods are primarily intended for use by many other programs. ! Client. Program that calls a library. ! API. Contract between client and implementation. ! Implementation. Program that implements the methods in an API.CompSci 100E 3.5 5 Modular Programming ! Modular programming. " Divide program into self-contained pieces. " Test each piece individually. " Combine pieces to make program. ! Ex. Flip N coins. How many heads? " Read arguments from user. " Flip one fair coin. " Flip N fair coins and count number of heads. " Repeat simulation, counting number of times each outcome occurs. " Plot histogram of empirical results. " Compare with theoretical predictions. CompSci 100E 3.6 Flashback: Data processing ! Scan a large (~ 107 bytes) file ! Print the words together with counts of how often they occur ! Need more specification? ! How do you do it? ! What is we only wanted the top k (say 20) words? CompSci 100E 3.7 What can you put into an ArrayList? ! Any Object ! Use a wrapper class (see java.lang.*) " int, double, char, boolean, … " Integer, Double, Character, Boolean, ! Can have your cake and eat it too ArrayList<Integer> list = new ArrayList<Integer>(); for (int k = 0; k < 10; k++){ list.add(k*k); } for (Integer jj : list){ System.out.println(jj); } ! All made practical by Version 5 of Java CompSci 100E 3.8 Exploring ArrayLists ! Look at the Java 6 API ! Note interfaces implemented " Serializable, Cloneable, Iterable " Collection, List, RandomAccess ! Note other descriptive text " Regarding performance " Constructors " Methods " Don’t forget methods in parent classesCompSci 100E 3.9 Exploring ArrayLists ! Some Commonly Used Methods " boolean add(E o) // append " void add(int index, E element) // insert " void clear() " boolean contains(Object elem) " E get(int index) " int indexOf(Object elem) " boolean remove(Object o) " E remove(int index) " E set(int index, E elem) // replace " int size() CompSci 100E 3.10 Exploring ArrayLists ! Performance " Constant Time # size, isEmpty, get, set, iterator, listIterator operations # add (amortized) " Linear Time # All of the other operations run in linear time " What does all of this mean? " Why do we care? " Exercise: Implement on an array the equivalent of # void add(int index, E element) # E remove(int index) " Remember: Memory is an array (well sort of) CompSci 100E 3.11 What is a char? ! Differences between unicode and ASCII " Why is unicode used? Why should we care? What should we know? How many of the details are important? ! A char value can be treated like an int value " Add integer to it, cast back to char " Subtract character from it, get int back counters[s.charAt(k)- ’A’]++; " Anatomy of the statement above?? CompSci 100E 3.12 Inheritance and Interfaces ! Inheritance models an "is-a" relationship " A dog is a mammal, an ArrayList is a List, a square is a shape, … ! Write general programs to understand the abstraction, advantages? void execute(Pixmap target) { // do something } ! But a dog is also a quadruped, how can we deal with this?CompSci 100E 3.13 Single inheritance in Java ! A class can extend only one class in Java " All classes extend Object --- it's the root of the inheritance hierarchy tree " Can extend something else (which extends Object), why? ! Why do we use inheritance in designing programs/systems? " Facilitate code-reuse (what does that mean?) " Ability to specialize and change behavior o If I could change how method foo() works, bar() is ok " Design methods to call ours, even before we implement o Hollywood principle: don't call us, … CompSci 100E 3.14 Comparable and Comparator ! Both are interfaces, there is no default implementation " Contrast with .equals(), default implementation? " Contrast with .toString(), default? ! Where do we define a Comparator? " In its own .java file, nothing wrong with that " Private, used for implementation and not public behavior o Use a nested class, then decide on static or non-static o Non-static is part of an object, access inner fields ! How do we use the Comparator? " Sort, Sets, Maps (in the future) ! Does hashing (future topic) have similar problems? CompSci 100E 3.15 Sets ! Set is an unordered list of items " Items are unique! Only one copy of each item in set! ! We will use two different implementations of sets ! TreeSet " A TreeSet is backed up by a tree structure (future topic) " Keeps items sorted (+) " Slower than HashSets ?? (-) ! HashSet " A HashSet is backed up by a hashing scheme (future topic) " Items not sorted – should seem to be in random order (-) " Faster than TreeSets ?? (+) CompSci 100E 3.16 Using Both ArrayList and Sets ! You may want to use a set to get rid of duplicates, then put the items in an ArrayList and sort them! ! Problem: " Often data comes in the form of an array " How do we go from array to ArrayList or TreeSet? ! Problem: " Often we are required to return an array " How do we go from a Collection such as an ArrayList or TreeSet to an array? ! Can do it the “hard” way with loops or iterators: " one item at a time !


View Full Document

Duke CPS 100E - Functions

Documents in this Course
Topics

Topics

9 pages

Lecture

Lecture

3 pages

Notes

Notes

2 pages

Hashing

Hashing

19 pages

Lecture

Lecture

59 pages

Lecture

Lecture

6 pages

Lecture

Lecture

4 pages

Lecture

Lecture

20 pages

Lecture

Lecture

12 pages

Lecture

Lecture

12 pages

Lecture

Lecture

7 pages

Lecture

Lecture

8 pages

Lecture

Lecture

10 pages

Lecture

Lecture

4 pages

Notes

Notes

16 pages

Lecture

Lecture

5 pages

Lecture

Lecture

9 pages

Lecture

Lecture

4 pages

Lecture

Lecture

13 pages

Lecture

Lecture

6 pages

Lecture

Lecture

16 pages

Lecture

Lecture

5 pages

Lecture

Lecture

5 pages

Lecture

Lecture

12 pages

Lecture

Lecture

12 pages

Lecture

Lecture

10 pages

Sets

Sets

14 pages

Lecture

Lecture

9 pages

Lecture

Lecture

4 pages

Test 1

Test 1

7 pages

Load more
Download Functions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Functions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Functions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?