1 / 24

Compsci 201, Collections, Hashing , Objects

Compsci 201, Collections, Hashing , Objects. Owen Astrachan ola@cs.duke.edu http://bit.ly/201spring19 January 30, 2019. F is for …. Folder aka Directory – where things are stored in Git Function Abstraction – a method in Java. PFLWoJ. Mundane Java-isms

arth
Download Presentation

Compsci 201, Collections, Hashing , Objects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Compsci 201, Collections, Hashing, Objects Owen Astrachan ola@cs.duke.edu http://bit.ly/201spring19 January 30, 2019 Compsci 201, Spring 2019: Collections, Hashing

  2. F is for … • Folder • aka Directory – where things are stored in Git • Function • Abstraction – a method in Java Compsci 201, Spring 2019: Collections, Hashing

  3. PFLWoJ • Mundane Java-isms • From char to autoboxing: primitives • What is this? • Generic classes: ArrayList to HashSet • From Arrays to HashSet to Collections to … • From Object.equals to Object.hashCode • Everything is an Object, what can an object do? Compsci 201, Spring 2019: Collections, Hashing

  4. NBody Review • Class CelestialBody represents Celestial Body • Why do we use myXPos and my… • Review: getters and mutators • Floating point issues, problems, quandaries • When is (a + b) + c != a + (b + c) • When is a/b * c != a*c / b • Watch for this in Gradescope tests!! Compsci 201, Spring 2019: Collections, Hashing

  5. The Object Concept • Every instance variable and every non-static method accessed/called after Object.Dot • b.getX(), b.calcForcExertedBy(other) • From within a class, e.g., CelestialBody • myXPos, getX(),this.myXPos, • All are equivalent as is this.getX() • Some prefer always using this. – clearer? Compsci 201, Spring 2019: Collections, Hashing

  6. Debugging Arithmetic • Order of operations with floating point values can result in overflow, underflow, more • Small number + Big number … Compsci 201, Spring 2019: Collections, Hashing

  7. Debugging double Arithmetic • Integer values are not the same as Double values • 1/0 is … whereas 1.0/0 is … Compsci 201, Spring 2019: Collections, Hashing

  8. WOTO http://bit.ly/201spring19-jan25-2 Compsci 201, Spring 2019: Collections, Hashing

  9. Donald Knuth • aka “The Donald” • Turing award (and others) • Author of “The Art of Computer Programming” • Arguably most important book written in Computer Science • First publication: Mad Magazine If you optimize everything you will always be unhappy. Everyday life is like programming, I guess. If you love something you can put beauty into it. https://www.youtube.com/watch?v=cK7yyjXfbc4 Compsci 201, Spring 2019: Collections, Hashing

  10. From Array to ArrayList • Have int[], String[], CelestialBody[] • Array of any type, but doesn't grow • Can't use .contains with array, can't print • The java.utils.Arrays class has some help Compsci 201, Spring 2019: Collections, Hashing

  11. java.util.ArrayList • Growable array with many useful methods • https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/List.html • Can only contain Object types (no primitives) • Convert from array? • Arrays.asList • It's a List! • String yes, int no Compsci 201, Spring 2019: Collections, Hashing

  12. From Array to ArrayList • Can make conversion with Object, e.g., String • Use Arrays.asList as a bridge, be careful Compsci 201, Spring 2019: Collections, Hashing

  13. Primitive Array? do it yourself • No bridge from Arrays.asList since primitive • Loop and use autoboxing/unboxing • Conversion of int to Integer and vice versa Compsci 201, Spring 2019: Collections, Hashing

  14. -- real world APT? • https://leetcode.com/problems/unique-morse-code-words/ • "a" > ".-", "b" > "-..." • "z" > "--.." • Note "gin" > "--...-." and "zen" > "--...-." • Given an array of strings, how many unique encodings are there? • Also given String[] of 26 Morse codes, where code[0] = ".-" for "a" Compsci 201, Spring 2019: Collections, Hashing

  15. From Nothing to Done • Basic ideas: how do we access encodings in an array where code[1] is for 'b', "-…" • Arithmetic with char values, 'b' – 'a' == 1 • What about (int) 'b' == 97? • https://www.youtube.com/watch?v=xLpfbcXTeo8 • Loop over characters in a String? • Index k with s.charAt(k) • Or for(char ch : s.toCharArray()){ Compsci 201, Spring 2019: Collections, Hashing

  16. WOTO with Live/Leet Code • Ideas for solving LeetCode problem • Given array of Strings, return number of unique Morse code encodings • How is a set useful here? Doable without? Compsci 201, Spring 2019: Collections, Hashing

  17. Solution • This was added after class, developed together during class Compsci 201, Spring 2019: Collections, Hashing

  18. ArrayList<…> • Generic aka parameterized type • Any Object subtype can be in ArrayList<..> • Integer, Double, Char, Boolean are wrapper classes for primitives • Mostly these work. But immutable. Cannot increment an Integer, can create new one Compsci 201, Spring 2019: Collections, Hashing

  19. DIYAD ArrayList • Do It Yourself Algorithm and Datastructure • SimpleStringArrayList: some methods • GrowableStringArrayList: more methods • Differences between +10, +100, and *2 • Helper methods are private: checkSize() • https://coursework.cs.duke.edu/201spring19/diyad201 Compsci 201, Spring 2019: Collections, Hashing

  20. DIYAD Ideas • Move from String to GrowableString to Generic • Lots of work to fit in with Collections hierarchy • For our own work? Easier! All of Java? Harder! • Differences between +10, +10000, *2 and * 1.2 • How do we measure empirically • How do we measure analytically Compsci 201, Spring 2019: Collections, Hashing

  21. Measurement and Analysis • We measured runtimes empirically • Same on ola's laptop tomorrow? Next year? • What about your computer, super computer? • Mathematical analysis of runtimes • Machine independent • Compare algorithms without timing them! Compsci 201, Spring 2019: Collections, Hashing

  22. Analysis via Pictures • Growing array by adding +1 (or + 100) each time • Create/copy 1, 2, 3, 4, …, N • 1+2+ … + N = N(N+1)/2 • Roughly N2 • Square with side N? Compsci 201, Spring 2019: Collections, Hashing

  23. Analysis via Pictures Again • Growing array by doubling each time • Create/copy 1, 2, 4, 8, 16, … 2N • If X = 2N, we've created 2x2N-1, or 2X-1 • Roughly X, where "roughly" defined later Compsci 201, Spring 2019: Collections, Hashing

  24. WOTO http://bit.ly/201spring19-jan30-1 Compsci 201, Spring 2019: Collections, Hashing

More Related