Compsci 201, Analysis+Markov

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
CSE 373 Data Structures and Algorithms Lecture 18: Hashing III.
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
Chapter 19 Java Data Structures
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Recitation 4 Abstract classes, Interfaces. A Little More Geometry! Abstract Classes Shape x ____ y ____ Triangle area() base____ height ____ Circle area()
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
Chapter 18 Java Collections Framework
CompSci 100E Functions (Static Methods)  Java function.  Takes zero or more input arguments.  Returns one output value.  Applications.  Scientists.
Comparable and Comparator Nuts and Bolts. Sets A set is a collection in which all elements are unique—an element is either in the set, or it isn’t In.
Project JETT/Duke 1 Project JETT Java Engagement for Teacher Training Duke University Department of Computer Science Association for Computing Machinery.
Compsci 101.2, Fall Plan for eleven-four l Thinking about APTs and test problems  How do you choose: list, string, set, dictionary  Experience?
CompSci Inheritance and Interfaces  Inheritance models an "is-a" relationship  A dog is a mammal, an ArrayList is a List, a square is a shape,
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
Interfaces, Abstract Classes, and Polymorphism. What Is an Interface? An interface is the set of public methods in a class Java provides the syntax for.
CompSci 100 Prog Design and Analysis II Sept 14, 2010 Prof. Rodger CompSci 100, Fall
Interfaces. In order to work with a class, you need to understand the public methods  methods, return types,…  after you instantiate, what can you do.
CPS What's in Compsci 100? l Understanding tradeoffs: reasoning, analyzing, describing…  Algorithms  Data Structures  Programming  Design l.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
1 clone() Defined in Object Creates an identical copy –Copies pointers to fields (does not copy fields of fields) –Makes a shallow copy if the object’s.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
CompSci 101 Introduction to Computer Science March 29, 2016 Prof. Rodger.
CompSci Toward understanding data structures  What can be put in a TreeSet?  What can be sorted?  Where do we find this information?  How do.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Lecture 5:Interfaces and Abstract Classes
Slides by Donald W. Smith
Jeff Forbes Owen Astrachan September 8, 2017
Using the Java Collection Libraries COMP 103 # T2
Compsci 201, Compare+Analysis
Sixth Lecture ArrayList Abstract Class and Interface
CompSci 101 Introduction to Computer Science
Chapter 19 Java Data Structures
Wednesday Notecards. Wednesday Notecards Wednesday Notecards.
ADT’s, Collections/Generics and Iterators
Plan for the Week Review Big-Oh
TAFTW (Take Aways for the Week)
Compsci 201 Priority Queues & Autocomplete
Week 2: 10/1-10/5 Monday Tuesday Wednesday Thursday Friday
Road Map CS Concepts Data Structures Java Language Java Collections
Java Collections Overview
Polymorphism 11-Nov-18.
Object Oriented Programming (OOP) LAB # 8
Part of the Collections Framework
Polymorphism 28-Nov-18.
CompSci 101 Introduction to Computer Science
CSE 1030: Implementing Non-Static Features
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
Lecture 18: Polymorphism (Part II)
Polymorphism 21-Apr-19.
slides created by Marty Stepp
Compsci 201 Binary Trees Recurrence Relations
Chapter 11 Inheritance and Polymorphism Part 2
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
Compsci 201, Collections, Hashing, Objects
CSE 143 Lecture 23 Inheritance and the Object class; Polymorphism
CMPE212 – Reminders Assignment 2 due next Friday.
Java String Class String is a class
Compsci 201, O-Notation and Maps (Interfaces too)
Practical Session 3 Java Collections
What can you put into an ArrayList?
Presentation transcript:

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