CompSci 100 2.1 Toward understanding data structures  What can be put in a TreeSet?  What can be sorted?  Where do we find this information?  How do.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
1 Java Object Model Part 2: the Object class. 2 Object class Superclass for all Java classes Any class without explicit extends clause is a direct subclass.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
CPS 108 : Spring From C++ to Java l Java history: Oak, toaster-ovens, internet language, panacea  Not really a standard language like C++  Arguably.
CompSci 100E 7.1 Solving Problems: Anagrams/Jumbles  How do humans solve puzzles like that at  Is it important to get computers.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
CompSci 100 Prog Design and Analysis II Sept 7, 2010 Prof. Rodger 1CompSci 100 Fall 2010.
1 CSC 321: Data Structures Fall 2013 HW1 autopsy 1.parallel lists 2.Comparable Word objects 3.Comparators 4.MVC pattern.
Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Data Structures and Java CS 105. L7: Java Slide 2 Data structure Data structure defined: A systematic way of organizing and accessing data Examples Dictionary:
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
Compsci 100, Fall Data and Information How and why do we organize data? Differences between data and information? What about knowledge?
Genome Revolution: COMPSCI 006G 2.1 Control in Java and Programming l The computer executes your program one statement at a time, from top to bottom within.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism.
Some Standard Classes Goals The Object class The String class Wrapper classes The Math class Random Numbers.
Types in programming languages1 What are types, and why do we need them?
Project JETT/Duke 1 Project JETT Java Engagement for Teacher Training Duke University Department of Computer Science Association for Computing Machinery.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
CPS 100, Spring Data and Information How and why do we organize data? Differences between data and information? What about knowledge?
Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based.
CPS APTs and structuring data/information l Is an element in an array, Where is an element in an array?  DIY: use a loop  Use Collections, several.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
CPS What's a pointer, why good, why bad? l Pointer is a memory address, it's an indirect reference to memory or an object.  Rather than say we.
CPS 100e 5.1 Inheritance and Interfaces l Inheritance models an "is-a" relationship  A dog is a mammal, an ArrayList is a List, a square is a shape, …
CPS What's in Compsci 100? l Understanding tradeoffs: reasoning, analyzing, describing…  Algorithms  Data Structures  Programming  Design l.
Building Java Programs Generics, hashing reading: 18.1.
Java: Base Types All information has a type or class designation
Java Arrays Fixed size, once created
Data and Information How and why do we organize data? Differences between data and information? What about knowledge?
Arrays.
Java: Base Types All information has a type or class designation
CSC 222: Object-Oriented Programming
Lecture 5: Some more Java!
ADT’s, Collections/Generics and Iterators
Anagrams/Jumbles How do humans solve puzzles like that at
Compsci 201 Priority Queues & Autocomplete
Java Review: Reference Types
Wrapper Classes ints, doubles, and chars are known as primitive types, or built-in types. There are no methods associated with these types of variables.
Efficiency add remove find unsorted array O(1) O(n) sorted array
Interfaces and Inheritance
Building Java Programs
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
int [] scores = new int [10];
Fall 2018 CISC124 12/3/2018 CISC124 or talk to your grader with questions about assignment grading. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod.
CSE 373: Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
Barb Ericson Georgia Institute of Technology Oct 2005
int [] scores = new int [10];
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
Object-Oriented Programming
Java Programming Language
Amortized Analysis and Heaps Intro
Review: libraries and packages
2009 Test Key.
CMPE212 – Reminders Assignment 2 due next Friday.
Java String Class String is a class
Review for Midterm 3.
Classes and Objects Object Creation
Java Basics – Arrays Should be a very familiar idea
CMSC 202 Constructors Version 9/10.
Presentation transcript:

CompSci Toward understanding data structures  What can be put in a TreeSet?  What can be sorted?  Where do we find this information?  How do we understand the information?  What can be put in an ArrayList? Why is this different?  What operations exist on an ArrayList?  What about an array, or operations done on an ArrayList as opposed to what an ArrayList does to itself?

CompSci What can an Object do (to itself)?   Look at java.lang.Object  toString()  Used to print (System.out.println) an object, overriding toString() can result in 'useful' information being printed, also used in String concatenation: String s = x + y;  Default is basically a pointer-value  equals()  Determines if guts of two objects are the same, must override, e.g., for using a.indexOf(o) in ArrayList a  Default is ==, pointer equality  hashCode()  Hashes object (guts) to value for efficient lookup

CompSci Objects and values  Primitive variables are boxes  think memory location with value  Object variables are labels that are put on boxes String s = new String("genome"); String t = new String("genome"); if (s == t) {they label the same box} if (s.equals(t)) {contents of boxes the same} s t What's in the boxes? "genome" is in the boxes

