Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CREATE A JAVA PROGRAM TO MAKE A 27 CARD MAGIC TRICK WITH THE FOLLOWING INSTRUCTIONS INCLUDING THE OOP DESIGN Your program will display step-by-step instructions,

CREATE A JAVA PROGRAM TO MAKE A 27 CARD MAGIC TRICK WITH THE FOLLOWING INSTRUCTIONS INCLUDING THE OOP DESIGN

Your program will display step-by-step instructions, with a working example. If run again, the instructions should remain consistent, but the example will change (randomly*). Instructions: 1. The observer (or magician) shuffles the deck and deals out the first 27 cards. The remaining cards are not used for the rest of the trick. 2. The observer selects any one card from the newly shuffled 27-card deck. (Console input, perhaps the position of the card in the 27.) The observer remembers her card without showing the magician. (In programming terms, that means you can do nothing with the value remembered other than acting as the observer in what follows. That is, you cant use the value to force the trick to work.) 3. The chosen card is returned to the deck. (In your program, it never left the deck.) 4. The observer picks a number between 1 & 27, call it luckyNum. (Generate a random integer in that range and display the number the user choose.) 5. While the observer shuffles the deck of 27, the magician does some figuring a. BTnum = luckyNum 1 // number of cards I want on top of selected card b. Convert BTnum into three base-three digits: LMF3 c. The digits are used is reverse order (least to most significant digit) to make an ordering decision later. Note that if you get this wrong FML takes on the usual texters meaning. 6. The magician does the following three times a. Deals out cards sequentially into three piles, face up. b. The observer identifies the pile containing the (unknown to the magician) selected card. c. The magician picks up the three piles, resequencing the cards following the rule below based on the observer identifying the pile containing her card. (The order of the two remaining piles of nine is not significant.) i. On the first iteration, 1. if F is a 0, the identified nine card pile goes on top 2. if F is a 1, the identified nine card pile goes in the middle 3. if F is a 2, the identified nine card pile goes on the bottom ii. On the second iteration, same decision for M iii. On the third iteration, same decision for L d. The magician then reveals the cards sequentially, counting to luckyNum and stops, showing that the card in position luckyNum is the users secretly selected card. Cards should be displayed simply as two chars: 2S,3H,4C,5D,6S,7H,8C,9D,TS,JH,QC,KD,AS * The randomness comes from a shuffle of the deck (and the selection of the lucky number). One way to shuffle N numbers in an array for cnt from 0 to N-1 choose M randomly, such that 0 <= M <= N-1 swap the number at location cnt with the number at location M

Application Programmer Interface (OOP design) to use for your program: public class Card public Character suit; // S,C,D,H public Character rank; // A,2,3,4,...9,T,J,Q,K // no encoded tricks here, like using 0..12 or 0..51 ------------------------------------------------------------ /> public Card(char rank, char suit) //constructor public String toString() // for println(aCard) public class Deck private int numCardsinDeck; // 52, 27, or 9. It does not change after creation. private Card[] cards; // specific sequence of cards private Random rng = new Random(); // create once, use many times for shuffling ------------------------------------------------------------ public Deck() // default, standard 52-card-deck constructor // like opening a new box of poker cards, minus the jokers public Deck(int N) //constructor that creates an empty deck capable of holding N cards // for an (initially) empty pile of cards (no new Cards created) public Deck(Deck dBig, int N) //constructor that uses the first N cards of provided Deck // like dealing out the top N cards to create a smaller deck (no new Cards created) // This could have been a deal in sequence operation into an empty deck, and // arguably should reduce the number of Cards to whatever remains undealt public void deal(Deck[] piles) // distributes current deck cards into three temporary decks // assumes piles is an array of three empty decks. (For trick, N = 27) // each of the piles gets every third card. For N=9: [0,3,6], [1,4,7],[2,5,8] public void combine (Deck top, Deck middle, Deck bottom) // reconstitute the deck from piles // Deck must be big enough to hold the three smaller Decks (piles), resulting sequence is // [top0, top1, top2, middle0, middle1, middle2, bottom0, bottom1, bottom3] public void shuffle() // the N cards in this deck are placed in random sequence private void swap(int loc1, int loc2) // helper for shuffle public String toString() // used for println(aDeck); public Card getCard(int loc) // returns reference to the Card at position loc 1..52, not 0..51

Code I have so far:

MAIN:

import java.util.Scanner;

public class Main {

private static Scanner in = new Scanner(System.in);

private static final int LEFT = 0;

private static final int MIDDLE = 1;

private static final int RIGHT = 2;

public static void main(String[] args) {

int selectedDeck;

int[] tbm = new int[3];

int theNumber = 22;

System.out.println("Using " + theNumber + " as your lucky number");

convertTernary(theNumber-1 , tbm);

Deck deck52 = new Deck();

System.out.println("New deck " + deck52);

Deck deck27 = new Deck(deck52, 27);

System.out.println("First 27 cards, Pick one! " + deck27);

Deck[] tempPiles = { new Deck(9), new deck(9), new Deck(9)};

for (int ternDigitNum=0; ternDigitNum < 3; ternDigitNum++) {

Object1 tempPiles;

((Object) deck27).deal(tempPiles);

System.out.println(tempPiles[LEFT] + " Left");

System.out.println(tempPiles[MIDDLE] + " Middle");

System.out.println(tempPiles[RIGHT] + " Right");

selectedDeck = queryUser();

switch(tbm[ternDigitNum]) {

case 0: //deck goes on top

}

}

}

private static int queryUser() {

return 0;

}

private static void convertTernary(int i, int[] tbm) {

}

}

DECK CLASS:

public class Deck {

private int numCardsinDeck; // 52, 27, or 9. It does not change after creation.

private Card[] cards; // specific sequence of cards

privateRandom rng = new Random(); // create once, use many times for shuffling

public Deck() { // default, standard 52-card-deck constructor

// like opening a new box of poker cards, minus the jokers

}

public Deck(int N){ //constructor that creates an empty deck capable of holding N cards

// for an (initially) empty pile of cards (no new Cards created)

}

public Deck(Deck dBig, int N) { //constructor that uses the first N cards of provided Deck

// like dealing out the top N cards to create a smaller deck (no new Cards created)

// This could have been a deal in sequence operation into an empty deck, and

// arguably should reduce the number of Cards to whatever remains undealt

}

public void deal(Deck[] piles) { // distributes current deck cards into three temporary decks

// assumes piles is an array of three empty decks. (For trick, N = 27)

// each of the piles gets every third card. For N=9: [0,3,6], [1,4,7],[2,5,8]

}

public void combine(Deck top, Deck middle, Deck bottom){ // reconstitute the deck from piles

// Deck must be big enough to hold the three smaller Decks (piles), resulting sequence is

// [top0, top1, top2, middle0, middle1, middle2, bottom0, bottom1, bottom3]

}

public void shuffle() { // the N cards in this deck are placed in random sequence

}

private void swap(int loc1, int loc2) { // helper for shuffle

}

public String toString() { // used for println(aDeck);

}

public Card getCard(int loc) { // returns reference to the Card at position loc 1..52, not 0..51

}

}

CARD CLASS:

public class Card {

public Character suit; // S,C,D,H

public Character rank; // A,2,3,4,...9,T,J,Q,K

// no encoded tricks here, like using 0..12 or 0..51

public Card(char rank, char suit) { //constructor

}

public String toString() { // for println(aCard)

return null;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

=+c. Savings as the Star focus on price.

Answered: 1 week ago

Question

=+b. Product-Focused emphasize product features.

Answered: 1 week ago