Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You must follow the style guide e for all projects. For this project, you will c

ID: 3574133 • Letter: Y

Question

You must follow the style guide e for all projects. For this project, you will create a function for a larger magic card trick game. Your task is to write a function that will allow a user will pick a card from a collection of cards and place it on the top of the deck of cards. The deck of cards will be an array of strings. The individual cards will be stored in the array. You can use a string to represent each card. For example, the queen of hearts might be stored as QH or "Queen of Hearts". Keep in mind that the card's value must be passed to your function, so an abbreviation for each card may be easier to work with. Here are the preconditions and post conditions for your function: Precondition: the array is not empty. Postcondition: This function removes the card value from an array and reinserts it at the begining of the array. This will reorder the elements in the array so the chosen card is in position 0 of the array and all other elements have been repositioned to make room. No card values are lost. Minimum Requirements: Create a class that has a string array as a private data member. You cannot use a vector for this assignment (5 points). You must include a function (member or non-member function) to fulfill the precondition and postcondition listed above (5 points). For testing purposes, include a class function to add some playing card

Explanation / Answer

MagicCardTrick.java


public class MagicCardTrick {

   private String cards[]={"AS","2S","3S","4S","5S","6S","7S","8S","9S","10S",
       "JS","QS","KS","AD","2D","3D","4D","5D","6D","7D","8D","9D","10D",
       "JD","QD","KD","AC","2C","3C","4C","5C","6C","7C","8C","9C","10C",
       "JC","QC","KC","AH","2H","3H","4H","5H","6H","7H","8H","9H","10H",
       "JH","QH","KH"};
   /*
   * array of 52 cards represented as string
   * for example AS represents "ace of spades"
   */

  
   void reposition(String cardVal)
{
   int i=0;
   /*
   * find the actual position of desired card value in the deck
   */
   while(!cards[i].equals(cardVal))
   {
       i++;
   }
  
   String key=cards[i]; //remove that card value
   /*
   * reposition other cards
   */
   for(int j=i;j>0;j--)
   {
       cards[j]=cards[j-1];
   }
   cards[0]=key; //reinsert that card at position 0.
  
   /*
   * show modified deck of cards.
   */
   for(int k=0;k<cards.length;k++)
       System.out.print(cards[k]+" ");
}
/*
* for testing
*/
public static void main(String a[])
{
   String testcard[]={"5S","7D","KH","JC","9D"};
   MagicCardTrick magic=new MagicCardTrick();
   for(int i=0;i<testcard.length;i++){
       magic.reposition(testcard[i]);
   System.out.println();
   }
   }
}

ClientProg.java


public class ClientProg {

   public static void main(String a[])
   {
       MagicCardTrick magic=new MagicCardTrick();
       magic.reposition("6S");
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote