Download presentation
Presentation is loading. Please wait.
1
Compsci 201, Analysis+Markov
Owen Astrachan Jeff Forbes September 22, 2017 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
2
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
3
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
4
Redesign for Efficiency
Consider APT anonymous Green solution 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
5
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
6
Using Numbers to Understand
Is 26M + Hsize better than 26*Hsize*M ? Suppose we have 100 messages with 1,000 characters in headlines ____ 2,600,000 Change with 10,000 characters in headlines ,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
7
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
8
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
9
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
10
Compsci 201, Fall 2017, Analysis+Markov
WOTO 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
11
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
12
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
13
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
14
Not Everything is Comparable
9/22/17 Compsci 201, Fall 2017, Analysis+Markov
15
Compsci 201, Fall 2017, Analysis+Markov
(x,y) < (z,w) Can we compare Point objects? To be “comparable”, implement the Comparable interface, supply .compareTo(..) method 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
16
Compsci 201, Fall 2017, Analysis+Markov
Points We’ll look at this to explore concepts in Java and Software Design 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
17
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
18
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
19
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
20
Compsci 201, Fall 2017, Analysis+Markov
Design, Build, Run Tests 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
21
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
22
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
23
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
24
Compsci 201, Fall 2017, Analysis+Markov
Markov Assignment Markov Models are used in many applications/areas 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
25
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
26
Compsci 201, Fall 2017, Analysis+Markov
Why use an interface? 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
27
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() 9/22/17 Compsci 201, Fall 2017, Analysis+Markov
28
Compsci 201, Fall 2017, Analysis+Markov
Developer Time Making changes in timeSet and timeUtilSet 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
29
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
30
Compsci 201, Fall 2017, Analysis+Markov
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
31
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
32
Compsci 201, Fall 2017, Analysis+Markov
Power and Simplicity 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
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.