473,471 Members | 2,008 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

problem in card shuffle

19 New Member
the problem is A standard pack of cards can be represented as an array of 52 integers with each number representing a
standard card. Thus:
0 1 2 3 4 5 6 7 8 9 10 11 12.....39 40 41 42 43 44 45 46 47 48 49 50 51
We can shuffle this pack of cards by randomly generating two indexes and swapping those two cards.
For example if we randomly generate the indexes 8 and 50 and swap these two "cards" we would get
(assuming this is the first swap):
0 1 2 3 4 5 6 7 50 9 10 11 12.....39 40 41 42 43 44 45 46 47 48 49 8 51
After a suitable number of swaps have been made the pack can be said to be "shuffled".
Design and implement a Java programme which takes as input the number of desired swaps, outputs an
ordered set of cards which are before the shuffling and output the set of cards after the shuffling. The
number of swaps must be at least 1 (otherwise we would not be doing any "shuffling") and not more
than 1000 (as any more than this would be ridiculous).

i want to use four class to solve this problem. below is my code. can any one help me to change the wrong code into the right one. thank you.
Expand|Select|Wrap|Line Numbers
  1. public class Card{
  2.     private String value_;
  3.     private String suit_;
  4.     public Card(String value, String suit){
  5.         value_=value;
  6.         suit_=suit;
  7.     }
  8.     public String toSting()
  9.     {
  10.         return value_ + "-" + suit_;
  11.     }
  12. }
  13.  
Expand|Select|Wrap|Line Numbers
  1. public class CardApp{
  2.     public static void main (String[] args){
  3.       System.out.print(card);
  4.       newManager = new IOManager(numberOfSwaps);
  5.       newManager.shuffle();
  6.       }
  7.       }
  8.  
Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. public class IOManager{
  3.     private static Scanner input= new Scanner(System.in);
  4.     private int numberOfSwaps;
  5.     private int index1;
  6.     private int index2;
  7.     public void shuffle(){
  8.         numberOfSwaps = input.nextInt();
  9.         int index1 = 0;
  10.         int index2 = 0;
  11.             for(int i=0;i<numberOfSwaps;i++){
  12.             index1=(int)(Math.random()*52);
  13.             index2=(int)(Math.random()*52);
  14.             newPack = new Pack();
  15.             newPack.swap(index1,index2);
  16.         }
  17.     }
  18.     }    
  19.  
Expand|Select|Wrap|Line Numbers
  1. public class Pack{
  2.  
  3.     private Card[] cardArray_;
  4.     public Pack(){
  5.         cardArray_=newCard[52];
  6.     }
  7. public void initialise(){
  8.     String value,suit;
  9.     for(int i=0;i<52;i++){
  10.         suit=i/13;
  11.         value=i%13;
  12.         Card card=new Card(value,suit);
  13.         cardArray_[i]=card;
  14. }
  15. public void swap(int index1, int index2){
  16.     Card card=cards[index1];
  17.     cards[index1]=cards[index2];
  18.     cards[index2]=card;
  19.     }
  20. }
  21. }
  22.  
Jan 9 '08 #1
2 3327
mia023
89 New Member
the problem is A standard pack of cards can be represented as an array of 52 integers with each number representing a
standard card. Thus:
0 1 2 3 4 5 6 7 8 9 10 11 12.....39 40 41 42 43 44 45 46 47 48 49 50 51
We can shuffle this pack of cards by randomly generating two indexes and swapping those two cards.
For example if we randomly generate the indexes 8 and 50 and swap these two "cards" we would get
(assuming this is the first swap):
0 1 2 3 4 5 6 7 50 9 10 11 12.....39 40 41 42 43 44 45 46 47 48 49 8 51
After a suitable number of swaps have been made the pack can be said to be "shuffled".
Design and implement a Java programme which takes as input the number of desired swaps, outputs an
ordered set of cards which are before the shuffling and output the set of cards after the shuffling. The
number of swaps must be at least 1 (otherwise we would not be doing any "shuffling") and not more
than 1000 (as any more than this would be ridiculous).

