SlideServe
Menu
Préférences
© SlideServe · v2026

CS 240 Week 4

ghada avatar
14 diapositives · 329 Views · Oct 21, 2014

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>

CS 240 Week 4 - slide 1
01 / 14CS 240 Week 4
1 / 17

Télécharger la présentation

CS 240 Week 4
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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