473,799 Members | 3,084 Online
Bytes | Software Development & Data Engineering Community
+ 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 3349
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
3723
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. The task was to use PHP to generate a random bingo card. The standard UK card has nine rows and three columns. Each row has five numbers. All numbers are different, out of a pool of 90. I asked my student to design one card. He came back a...
23
12958
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 C = 0 To 1000 C1 = Cards(Int(Rnd * 52)) ' returns a number from 0 to 51
7
1917
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 strange when I run it. (In windows), when I double-click or run the prog from the IDE, it displays the deck, then exits. When I run it from a console window, if I type the filename eg "cards.exe", it displays the cards as expected but doesn't pause...
7
3099
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); getElementById(cellNum).innerHTML="<img src='imagePicked'>"; I use small 1k.gif images for the cards. Sometimes on the net the 1k.gifs take a while to load & kills bandwith traffic.
8
5558
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 this deck starting from the first of the right segment, then first of the left, second of the right, second of the left. Once one side is exhausted it fills in the rest of the shuffle with the other remaining segment. Any suggestions?
6
3039
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 it has been dealt. I have tested this in a seperate program to see if it work and it does. But I get this error when I try to compile my deck class: 'std::_Vector_iterator<_Ty,_Alloc>...
0
3014
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 someone helps me. Thanks!! ----------------------------------------------------------------------- The code for the Stock class: ----------------------------------------------------------------------- public class Stock { private Deck deck = new...
10
4966
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 implement the following methods: _ _ init _ _ (self, rank, suit) Creates a card. rank is an integer in range of 1 to 13 (Ace:1, King 13), suit is a character in set {'h','d','c','s'} getRank(self) Returns the rank of the card
6
1893
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 and ask. Any help is really appreciated; this should be a piece of cake…driving me crazy. I am writing a simple program (from a book for fun) that creates a deck of cards, shuffles them, gives the user a card, and asks if the next card will be...
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10249
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10219
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10025
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9068
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7563
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6804
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5461
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4138
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.