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
- public class Card{
- private String value_;
- private String suit_;
- public Card(String value, String suit){
- value_=value;
- suit_=suit;
- }
- public String toSting()
- {
- return value_ + "-" + suit_;
- }
- }
Expand|Select|Wrap|Line Numbers
- public class CardApp{
- public static void main (String[] args){
- System.out.print(card);
- newManager = new IOManager(numberOfSwaps);
- newManager.shuffle();
- }
- }
Expand|Select|Wrap|Line Numbers
- import java.util.*;
- public class IOManager{
- private static Scanner input= new Scanner(System.in);
- private int numberOfSwaps;
- private int index1;
- private int index2;
- public void shuffle(){
- numberOfSwaps = input.nextInt();
- int index1 = 0;
- int index2 = 0;
- for(int i=0;i<numberOfSwaps;i++){
- index1=(int)(Math.random()*52);
- index2=(int)(Math.random()*52);
- newPack = new Pack();
- newPack.swap(index1,index2);
- }
- }
- }
Expand|Select|Wrap|Line Numbers
- public class Pack{
- private Card[] cardArray_;
- public Pack(){
- cardArray_=newCard[52];
- }
- public void initialise(){
- String value,suit;
- for(int i=0;i<52;i++){
- suit=i/13;
- value=i%13;
- Card card=new Card(value,suit);
- cardArray_[i]=card;
- }
- public void swap(int index1, int index2){
- Card card=cards[index1];
- cards[index1]=cards[index2];
- cards[index2]=card;
- }
- }
- }