Deck of Cards Program

I need help printing a flush, which is showing the top 5 cardsof the same suite.

Below is the code I already have that answers other objectives,such as dealing the cards, and finding pairs.

Towards the end I have attempted printing a flush, but I cannotfigure it out.

public classShuffler {

   /**

   * The number of consecutive shuffle steps to beperformed in each call

   * to each sorting procedure.

   */

   private staticfinal intSHUFFLE_COUNT = 1; // Determines amountof times that the numbers will shuffle

   private staticfinal int[]VALUE_COUNT = {0,1,2,3}; // Determinesamount of times that the numbers will shuffle

   /**

   * Tests shuffling methods.

   * @param args is not used.

   */

   public staticvoidmain(String[] args) {

      System.out.println(“Resultsof ” + SHUFFLE_COUNT +

              ” consecutive perfect shuffles:”);

       int[]values1 = VALUE_COUNT;// This array has the numbers that will be shuffled. The numberscan be changed to anything. This particular array applies to theperfect shuffle.

       for(int j = 1; j <=SHUFFLE_COUNT; j++) { // Prints out theperfect shuffle

          perfectShuffle(values1);

          System.out.print(” ” + j+ “:”);

          for (int k = 0;k < values1.length; k++) {

             System.out.print(” ” +values1[k]);

           }

          System.out.println();

       }

      System.out.println();

      System.out.println(“Resultsof ” + SHUFFLE_COUNT +

              ” consecutive efficient selectionshuffles:”);

       int[]values2 = VALUE_COUNT;//This array is used for the selection shuffle. The numbers in thearray can be changed to anything

       for(int j = 1; j <=SHUFFLE_COUNT; j++) {

          selectionShuffle(values2);

          System.out.print(” ” + j+ “:”);

          for (int k = 0;k < values2.length; k++) { // Prints out the numbers after theyare shuffled

             System.out.print(” ” +values2[k]);

           }

          System.out.println();

       }

      System.out.println();

   }

   /**

   * Apply a “perfect shuffle” to the argument.

   * The perfect shuffle algorithm splits the deck inhalf, then interleaves

   * the cards in one half with the cards in theother.

   * @param values is an array ofintegers simulating cards to be shuffled.

   */

   public staticvoidperfectShuffle(int[] values) { //Intertwines all the cards in the deck by splitting deck in half andmixing the two halves.

       /* *** TO BE IMPLEMENTED INACTIVITY 3 *** */

       int []hold = new int[values.length]; // Creates new array to hold the temporary valuesthat the method will use when it shuffles

       intleft = 0; // The left side of the deck will startat the first card

       intright = values.length/2; // Right side will be theentire deck split in half

      for(int i = 0;i<values.length; i+=2){ // Runs the loop how ever many cardsthere are. It adds two each time.

           hold[i]=values[left]; // replaces the new shuffled value in the holdarray

           hold[i+1] =values[right];

           left++; //moves to next card

           right++;

       }

      for(int i = 0;i<values.length; i++){

           values[i] =hold[i]; // replaces all of the ordered cards into the newlyshuffled array

       }

   }

   /**

   * Apply an “efficient selection shuffle” to theargument.

   * The selection shuffle algorithm conceptuallymaintains two sequences

   * of cards: the selected cards (initially empty)and the not-yet-selected

   * cards (initially the entire deck). It repeatedlydoes the following until

   * all cards have been selected: randomly remove acard from those not yet

   * selected and add it to the selected cards.

   * An efficient version of this algorithm makes useof arrays to avoid

   * searching for an as-yet-unselected card.

   * @param values is an array ofintegers simulating cards to be shuffled.

   */

   public staticvoidselectionShuffle(int[] values) {// Chooses a random card and puts it at the end of the list. Thentakes another card with the remaining ones and puts that next tothe previous one.

       /* *** TO BE IMPLEMENTED INACTIVITY 3 *** */

      for(int i =values.length-1; i>0; i–){ // Runs through the loop for each ofthe cards

          int position =(int) (Math.random() *i); // Selects a random card from the pile

          int temp = values[position];

          values[position] = values[i];

           values [i] =temp; // replaces the temporary value with the card that wasselected

       }

   }

}

