I am wondering if my answer to this most recent assignment is correct. Just want see if anyone has any input before I submit. Any comments would be appreciated! Thanks!

Here is the assignment:The card game poker is a game of chance. If you aren't familiar with the game, you can read about it at the following links (among other places). The deck is described at http://en.wikipedia.org/wiki/Standard_52-card_deck; poker rules are described at Rules of Card Games: Poker. The names of hands, e.g., flush, straight, and so forth, and what they mean, can be found at Poker Hands l Official Poker Hand Ranking from Best to Worst. This assignment ignores betting completely; it is only concerned with the relative frequency of various kinds of poker hands.

The code provided in the three classes below calculates the frequency of obtaining a straight (five cards running consecutively, e.g. 3-4-5-6-7); and a flush (all five cards of the same suit). The calculation for a straight does not exclude the possibility of the straight also being a flush, e.g., 3-4-5-6-7 all of the same suit. As you will surely notice, the code for determining a straight is considerably more complicated than the flush code.

Using the given algorithms as a model, your job for this assignment is to

1) determine the chances (the relative frequency) of obtaining a full house;

2) determine the chances (the relative frequency) of getting a flush that is not a straight; and

3) determine the chances (the relative frequency) of obtaining nothing: five cards with distinct values, not of the same suit, and not a straight;

A word on how the given code works. We run one million experiments. Each time, we a) assemble an unshuffled deck (the cards appear in a specific order, e.g., all the hearts, then all the diamonds, etc.); b) we shuffle the deck, and then look at the first five cards - this is the hand we're dealt; and c) we then examine this hand to see if it's a straight or a flush.

Below we've given you 3 classes, a Card class, a Deck class, which makes use of the Card class, and a driver class, HandTester. Study these classes carefully. You need to copy each class to your machine and of course you need to compile them. Thye provide the framework for developing the three methods that make up the assignment.

public class Card {
 private int value;
 private char suit; // H D C S, for hearts, diamonds, clubs, spaces
 
 public Card(int i, char c) {
  value = i;
  suit = c;
 }
 
 public int getValue() {
  return value;
 }
 
 public char getSuit() {
  return suit;
 }
 
 public String toString() {
  String ans = "";
  switch (value) {
  case 1:
   ans = "A";
   break;
  case 11:
   ans = "J";
   break;
  case 12:
   ans = "Q";
   break;
  case 13:
   ans = "K";
   break;
  default:
   ans = ("" + value);
  }
  return (ans + '-' + suit);
 }
 
}

public class HandTester {
 public static void main(String[] args) {
  int runs = 1000000;
  int straightCt = 0;
  int flushCt = 0;
  Deck d = new Deck();
  for (int j = 0; j < runs; j++) {
   d.freshDeck();
   d.shuffle();
   if (d.straight())
    straightCt++;
   if (d.flush())
    flushCt++;
  }
  System.out.print("straight frequency ");
  System.out.println(straightCt / (double) runs);
  System.out.print("flush frequency ");
  System.out.println(flushCt / (double) runs);
 }
}

public class Deck
{
 // models a deck of cards
 private Card[] cards;
 // a random object from java.util
 private java.util.Random r = new java.util.Random();
 
 public Deck() {
  cards = freshDeck();
 }
 
 public Card[] getDeck() {
  return cards;
 }
 
 public Card[] freshDeck() {
  // makes a "mint" deck that runs through hearts, diamonds, clubs, spades
  Card[] deck = new Card[52];
  int loc = 0;
  for (int j = 1; j <= 13; j++) {
   deck[loc] = new Card(j, 'H');
   loc++;
  }
  for (int j = 1; j <= 13; j++) {
   deck[loc] = new Card(j, 'D');
   loc++;
  }
  for (int j = 1; j <= 13; j++) {
   deck[loc] = new Card(j, 'C');
   loc++;
  }
  for (int j = 1; j <= 13; j++) {
   deck[loc] = new Card(j, 'S');
   loc++;
  }
  return deck;
 }
 
 public void shuffle() {
  /*
   * method randomly shuffles cards - shuffle details in ch 9 first five
   * cards make up a random poker hand
   */
  int swapPos;
  Card temp;
  for (int i = cards.length - 1; i > 0; i--) {
   swapPos = r.nextInt(i + 1); // pick pos from 0 -> i (i is possible)
   temp = cards[swapPos]; // swap vals at i, swapPos
   cards[swapPos] = cards[i];
   cards[i] = temp;
  }
 }
 
 public boolean flush() {
  /*
   * applied to array cards, after cards have been shuffled straight-flush
   * not excluded (so: true even if hand is both straight AND flush).
   * method works like this: gets suit of 0th card in deck -keySuit- and
   * checks it against next four
   */
  char keySuit = cards[0].getSuit();
  char suit;
  for (int j = 1; j <= 4; j++) {
   suit = cards[j].getSuit();
   if (keySuit != suit) {
    return (false);
   }
  }
  return true; // case reached ONLY when all suits match keySuit
 }
 
 private int[] makeTally() {
  /*
   * looks at first 5 cards in (shuffled) deck - these make up the random
   * hand - and tallies their values on a scoreboard. A 2 in cell 7 means
   * that there are two 7's in the current hand. Method returns scoreboard
   */
  int[] scoreboard = new int[14];// Ace count in cell 1,2 ct in cell 2
  for (int j = 0; j <= 4; j++) {
   int k = cards[j].getValue();
   scoreboard[k]++;
  }
  return scoreboard;
 }
 
 public boolean straight() {
  int[] tally = makeTally(); // scoreboard of values in the hand.
  // looking for a consecutive sequence of all 1's starting at
  // firstPos - where the first 1 falls on left
  int f = firstPos(tally); // f: first "1" entry in tally
  if (f == 0)
   return false; // means: none - so: no straight
  else
  // tricky case: single Ace, so check 2-5, 10-K (10-13)
  if (f == 1)
   return (legit(2, 5, tally) || legit(10, 13, tally));
  if (f > 9)
   return false; // if first 1 is 10 or higher there's
  // no Ace - so a straight isn't possible
  else
   return (legit(f + 1, f + 4, tally)); // what about the 4 cards after
             // f
 }
 
 private int firstPos(int[] t) {
  /*
   * serves the straight method. Returns first tally position that holds a
   * singleton - a 1. If none do, 0 is returned which means that there are
   * no singletons - so a full house
   */
  for (int p = 1; p <= 13; p++)
   if (t[p] == 1)
    return p;
  return 0; // happens when no '1' entry is found
 }
 
 private boolean legit(int first, int last, int[] t) {
  /*
   * serves the straight method. Returns true exactly when the tally -
   * what will be tied to the formal parameter t -- shows a string of 1's
   * and nothing else from first to last
   */
  for (int i = first; i <= last; i++)
   if (t[i] != 1)
    return false;
  return true; // all 1's from first to last
 }
 
}

I just have to submit the three methods ( fullHouse(), flushNotStraight(), and nothing() ) , so I do not have to worry about altering the original classes and driver that was given. Here are my three methods:

public boolean fullHouse(){
if(firstPos()==0)
  return true;
else
  return false;
 
}
 
public boolean flushNotStraight(){
  if((flush()== true)&&(straight()== false))
    return true;
else
  return false;
}
 
public boolean nothing(){
if((flush()== false)&&(straight()==false)&&(fullHouse()==false))
  return true;
else
  return false;
}

Is this correct?