i want to use four class to solve this problem. below is my code. can any one help me to change the wrong code into the right one. thank you.
Expand|Select|Wrap|Line Numbers
  1. public class Card{
  2.     private String value_;
  3.     private String suit_;
  4.     public Card(String value, String suit){
  5.         value_=value;
  6.         suit_=suit;
  7.     }
  8.     public String toSting()
  9.     {
  10.         return value_ + "-" + suit_;
  11.     }
  12. }
  13.  
Expand|Select|Wrap|Line Numbers
  1. public class CardApp{
  2.     public static void main (String[] args){
  3.       System.out.print(card);
  4.       newManager = new IOManager(numberOfSwaps);
  5.       newManager.shuffle();
  6.       }
  7.       }
  8.  
Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. public class IOManager{
  3.     private static Scanner input= new Scanner(System.in);
  4.     private int numberOfSwaps;
  5.     private int index1;
  6.     private int index2;
  7.     public void shuffle(){
  8.         numberOfSwaps = input.nextInt();
  9.         int index1 = 0;
  10.         int index2 = 0;
  11.             for(int i=0;i<numberOfSwaps;i++){
  12.             index1=(int)(Math.random()*52);
  13.             index2=(int)(Math.random()*52);
  14.             newPack = new Pack();
  15.             newPack.swap(index1,index2);
  16.         }
  17.     }
  18.     }    
  19.  
Expand|Select|Wrap|Line Numbers
  1. public class Pack{
  2.  
  3.     private Card[] cardArray_;
  4.     public Pack(){
  5.         cardArray_=newCard[52];
  6.     }
  7. public void initialise(){
  8.     String value,suit;
  9.     for(int i=0;i<52;i++){
  10.         suit=i/13;
  11.         value=i%13;
  12.         Card card=new Card(value,suit);
  13.         cardArray_[i]=card;
  14. }
  15. public void swap(int index1, int index2){
  16.     Card card=cards[index1];
  17.     cards[index1]=cards[index2];
  18.     cards[index2]=card;
  19.     }
  20. }
  21. }
  22.  
Why don't you sort the cards?Here is a sample code for sorting cards
public void sort() {
for (int i = 0; i < N; i++) {
for (int j = i; j > 0; j--) {
if (cards[j-1].less(cards[j])) {
Card swap = cards[j];
cards[j] = cards[j-1];
cards[j-1] = swap;
}
}
}
}
BE CAREFUL THAT when you compare 2 cards you have to design a method to compare the two corresponding cards.
Jan 9 '08 #2
BigDaddyLH
1,216 Recognized Expert Top Contributor
> can any one help me to change the wrong code into the right one.

You shouldn't just toss assignments into this forum and expect members to do them for you. What do you think is wrong?
Jan 9 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

33
by: Geoff Berrow | last post by:
I may have mentioned that I run an Introduction to PHP course at a local college (very basic - I'm no PHP expert). Well, one of my students was doing really well so I set him some extension work. ...
23
by: JC | last post by:
I am very new to programming and learning on my own. Why do I keep getting duplicate values using this code? I want to shuffle a deck of 52 cards. The logic seems right to me. Randomize For...
7
by: J. Campbell | last post by:
I'm trying to make a class that represents a deck of cards and a dealer for use in a card game. I'm fairly novice. The code compiles (Dev c++(gcc 3.3.1)) without complaint, however it acts...
7
by: Ken Smith | last post by:
I have a little video poker game I created in Javascript. It uses Tables and inner html stuff - for example: (psudo-code) imagePicked=random card image (2h.gif);...
8
by: ericvw | last post by:
How would I shuffle a static array of 52 cards that you input an integer, n, into a function and it takes the first n cards as the left segment and the remaining as the right. Then it shuffles...
6
by: nabeel.girgis | last post by:
I'm creating a deck class which uses a card class. The card class is correct and works perfectly. One fo my member functions is to draw a card, but I'm having trouble removing the top card after...
0
by: Limpor | last post by:
Hello, I am new to learning java, and i am trying to build the class for a calculation card game, unfortunately i can't get the public Card top() and Card takeTop() method in the Stock class. Can...
10
by: Arun Nair | last post by:
Can any one help me with this im not getting it even after reading books because there is not much of discussion anywhere a> Implement a calss that represents a playing card. The class should...
6
by: JNeko | last post by:
Hello all, awesome site! I guess I am technically not a beginner in JAVA, but from my code you would not realize it! I don’t expect anyone to help me with this, but I figure I might as well as try...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.