public classDeck{

   /**

   *

   * cards contains all the cards in the deck.

   *

   */

   private List<Card>cards;

   /**

   *

   * size is the number of not-yet-dealt cards.

   *

   * Cards are dealt from the top (highest index)down.

   *

   * The next card to be dealt is at size – 1.

   *

   */

   private intsize;

   /**

   *

   * Creates a new <code>Deck</code>instance.<BR>

   *

   * It pairs each element of ranks with each elementof suits,

   *

   * and produces one of the corresponding card.

   *

   * @param ranks

   * is an array containing all of the card ranks.

   *

   * @param suits

   * is an array containing all of the card suits.

   *

   * @param values

   * is an array containing all of the card pointvalues.

   *

   */

   publicDeck(String[] ranks,String[] suits, int[] values){

       /* *** TO BE IMPLEMENTED INACTIVITY 2 *** */

       cards = newArrayList<Card>();

       for(int i = 0; i < suits.length;i++) {

          for (int j = 0;j < ranks.length; j++) {

              cards.add(new Card(ranks[j],suits[i], values[j]));

           }

       }

       size = cards.size();

       shuffle();

   }

   /**

   *

   * Determines if this deck is empty (no undealtcards).

   *

   * @return true if this deck isempty, false otherwise.

   *

   */

   public booleanisEmpty() {

       /* *** TO BE IMPLEMENTED INACTIVITY 2 *** */

       if (size >0)

          return false;

       else

          return true;

   }

   /**

   *

   * Accesses the number of undealt cards in thisdeck.

   *

   * @return the number of undealtcards in this deck.

   *

   */

   public intsize() {

       /* *** TO BE IMPLEMENTED INACTIVITY 2 *** */

       returnsize;

   }

   /**

   *

   * Randomly permute the given collection ofcards

   *

   * and reset the size to represent the entiredeck.

   *

   */

   public voidshuffle() {

       /* *** TO BE IMPLEMENTED INACTIVITY 4 *** */

       for(int i = cards.size() – 1; i >0; i–) {

          int t = (int)Math.round(Math.random()* i);

          Card temp = cards.get(i);

           cards.set(i,cards.get(t));

           cards.set(t,temp);

       }

       size = cards.size();

   }

   /**

   *

   * Deals a card from this deck.

   *

   * @return the card just dealt, ornull if all the cards have been

   *

   * previously dealt.

   *

   */

   public Carddeal() {

       /* *** TO BE IMPLEMENTED INACTIVITY 2 *** */

       if(isEmpty())

          return null;

       else {

           size–;

          return cards.get(size);

       } //if the deck is empty a nullvalue is returned and if it is not then the cards are dealtagain

   }

   /**

   *

   * Generates and returns a string representation ofthis deck.

   *

   * @return a string representationof this deck.

   *

   */

   @Override

   public StringtoString() {

       Stringrtn = “size = ” + size + “nUndealt cards: n”;//sets up a string to print out the number of undealt cards

       for(int k = size – 1; k >= 0;k–) {

           rtn = rtn +cards.get(k); //adds the cards to the string to be printed out

          if (k != 0) {

              rtn = rtn + “, “;

           }

          if ((size – k) % 2 == 0) {

              // Insert carriage returns so entire deck isvisible on console.

              rtn = rtn + “n”;

           }

       }

       rtn = rtn + “nDealt cards:n”;

       for(int k = cards.size() – 1; k>= size; k–) {

           rtn = rtn +cards.get(k);

          if (k != size) {

              rtn = rtn + “, “;

           }

          if ((k – cards.size()) % 2 == 0) {

              // Insert carriage returns so entire deck isvisible on console.

              rtn = rtn + “n”;

           }

       }

       rtn = rtn + “n”;

       return rtn;

   }

}

