Compsci 201, Analysis+Markov Owen Astrachan Jeff Forbes September 22, 2017 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
Compsci 201, Fall 2017, Analysis+Markov H is for … Hashing What better way to have a bucket list? HTTP A protocol we use every day HackDuke Oct 28/29, hackduke.org Hexadecimal 0x001A57 is Duke Blue 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
Compsci 201, Fall 2017, Analysis+Markov Plan for the Day Review Anonymous APT Motivate Maps + Efficiency, prelude to Markov Reminder of APTs, policies, APT Quizzes Comparing elements, toward sorting and trees Efficiency has a cost, when is it “worthwhile”? Background for Markov: JUnit + Inheritance 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
Redesign for Efficiency Consider APT anonymous Green solution http://bit.ly/201fall17-anon for each message: If count(message,’a’) <= count(headline,’a’) OK Repeat for ‘b’, ‘c’, …, ‘z’ Rescan the headline text for ‘a’, … ‘z’ AND for every message Is this a concern? 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
Compsci 201, Fall 2017, Analysis+Markov Quantifying Concerns Counting # occurrences of ‘a’ in headlines requires looking at all Hsize characters We do this 26 times, that’s 26* Hsize (why) We do this for M messages, that’s 26*Hsize*M Also 26*Msize to count chars in each message If we scan headline once to create map: Hsize Process M messages is then 26*M + 26Msize Is 26M + Hsize better than 26*Hsize*M ? 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
Using Numbers to Understand Is 26M + Hsize better than 26*Hsize*M ? Suppose we have 100 messages with 1,000 characters in headlines 2600 + 1000 ____ 2,600,000 Change with 10,000 characters in headlines 2600 + 10,000 ___ 26,000,000 If M or Hsize is constant, how does time change? Is M + N better than MN? 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
Compsci 201, Fall 2017, Analysis+Markov Making Maps In discussion saw int[] counters[256] Can use Map<Character,Integer> to for(char ch : str.toCharArray()){ counters[ch] += 1; } for(char ch : str.toCharArray()){ if (! map.containsKey(ch)) map.put(ch,0); map.put(ch, map.get(ch) + 1); } 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
Compsci 201, Fall 2017, Analysis+Markov Which map is best? We can use both array and Map because char acts like an int The “key” in some contexts won’t be an index 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
Refactoring Anonymous We had an all-green solution and we changed it!? Didn't change what the code did Did change how the code worked/performance Refactoring: don't add functionality! Correct code might break, tests to the rescue! 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
Compsci 201, Fall 2017, Analysis+Markov WOTO http://bit.ly/201fall17-sept22-1 Naïve or simple algorithms work well for small problems “We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%” Donald Knuth Structured Programming with go to Statements". ACM Computing Surveys. 6 (4): 268 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
Compsci 201, Fall 2017, Analysis+Markov Donald Knuth aka “The Donald” Turing, Hopper, von Neumman awards Author of “The Art of Computer Programming” Arguably most important book written in Computer Science First publication: Mad Magazine (author of TeX) “I can’t go to a restaurant and order food because I keep looking at the fonts on the menu.” 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
Compsci 201, Fall 2017, Analysis+Markov Comparing and Sorting Arrays.sort, Collections.sort {“ant”, “bat”, “cat”, “dog”} What algorithm is used in sorting? How to change to sort-in-reverse or other order Java 8 API helps here (study on your own) TreeSet and TreeMap Require comparisons 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
Strings are Comparable Compare strings lexicographically, natural ordering, dictionary order “zebra” > “aardvark” but “Zebra” < “aardvark” Conceptual, cannot use < or > or == “yak”.compareTo(s) returns < 0, == 0, > 0 s is “zebra”, “yak”, and “toad”, respectively The int convention also used in C++, C, others 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
Not Everything is Comparable 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
Compsci 201, Fall 2017, Analysis+Markov (x,y) < (z,w) Can we compare Point objects? http://stackoverflow.com/questions/5178092/sorting-a-list-of-points-with-java https://stackoverflow.com/questions/6886836/can-i-use-the-operator-to-compare-point-objects-in-java To be “comparable”, implement the Comparable interface, supply .compareTo(..) method 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
Compsci 201, Fall 2017, Analysis+Markov Points http://bit.ly/201fall17-pointcode We’ll look at this to explore concepts in Java and Software Design 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
Compsci 201, Fall 2017, Analysis+Markov Build on What You Know How does .equals work? Make sure you have the correct type Cast, compare public boolean equals(Object o) { if (o == null || ! (o instanceof Point)) { return false; } Point p = (Point) o; return p.x == x && p.y == y; 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
Compsci 201, Fall 2017, Analysis+Markov Extend what you know This is method in Point class Point implements Comparable<Point> public int compareTo(Point p) { if (this.x < p.x) return -1; if (this.x > p.x) return 1; if (this.y < p.y) return -1; if (this.y > p.y) return 1 return 0; } 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
Developing and Testing How did we test the Planet class? How do we test APTs? Who writes the tests, how do we run them? Unit test: single method, one step in development JUnit is common library (also other languages) Goal: all green, but red expected as you go! 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
Compsci 201, Fall 2017, Analysis+Markov Design, Build, Run Tests http://bit.ly/201fall17-pointcode 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
Compsci 201, Fall 2017, Analysis+Markov Adding Tests We will test compareTo What are some good tests here How will we implement them What are some JUnit conventions @Test public void testMethod assertEquals 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
Compsci 201, Fall 2017, Analysis+Markov Shafi Goldwasser 2012 Turing Award Winner RCS professor of computer science at MIT Twice Godel Prize winner Grace Murray Hopper Award National Academy Co-inventor of zero-knowledge proof protocols Work on what you like, what feels right, I now of no other way to end up doing creative work 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
Compsci 201, Fall 2017, Analysis+Markov Pause and Reflect Ask someone near you that you don't know what their name is and where they are from Move if you have to What's the purpose of in-class WOTO questions? 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
Compsci 201, Fall 2017, Analysis+Markov Markov Assignment https://en.wikipedia.org/wiki/Infinite_monkey_theorem Markov Models are used in many applications/areas 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
What is a Java Interface? An enforceable abstraction: methods required Set and Map interfaces Comparable interface If Set<String> is parameter then can pass … HashSet<String> or TreeSet<String> Can sort String or anything that’s Comparable Call .compareTo(..) method 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
Compsci 201, Fall 2017, Analysis+Markov Why use an interface? 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
Compsci 201, Fall 2017, Analysis+Markov Why use an Interface? Work with frameworks, e.g., java.util.Collection Iterable, Serializable, and more – use with Java ArrayList, LinkedList, TreeSet, HashSet all … .clear(), .contains(o), .addAll(..), .size(), … .toArray() https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
Compsci 201, Fall 2017, Analysis+Markov Developer Time Making changes in timeSet and timeUtilSet http://bit.ly/201fall17-setcode Change the call, don’t change method: util TreeSet, HashSet implement same interface Change the call, change method signature Same methods: ArraySet, SimpleHashSet, but no common interface explicit 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
Compsci 201, Fall 2017, Analysis+Markov Concept: Inheritance In Java, every class extends Object Gets methods by default: .toString, .hashCode, .equals, and more Subclass can override baseclass methods Make .equals work for String 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
Compsci 201, Fall 2017, Analysis+Markov http://bit.ly/201fall17-shapes Shape is a base class We'll show Rectangle and Circle subclasses We can add new classes without recompiling or touching existing and tested classes Subclasses can access protected data/methods Cannot access private state/behavior 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
Compsci 201, Fall 2017, Analysis+Markov MakeShapes.java public class MakeShapes { public static void main(String[] args) { ArrayList<Shape> shapes = new ArrayList<>(); shapes.add(new Rectangle(3,5)); shapes.add(new Circle(1.0)); shapes.add(new Rectangle(5,3)); for(Shape s : shapes) { System.out.printf("%s\t%1.3f\n", s,s.area()); } Inheritance models "is-a" relationships 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
Compsci 201, Fall 2017, Analysis+Markov Power and Simplicity Use @Override when changing a method from parent class Helps compiler and developers Java isn't the only Object Oriented language Swift, Objective-C, C++, C#, Ruby, Python, .. Java requires OO, not all languages do 9/22/17 Compsci 201, Fall 2017, Analysis+Markov