Question: This question is from Object-Oriented Data Structures Using Java Third Edition. Pg.465. The Program will be below the questions. 32. In this exercise, you investigate

This question is from Object-Oriented Data Structures Using Java Third Edition. Pg.465. The Program will be below the questions.

32. In this exercise, you investigate potential improvements to the PokerApp program.

a. Run this program five times on your machine. Record how long it takes. Create a table and record both the reported probability and the number of seconds taken for each run.

b. Redesign and recode the program so that instead of always dealing out seven cards to a hand, it stops dealing whenever a pair is discovered. Run the program five times, and record both the results and the execution time. Compare your findings to the data gathered in part a, and discuss the differences.

c. Starting again with the original program, redesign and recode it so that instead of reshuffling after every hand, the deck is reshuffled after every seven hands. Run the program five times, recording both the results and the execution time. Compare your findings to the data gathered in parts a and b, and discuss the differences.

PokerApp Program:

//--------------------------------------------------------------------- // PokerApp.java by Dale/Joyce/Weems Chapter 6 // // Simulates dealing poker hands to calculate the probability of // getting at least one pair of matching cards. //----------------------------------------------------------------------

import ch06.lists.*;

import support.*; // RankCardDeck

public class PokerApp { public static void main(String[] args) { final int HANDSIZE = 7; // number of cards per hand final int NUMHANDS = 1000000; // total number of hands int numPairs = 0; // number of hands with pairs boolean isPair; // status of current hand float probability; // calculated probability

ListInterface hand; RankCardDeck deck = new RankCardDeck(); int card;

for (int i = 0; i < NUMHANDS; i++) { deck.shuffle(); hand = new ArrayUnsortedList(HANDSIZE); isPair = false; for (int j = 0; j < HANDSIZE; j++) { card = deck.nextCard(); if (hand.contains(card)) isPair = true; hand.add(card); } if (isPair) numPairs = numPairs + 1; }

probability = numPairs/(float)NUMHANDS;

System.out.println(); System.out.print("There were " + numPairs + " hands out of " + NUMHANDS); System.out.println(" that had at least one pair of matched cards."); System.out.print("The probability of getting at least one pair,"); System.out.print(" based on this simulation, is "); System.out.println(probability); } }

RankCardDeck Program:

package support;

import java.util.Random;

public class RankCardDeck {

private static final int numCards = 52;

protected int[] carddeck = new int[numCards];

protected int curCardPos = 0; // position of the next card to be dealt

protected Random rand = new Random(); // to generate random numbers

public RankCardDeck()

{

for (int i = 0; i < numCards; i++)

carddeck[i] = i / 4; // there are 4 cards of each rank

}

public void shuffle()

// Randomizes the order of the cards in the deck and resets the

// position of the current card to card 0.

{

int randLoc; // random location in card deck

int temp; // for swap of cards

for (int i = (numCards - 1); i > 0; i--)

{

randLoc = rand.nextInt(i); // random integer between 0 and i - 1

temp = carddeck[randLoc];

carddeck[randLoc] = carddeck[i];

carddeck[i] = temp;

}

curCardPos = 0;

}

public boolean hasMoreCards()

// Returns true if there are still cards left to be dealt;

// otherwise, returns false.

{

return (curCardPos != numCards);

}

public int nextCard()

// Precondition: curCardPos != numCards

//

// Models a card being dealt by returning an integer representing

// its rank and incrementing the position of the current card.

{

curCardPos = curCardPos + 1;

return (carddeck[curCardPos - 1]);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!