public classCard {

/**

   * String value that holds the suit of the card

   */

   private Stringsuit;

   /**

   * String value that holds the rank of the card

   */

   private Stringrank;

   /**

   * int value that holds the point value.

   */

   private intpointValue;

   /**

   * Creates a new Card instance.

   *

   * @param cardRank

   * a String value containing the rank of thecard

   *

   * @param cardSuit

   * a String value containing the suit of thecard

   *

   * @param cardPointValue

   * an int value containing the point value of thecard

   */

   publicCard(String cardRank,String cardSuit, intcardPointValue) {

       /* *** TO BE IMPLEMENTED INACTIVITY 1 *** */

       this.rank =cardRank;

       this.suit =cardSuit;

       this.pointValue= cardPointValue;

   }

   /**

   * Accesses this Card’s suit.

   *

   * @return this Card’s suit.

   */

   public Stringsuit() {

       /* *** TO BE IMPLEMENTED INACTIVITY 1 *** */

       returnsuit;

   }

   /**

   * Accesses this Card’s rank.

   *

   * @return this Card’s rank.

   */

   public Stringrank() {

       /* *** TO BE IMPLEMENTED INACTIVITY 1 *** */

       returnrank;

   }

   /**

   * Accesses this Card’s point value.

   *

   * @return this Card’s pointvalue.

   */

   public intpointValue() {

       /* *** TO BE IMPLEMENTED INACTIVITY 1 *** */

       returnpointValue;

   }

   /**

   * Compare this card with the argument.

   *

   * @param otherCard

   * the other card to compare to this

   *

   * @return true if the rank, suit,and point value of this card are equal to

   * those of the argument; false otherwise.

   *

   */

   public booleanmatches(Card otherCard) {

       /* *** TO BE IMPLEMENTED INACTIVITY 1 *** */

       if(this.pointValue == otherCard.pointValue&& this.suit.equals(otherCard.suit)

              &&this.rank.equals(otherCard.rank))

          return true;

       else

          return false;

   }

   /**

   * Converts the rank, suit, and point value into astring in the format

   * “[Rank] of [Suit] (point value = [PointValue])”.This provides a useful

   * way of printing the contents of a Deck in aneasily readable format or

   * performing other similar functions.

   *

   * @return a String containing therank, suit, and point value of the card.

   */

   @Override

   public StringtoString() {

       /* *** TO BE IMPLEMENTED INACTIVITY 1 *** */

       return rank + “of ” + suit + ” (point value = ” + pointValue + “)”;

   }

}

————————————————————————–

import java.lang.reflect.Array;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Collections;

import java.util.Scanner;

public class Hand {

   static ArrayList hand = new ArrayList();

   static String [] ranks = {“Ace”, “2”,”3″,”4″,”5″,”6″,”7″,”8″,”9″,”10″,”Jack”,”Queen”,”King”}; // anarray with all of the ranks

   static String [] suits = {“spades”, “hearts”,”clubs”, “diamonds”}; // can array with all of the suits

   static int [] values ={1,2,3,4,5,6,7,8,9,10,11,12,13}; // Puts a point value for each ofthe cards

