1 CSC 321: Data Structures Fall 2013 HW1 autopsy 1.parallel lists 2.Comparable Word objects 3.Comparators 4.MVC pattern.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 5 The Collections API.
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Section 2.5 Single-Linked Lists. A linked list is useful for inserting and removing at arbitrary locations The ArrayList is limited because its add and.
Interfaces CSC 171 FALL 2004 LECTURE 14. Project 1 review public class Rational { private int numerator, denominator; public Rational(int numerator, int.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Dictionaries Chapter Chapter Contents Specifications for the ADT Dictionary Entries and methods Using the ADT Dictionary English Dictionary Telephone.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary trees Reading: L&C 9.1 – 9.7.
1 Lists A List ADT Types of Lists Lists in Java Collections API Using ordered lists – Tournament Maker Using indexed lists – Josephus Problem Implementing.
CSE 143 Lecture 17 Binary Search Trees and Comparable slides created by Ethan Apter
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,
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Chapter 19 Java Data Structures
1 CSC 222: Computer Programming II Spring 2005 Searching and efficiency  sequential search  big-Oh, rate-of-growth  binary search  example: spell checker.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 trees,
Iterator COMP 401, Spring 2013 Lecture 07 1/31/2013.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Abstract Data Types (ADTs) Data Structures The Java Collections API
Chapter 9 Domain Models. Domain Model in UML Class Diagram Notation A “visual dictionary”
Chapter 7 Designing Classes. Class Design When we are developing a piece of software, we want to design the software We don’t want to just sit down and.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.
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.
1 CSC 222: Computer Programming II Spring 2005 Stacks and recursion  stack ADT  push, pop, peek, empty, size  ArrayList-based implementation, java.util.Stack.
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,
1 CSC 222: Object-Oriented Programming Spring 2013 Java interfaces & polymorphism  Comparable interface  defining an interface  implementing an interface.
1 CSC 222: Object-Oriented Programming Spring 2012 Searching and sorting  sequential search  algorithm analysis: big-Oh, rate-of-growth  binary search.
1 CSC 221: Computer Programming I Spring 2010 interaction & design  modular design: roulette game  constants, static fields  % operator, string equals.
1 CSC 222: Object-Oriented Programming Spring 2012 recursion & sorting  recursive algorithms  base case, recursive case  silly examples: fibonacci,
1 CSC 222: Object-Oriented Programming Spring 2012 Object-oriented design  example: word frequencies w/ parallel lists  exception handling  System.out.format.
1 CSC 221: Computer Programming I Spring 2008 ArrayLists and arrays  example: letter frequencies  autoboxing/unboxing  ArrayLists vs. arrays  example:
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
Chapter 18 Java Collections Framework
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
1 CSC 221: Computer Programming I Fall 2004 ArrayLists and OO design  ArrayList summary  example: word frequencies  object-oriented design issues cohesion.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
1 CSC 222: Computer Programming II Spring 2005 Java interfaces & polymorphism  Comparable interface  defining an interface  implementing an interface.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
Arrays and Objects CIS 362. The familiar Employee class.
COMP 121 Week 8: Generic Collections. Objectives To understand type variables and how they are used in generic programming To be able to implement and.
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
Take-Home Lab #02 CS1020 – DATA STRUCTURES AND ALGORITHMS 1 AY SEMESTER 2 1.
Collections Dwight Deugo Nesa Matic
Part 1: Composition, Aggregation, and Delegation Part 2: Iterator COMP 401 Fall 2014 Lecture 10 9/18/2014.
Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA.
Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,
CompSci Toward understanding data structures  What can be put in a TreeSet?  What can be sorted?  Where do we find this information?  How do.
Using the Java Collection Libraries COMP 103 # T2
CSC 222: Object-Oriented Programming Fall 2017
CSC 222: Object-Oriented Programming
CSC 222: Object-Oriented Programming Spring 2017
CSC 427: Data Structures and Algorithm Analysis
CSC 222: Object-Oriented Programming
Chapter 19 Java Data Structures
CSC 222: Object-Oriented Programming Fall 2017
CSE 373: Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Effective Java, 3rd Edition Chapter 7: Lambdas and Streams
CSC 221: Computer Programming I Fall 2009
CSC 222: Object-Oriented Programming Spring 2013
More Data Structures (Part 1)
CSC 143 Java Linked Lists.
CSC 222: Object-Oriented Programming
Recursive Objects Singly Linked Lists.
CSC 222: Object-Oriented Programming Spring 2012
Presentation transcript:

1 CSC 321: Data Structures Fall 2013 HW1 autopsy 1.parallel lists 2.Comparable Word objects 3.Comparators 4.MVC pattern

Approach 1: parallel lists store words and counts in separate lists  advantages?  disadvantages? 2 "it""was""the""best"… 25121… 0123 … while this approach is simple, it is not cohesive  logically related data is not physically related  it is up to you to keep words & counts in corresponding positions – tricky!

Issue: how do you sort the parallel lists? can't use Collections.sort (at least not directly) can modify the source code of a sort (e.g., selectionSort)  whenever items in one list are moved, move corresponding items in the other  could have two different versions (one to sort by frequency, one by alphabet), or could have a single, generic sort 3 public static, T2> void selectionSort(ArrayList items, ArrayList parallel) { for (int i = 0; i < items.size()-1; i++) { int indexOfMin = i; for (int j = i+1; j < items.size(); j++) { if (items.get(j).compareTo(items.get(indexOfMin)) < 0) { indexOfMin = j; } T1 temp = items.get(i); items.set(i, items.get(indexOfMin)); items.set(indexOfMin, temp); T2 tempP = parallel.get(i); parallel.set(i, parallel.get(indexOfMin)); parallel.set(indexOfMin, tempP); }

Approach 2: encapsulation encapsulate a word and its frequency into a single object  provide methods for accessing the fields, incrementing the frequency  can make the Word class Comparable, so that a list of Words can be sorted but we need two versions!  compare by freq (for WordFrequencies)  compare by alpha (for TagCloud) 4 public class Word implements Comparable { private String word; private int count; public Word(String newWord, int count) { this.word = newWord; this.count = count; } public Word(String newWord) { this(newWord, 1); } public String getWord() { return this.word; } public int getFrequency() { return this.count; } public void increment() { this.count++; } public String toString() { return this.getWord() + " " + this.getFrequency(); } public int compareTo(Word other) { ??? }

Issue: having two different Word classes is kludgy! 5 public class WordByFreq implements Comparable { private String word; private int count;... public int compareTo(WordByFreq other) { if (other.count == this.count) { return this.word.compareTo(other.word); } else { return (other.count - this.count); } public class WordByAlpha implements Comparable { private String word; private int count;... public int compareTo(WordByAlpha other) { if (other.word.equals(this.word)) { return (other.count - this.count); } else { return this.word.compareTo(other.word); }

Approach 3: Comparators the Comparator interface requires compareTo and equals methods  can have one Word class, which provides 2 different Comparators (could be public constants, since they don't change) (or, could provide methods that return Comparators)  there is a version of Collections.sort that takes a Comparator the specified Comparator determines the ordering when sorting Collections.sort(words, Word.FreqComparator); // will sort by frequency Collections.sort(words, Word.AlphaComparator); // will sort alphabetically 6

Issue 7 public class Word { private String word; private int count;... public static Comparator AlphaComparator = new Comparator () { public int compare(Word w1, Word w2) { if (w2.word.equals(w1.word)) { return (w2.count - w1.count); } else { return w1.word.compareTo(w2.word); } public boolean equals(Word w1, Word w2) { return this.equals(w1, w2); } }; public static Comparator FreqComparator = new Comparator () { public int compare(Word w1, Word w2) { if (w2.count == w1.count) { return w2.word.compareTo(w1.word); } else { return (w2.count - w1.count); } public boolean equals(Word w1, Word w2) { return this.equals(w1, w2); } }; } still ugly, but better than two separate classes

Approach 4: MVC Model-View-Controller is a design pattern for OO software  Model is the data & logic of the application  View is the interface for viewing/updating the model  Controller is the connector between the Model & View ideally, there should be no I/O in the Model portion of the project ideally, there should be (almost) no application logic in the Controller GOAL: you should be able to change the View (e.g., go from console-based to GUI-based) without touching the Model  simply change the Controller (which connects the new View with the old Model) 8

MVC version 9 public class WordFreqList { private ArrayList words; private ArrayList ignoreWords; private int maxSoFar = -1; public WordFreqList() {... } public WordFreqList(String ignoreFileName) {... } public void add(String newWord) {... } public void add(String newWord, int count) {... } public int getFrequency(String word) {... } public int getMax() {... } public List asListByFreq() {... } public List asListByAlpha() {... } } public class Word { private String word; private int count; public Word(String newWord, int count) {... } public Word(String newWord) {... } public String getWord() {... } public int getFrequency() {... } public void increment() {... } public String toString() {... } public static Comparator AlphaComparator = new Comparator () {... }; public static Comparator FreqComparator = new Comparator () {... }; } the Word class encapsulates a word & its frequency  with operations for storing a frequency, incrementing it, and comparing Words the WordFreqList class encapsulates a list of Words  with operations for adding words, getting frequencies, and retrieving the list in different orders the Controller classes (WordFrequencies & TagCloud) then focus on I/O and calling Model methods

Other options? how easily could these two programs be combined into one?  i.e., go directly from speech.txt to speech.html 10 later in the course, you will learn about Maps  a Map is a 2-dimensional structure that maps a key to a value e.g., can map a word to its frequency count  makes it easy (and efficient) to look up and update a value based on its key e.g., look up the frequency count for a word you will also learn about iterators  instead of returning sorted copies of the Word list, you could provide iterators that allow for traversing the list