CompSci Objects, values, classes  For primitive types: int, char, double, boolean  Variables have names and are themselves boxes (metaphorically)  Two int variables assigned 17 are equal with ==  For object types: String, Sequence, others  Variables have names and are labels for boxes  If no box assigned, created, then label applied to null  Can assign label to existing box (via another label)  Can create new box using new  Object types are references or pointers or labels to storage

CompSci What about a 'struct' (plain old data)  We use classes, data/state is private, methods are public  Why do we have rules? When can they be broken?  Why are there both structs and classes in C++?  What about helping class, e.g., word and frequency together?  We can have one class nested in another, then we don't have to worry so much about encapsulation  See recitation example for creating a Class that can be compared using equality and can be sorted  Comparable interface must be symmetric with.equals  What happens if this isn't the case?

CompSci Brute force? SillyAnagrams.java public String[] allAnagrams(String s) { int anaCount = factorial(s.length()); Set anagrams = new TreeSet(); ArrayList list = new ArrayList(); for(int k=0; k < s.length(); k++){ list.add(s.substring(k,k+1)); } while (anagrams.size() != anaCount){ Collections.shuffle(list); anagrams.add(listToString(list)); } return (String[]) anagrams.toArray(new String[0]); }

CompSci Quantifying brute force for anagrams  All anagrams of "compute" takes average of 1 second over 20 trials. How long will "computer" take? Why?  What is worst case time?  What is best case time?  We’re willing to do some pre-processing to make the time to find anagrams quicker  Often find that some initialization/up-front time or cost saves in the long run  What properties do words share that are anagrams?

CompSci John von Neumann “Anyone who attempts to generate random numbers by deterministic means is, of course, living in a state of sin.” “There's no sense in being precise when you don't even know what you're talking about. “ “There are two kinds of people in the world: Johnny von Neumann and the rest of us.” Eugene Wigner, Noble Physicist

CompSci Toward a faster anagram finder  Words that are anagrams have the same letters; use a letter fingerprint or signature/histogram to help find anagrams  Count how many times each letter occurs: “teacher” “cheater”  Store words, but use fingerprint for comparison when searching for an anagram  How to compare fingerprints using.equals()  How to compare fingerprints using.compareTo()  How do we make client programmers unaware of fingerprints? Should we do this?

CompSci Another anagram method  Instead of fingerprint/histogram idea, use sorted form of word  “gable” and “bagel” both yield “abegl”  Anagrams share same sorted form  Similarities/differences to histogram/fingerprint idea?  Both use canonical or normal/normalized form  Normalized form used for comparison, not for printing  When should this normal form be created?  When is one method preferred over the other?  Big words, little words? Different alphabets? DNA vs English?

CompSci OO and Java  We’ll use an adapter or wrapper class called Anaword instead of String  Clients can treat Anaword objects like strings, but the objects are better suited for finding anagrams than strings  The Anaword for “bear” prints as “bear” but compares to other Anaword objects as  In Java change behavior with.toString() and.equals()  No overloaded operators as in C++ o Exception is +, this works for strings, but can't change it  When string needed, automatically call toString()

CompSci Understandable, extensible?  The code does things simply, but isn't very OO. Why is simple sometimes better? Why is it worse? void printAll(Anaword[] list, Anaword target) { System.out.print("anagrams of "+target+": "); for(int k=0; k < list.length; k++){ if (target.equals(list[k])) { System.out.print(list[k]); } System.out.println(); }

CompSci Find all anagrams in dictionary  If we sort the dictionary what will happen to the anagrams?  capitol optical topical  danger gander garden ranged  lameness maleness nameless salesmen  How can we overload.equals()?  Look at "danger" or ….  How can we sort with Collections.sort or Arrays.sort  Elements sorted must be comparable/sortable  Must implement the java.lang.Comparable interface o Return negative, zero, positive number depending on less than, equal to, or greater than o What is method signature?

CompSci Anaword objects with options  Can we use different canonical forms in different contexts?  Could have Anaword, FingerPrintAnaword, SortAnaword  What possible issues arise? What behavior is different in subclasses? o If there’s no difference in behavior, don’t have subclasses  Alternative, make canonical/normalize method a class  Turn a function/idea into a class, then let the class vary to encapsulate different methods  Normalization done at construction time or later  Where is normalizer object created? When?

CompSci Anagram: Using Normalizers  How can we normalize an Anaword object differently?  Call normalize explicitly on all Anaword objects  Have Anaword objects normalize themselves  Advantages? Disadvantages?  If Anaword objects normalize themselves, how can we experiment with different normalization techniques?  Cut and paste. Problems? Versions? Saved code?  What about using save-as and several.java files?  What about deciding at runtime on normalization?  We need inheritance!

CompSci Normalizer hierarchy  Anaword objects normalize themselves  Where does the normalizer come from? o Passed in at construction time o Obtained from normalizer factory o Other approaches?  How is Normalizer used?  Normalizer is conceptually an interface  Different implementations of the interface have different behavior (guts) but same skin (sort of)