472,991 Members | 2,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,991 software developers and data experts.

problem in card shuffle

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 3302
mia023
89
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 Expert 1GB
> 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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.