1 / 14

CS 240 Week 4

CS 240 Week 4. Evil Hangman. java Hangman wordLength guesses Run Demo Building a pattern for the letters guessed so far Data Structures Set of T: Set<T> Set of words of proper length – Set<String> Set of words that match a pattern – Set<String>

ghada
Télécharger la présentation

CS 240 Week 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 240Week 4

  2. Evil Hangman • java Hangman wordLengthguesses • Run Demo • Building a pattern for the letters guessed so far • Data Structures • Set of T: Set<T> • Set of words of proper length – Set<String> • Set of words that match a pattern – Set<String> • Map from Key to Value: Map< Key_Type, Value_Type> • Map from pattern to set of words matching that pattern • Map<String, Set<String>> • List of T: List<T> • List of sets of Maximum Size: List of prospective pattern/set of word pairs • The set of words associated with the prospective pattern must match the associated pattern •  p ∈ list of sets of Maximum Size (¬ q ∈ all pattern/set of word pairs ^ |q.set of word| > |p.set of word|) • Java syntax: Map.Entry<String, Set<String>> • Each entry is a key, value pair in a Map • List of sets of Maximum Number of Blanks: List of pattern,/set of word pairs such that •  p ∈ list of sets of Maximum Number of Blanks (p ∈ list of sets of Maximum Size ^ ¬ q ∈ list of sets of Maximum Size (numberOfBlanksIn(q.pattern) > numberOfBlanksIn(p.patterhn))) • Java syntax: Map.Entry<String, Set<String>> • Each entry is a key, value pair in a Map

  3. Basic Algorithm(Terribly Inefficient) • Create a list of words • Start with blank pattern of appropriate size • e.g. “------” • Repeat the following until the word has been guessed or the player has exceeded the number of guesses • For every guess create all possible patterns from the current pattern where the guessed letter has been substituted for zero or more of the blanks • For each of these new patterns create patttern/set of matching word pairs • Create the set of all pattern/set of matching word pairs that have the maximum number of matching words for the corresponding pattern • From that set create a subset of pairs such that the pattern has the most number of blanks • Select a pair, p, from the resulting set • If |p.set of matching word pairs| = 1 the player has guessed the word else set the current pattern to p.pattern and have the player guess again

  4. Reading from Standard.in • new Scanner(System.in)

  5. Inner Classes • Standard Inner Classes – usually just called inner classes • Local Inner Classes – classes declared within a method • Anonymous Inner Classes – classes declared within a method but it has no name. • At the same time you define the class, you create a single instance of the class. • Usually, when defining the class you override at least one method in the class

  6. Inner Classes • Declare a class inside another class • Example: declare the class Iterator inside the class Stuff. Make it extend Iterator. • Now we can declare: • Stuff.Iteratoriter = stuff.iterator(); • Inner classes can be public, private, or protected • Example • Main.java • Node.java • Graph.java

  7. Local Inner Classes • Classes declared in a method • No visibility designator • A weaker form of closure • Example • Main.java

  8. Anonymous Inner Classes • Inner classes declared on the fly in a metod • No visibility designator • A weaker form of closure • Used extensively in GUIs • Syntax new T(…) { public void foo() { //overridden method of } } • Version of command pattern • How do you pass method as parameter • Example • Main.java • Integral.java

  9. Java Collections • From the library • See the java api • Simple Generics • Parameters • The parameters may be generic • C<X, Y, Z> • Parameters cannot be of atomic type • Abstract Class or Interface vs. Concrete • List: ArrayList, LinkedList, Stack, Vector • Set: HashSet, TreeSet • Map: HashMap, HashTable • Iterators

  10. Iterators • Iterator<Integer> iter = someSet.iterator(); • hasNext() • next() • Interface Iterable<T> • Example of use 1: Iterator<String> iter = studentList.iterator() while(iter.hasNext()){ String str = iter.next(); … } • Example of use 2: for-each loop for(String str : studentList) { … } • studentList must implement Iterable • It does if it is declared as ArrayList<String> studentList;

  11. Lists • List<T> • Common methods • add(T t) hashCode() • add(int index, T t) isEmpty() • clear() iterator() • contains(T t) indexOf(Object o) • equals(Object o) remove(int index) • get(int index) remove(Object o) • set(int index, T t) size() • subList(int from, int to) • List Example • To run this you also need Name.java • Notice the use of interfaces for Comparable objects

  12. Sets • Set<T> • Common methods • add(T t) iterator() • clear() remove(Object o) • contains(T t) size() • equals(Object o) • hasCode() • isEmpty() • Sets Example • To run this you also need Name.java

  13. Map • Map<KeyType, ValueType> • Map.Entry<KeyType, ValueType> • Common Methods • clear() containsKey(Object key) • containsValue(Object value) entrySet() • equals(Object o) get(Object key) • hashCode() isEmpty() • keySet() put(KeyType k, ValueType v) • remove(Object key) size() • values() – returns Collection<V> • Map Example

  14. String/Character • Additional methods for String • matches(regular expression) • Example: answer.matches(“[Yy][Ee][Ss]”); • Static methods from the class Character • Character.toUpperClase(char) • Character.toLowerCase(char) • Example Code

More Related