Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4104846
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:10:13+00:00 2026-05-20T21:10:13+00:00

Thank you for everyone’s help!! This is what I went with. 3/30/2011- import java.util.Arrays;

  • 0

Thank you for everyone’s help!! This is what I went with.

3/30/2011-

import java.util.Arrays; public class
Deck {

String [] cards = {"AH", "2H", "3H", "4H", "5H", "6H", "7H", "8H",

“9H”, “10H”, “JH”, “QH”, “KH”,
“AC”, “2C”, “3C”, “4C”, “5C”, “6C”, “7C”, “8C”, “9C”,
“10C”, “JC”, “QC”, “KC”,
“AD”, “2D”, “3D”, “4D”, “5D”, “6D”, “7D”, “8D”, “9D”,
“10D”, “JD”, “QD”, “KD”,
“AS”, “2S”, “3S”, “4S”, “5S”, “6S”, “7S”, “8S”, “9S”,
“10S”, “JS”, “QS”, “KS”,
};

Deck(){

}


public void shuffle()
{
    String [] temp = new String[52];
    for ( int i = 0; i < 26; i++){
        temp [2*i] = cards[i];
        temp [2*i+1]= cards[i+26];
     }

    cards = temp;

 }



@Override
public String toString() {

    String cards1 = "";
    for ( int i = 0; i < cards.length; i++){
        cards1 += cards[i] + " ";
        if ((i+1)%13==0){
            cards1 += "\n";
        }
    }
                return cards1;

}


public boolean equals(Deck other) {
        for (int i=0; i<cards.length; ++i) {
            if (!this.cards[i].equals(other.cards[i]))

return false;
}
return true;
}

}

Hi, I need some assistance with my lab hw.

B. It is said that if a deck of cards is given perfect shuffles enough times, it will return to its original order. A perfect shuffle is done by splitting the deck exactly in half and interleaving the cards from the two halves; that is, the first card is from the first half, the second from the second half, the third from the first half and so on.

I need to include the following methods.
-Deck() constructor that creates an unshuffled deck.
-A shuffle() method that does a perfect shuffle.
-A toString() method that print the deck
-An equals(Deck aDeck) method that compares itself with he given deck and returns true if all the cards in both decks are in the same order and false otherwise

I think I am having problems with the constructor part. I don’t know how to create a correct constructor for string arrays. I have three java books, and none of them touched on it.

public class DeckTester {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    Deck d1 = new Deck();

    System.out.println(d1);

    }
}      public class Deck {

String [] cards = {"AH", "2H", "3H", "4H", "5H", "6H", "7H", "8H",

“9H”, “10H”, “JH”, “QH”, “KH”,
“AC”, “2C”, “3C”, “4C”, “5C”, “6C”, “7C”, “8C”,
“9C”, “10C”, “JC”, “QC”, “KC”,
“AD”, “2D”, “3D”, “4D”, “5D”, “6D”, “7D”, “8D”,
“9D”, “10D”, “JD”, “QD”, “KD”,
“AS”, “2S”, “3S”, “4S”, “5S”, “6S”, “7S”, “8S”,
“9S”, “10S”, “JS”, “QS”, “KS”,
};

Deck(){
cards = new String []{}; }

     public void shuffle()
     {
     for ( int i = 0; i< cards.length; i++){
         String temp = cards[ i ]; // swap
         cards[ i ] = cards[ i+25 ]; // the
         cards[ i+25 ] = temp; // cards
     }
 }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T21:10:13+00:00Added an answer on May 20, 2026 at 9:10 pm

    One could assume that the cards will always be the same. So you could have a constant that defines the set of cards available.

    Your constructor then simply allocates memory and copies the constant array to this member variable.

    public class Deck {
      // static final defines a constant valid for all the objects of that class.
      // this will allow us to initiase our array in a clean way.
      // You will notice that constants are always named with capital letters. 
      private static final String[] ORDERED_CARDS = new String[] {
          "AH", "2H", "3H", "4H", "5H", "6H", "7H", "8H",
          "9H", "10H", "JH"
          // etc.
        };
    
      // This is the member variable that holds the cards for our Deck objects
      private String[] cards;
    
      // The constructor allocates memory with the new keyword
      // Then it uses a function from the Java library to copy from one array to the other
      public Deck() {
        cards = new String[ORDERED_CARDS.length];
        System.arraycopy(ORDERED_CARDS, 0, cards, 0, ORDERED_CARDS.length);
      }
    
      // Using Google "java shuffle array" you'll find how to do it in one line using the 
      // Java library. There are other ways to do it, depending on which shuffle type 
      // you need. You could as well implement your own algorithm. The class to generate 
      // random numbers is called Random (part of the Java library)
      public void shuffle() {
        assert(cards!=null);
        Collections.shuffle(Arrays.asList(cards));
      }
    
      // The toString method shows how to iterate over an array or a collection using the 
      // "for-each" loop. We use a StringBuffer to build a string with the resulting card 
      // list. The final keyword says that this variable will not be re-allocated anywhere
      // else during this method. This avoids mistakes in long methods and also allows the
      // Java compiler to optimize your code
      public String toString() {
        final StringBuffer sb = new StringBuffer();
        for (String card : cards) {
          sb.append(card);
          sb.append(", ");
        }
        return sb.toString();
      }
    
      // The equals method shows how to iterate over an array or a collection using the 
      // "for" loop. 
      public boolean equals(Deck other) {
        if (other==null) return false;
    
        // assert is used to ensure that a Deck will always have cards. If not, the program 
        // will throw an Exception. This is good practise when you write classes to assert 
        // that you don't get values that should not be possible.
        assert(other.cards!=null);
    
        // Decks can only be equal if they have the same number of cards
        if (other.cards.length != this.cards.length) return false;
    
        final int cardCount = this.cards.length;
        for (int i=0; i<cardCount; ++i) {
          // Always compare strings using the equals method, not ==
          if (!this.cards[i].equals(other.cards[i])) return false;
        }
    
        // If we reach this point, then we have equal objects
        return true;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update 25/03/2011 I've marked this question as answered, while I don't yet have the
I think everyone here would agree that in order to be considered a professional
What are the expert debugging hints for your favourite language, you think everyone should
UPDATE: Thanks to everyone for the responses. I didn't realize document.write() was deprecated. Add
I now have it set up so that when people go to a thank
What is the SQL query to select all of the MSSQL Server's logins? Thank
SQL is not my forte, but I'm working on it - thank you for
Please let me know what architecture do VoIP applications use, P2P or Client-Server? Thank
I am simply looking for the communities recommendation on an open library to help
Thanks to a Q&A on stackoverflow. I just found out how to determine the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.