   public static void main(String args[]) {

       Scanner scan = newScanner(System.in);

       // Shuffles deck and ask userhow many cards

       int cards = 52;

       Deck deck = new Deck(ranks,suits, values); // puts in the format of ranks suits and thenvalues

       deck.shuffle(); // uses theshuffle method to shuffle the deck

       System.out.println(“How manycards should be dealt?: “);

       cards = scan.nextInt();

       //takes the users input and putit into a while loop to deal that many cards.

       int index = 0;

       while (index < cards) {

          hand.add(deck.deal()); // deals cards depending on what the userasks for

           index++;

       }

       //Shows the hand.

       System.out.println(“”);

       System.out.print(“Your Hand:n”); // prints out the hand

       for(int j = 0; j <=hand.size()-1; j++){

          System.out.print(“t”+hand.get(j) + “n”);

       } // prints out the cards in thehand

       for(index = 0; index<hand.size(); index ++)

       {

           Stringspade;

           spade =hand.get(index).toString();

          if(spade.contains(“spades”))

           {

              System.out.println(“tPosition in Hand: ” +(index+1) ); // adds one so position in hand doesn’t begin withzero

           }

       }

       ArrayList ValueCompare = newArrayList();

       while (hand.size() > 0) {

           int place =0; // place of card.

           Card c =(Card)hand.get(0); // Takes the first card and looks at each cardafter that and sorts it

           for (int j =1; j < hand.size(); j++) {

              Card c1 = (Card)hand.get(j);

              if ( c1.pointValue() < c.pointValue() ||

                     (c1.pointValue() < c.pointValue() && c1.suit() ==c.suit()) ) { // if the point values are different it ordersthem

                  place = j;

                  c = c1;

              }

           }

          hand.remove(place); // removes the place

          ValueCompare.add(c); // adds the ordered cards to the ValueComparearraylist

       }

       hand = ValueCompare; // setshand equal to the ValueCompare

       System.out.println(“”);

       System.out.print(“AscendingOrder: nt”);// prints it out in ascending order

       for(int j = 0; j <=hand.size()-1; j++){

          System.out.print(hand.get(j) + “nt”);

       }

       Collections.reverse(hand); //reverses it using the collection class

       System.out.println(“”);

       System.out.print(“DescendingOrder: nt”);// prints it out in ascending order

       for(int j = 0; j <=hand.size()-1; j++){

          System.out.print(hand.get(j) + “nt”);

       }

       Collections.reverse(hand); //reverses it using the collection class

       ArrayList RedCards = newArrayList();

       ArrayList BlackCards = newArrayList();

       for (int j = 0; j <hand.size(); j++) {

           Card c1 =(Card)hand.get(j);

           if (c1.suit().equals(“hearts”) || c1.suit().equals(“diamonds”)) {

              RedCards.add(c1);

           }

           else{

              BlackCards.add(c1);

           }

       }

       System.out.println(“”);

       System.out.print(“Red Cards:nt”);

       for(int j = 0; j <=RedCards.size()-1; j++){

          System.out.print(RedCards.get(j) + “nt”);

       }

       System.out.println(“”);

       System.out.print(“Black Cards:nt”);

       for(int j = 0; j <=BlackCards.size()-1; j++){

          System.out.print(BlackCards.get(j) + “nt”);

       }

      System.out.println(“”);        

       System.out.println(“What is thevalue of the pairs you want?”);

       int pairValue=scan.nextInt();

       int numberOfPairs=0;

       for(int i =0; i <hand.size()-1;i++) {

           for(int j =(i+1); j < hand.size()-1; j++ ) {

              if (hand.get(i).pointValue() ==hand.get(j).pointValue()) {

                  if (pairValue ==hand.get(i).pointValue()){

                     numberOfPairs ++;  

                  }        

              }

           }

       }

       System.out.println(“There are “+ numberOfPairs + ” pair(s) of cards with the value “+pairValue+”.” );

  

  

       ArrayList SpadeCards = newArrayList();

       ArrayList ClubCards = newArrayList();

       ArrayList HeartCards = newArrayList();

       ArrayList DiamondCards = newArrayList();

       int[] SpadeNums = newint[hand.size()];

       for(int i =0;i

          if(hand.get(i).suit().equalsIgnoreCase(“spades”)){

              SpadeCards.add(hand.get(i));

           }

       }

       if(SpadeCards.size()<5){

          

       }

       else{

           for(int i=0;i

              SpadeNums[i]=SpadeCards.get(i).pointValue();

           }

      

       Arrays.sort(SpadeNums);

       ArrayList SecondSpadeCards = newArrayList();

       for(int i =0;i

          if(hand.get(i).pointValue()==SpadeNums[4] ||hand.get(i).pointValue()==SpadeNums[3]||hand.get(i).pointValue()==SpadeNums[2]||hand.get(i).pointValue()==SpadeNums[1]||hand.get(i).pointValue()==SpadeNums[0]){

              SecondSpadeCards.add(hand.get(i));

           }

           }

              for(int i =0;i

                 System.out.println(SecondSpadeCards.get(i).toString());

           }

      

  

   System.out.println(“Your flush is”); // adds one soposition in hand doesn’t begin with zero

   }

}

}