Assignment 2
Recursion and ADTs
Submit before 11:30 PM Saturday January 24

Overview

The first part involves studying the recursive permute method and modifying it to produce valid anagrams. The second part involves creating an abstract data type (ADT) that represents a hand of playing cards.

As before, you may collaborate with one or two other people in the class.

Both the Anagram.java file and the PokerHand.java file should be in a folder called assn2 (and a package with the same name)

Anagrams

Create a class called Anagram. In the class, define a static method called getAnagrams, which takes a string as an argument and returns an array of strings. The array of strings should consist of valid anagrams (found in the given the dictionary file, wordsEn.txt, available online). The array should not have any duplicates.

Modify the permute method (discussed in class) to complete this assignment. You may want to use the BagOWords class to simplify the assignment.

You'll find that your anagram program runs much faster if it searches words using a binary search.

Hand ADT

Create a class called PokerHand that consists of Card objects. It should provide the following instance methods:

  • add -- this method takes a Card object as an argument and adds it to the hand.
  • size -- this method takes no arguments and returns the number of cards in the hand.
  • isFlush -- this method returns true if the hand is a flush (all cards have the same suit), false otherwise. The method takes no arguments.
  • isStraight -- this method returns true if the cards can be arranged so that their ranks are all in sequence (Ace can be low or high), false otherwise. The method takes no arguments.
  • isFullHouse -- this method returns true if the hand has 2 cards of one rank and 3 cards of another rank, false otherwise. The method takes no arguments.

The class should generally work for hands of five cards, the code should also work for hands of other sizes with minimal modification. In other words, it should make use of loops with index variables instead of hard-coded references to each card in the hand.

Here are some suggestions for completing the assignment:

  • Study the DeckOfCards class. It has methods that might be useful for the PokerHand class.
  • Sorting the hand by rank makes it easier to check for a straight and a full house, although checking for a low Ace in the straight requires a special case.
  • Use the main method to write test code.
  • A fun way to test the code is by simulating many deals (even a million deals) to the hand and counting the number of times a particular hand is drawn. You can then compare the percentage from your simulation with the actual probability of obtaining the hand. Don't forget to shuffle the deck before you create the hand!

Submission

Zip up your assn2 package and submit it as assn2.zip on D2L. If you work with one or two other people, only one person needs to submit. In the submissions comments, indicate who you worked with.