• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Urgent help PLEASE

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My code is as follows for my class file:

class CardShuffle{

// -----------------FIELDS------------------

private int [] deck = new int[51];
public double shuffleDeck;
public double randomPos1;
public double randomPos2;
private int card1;
private int card2;
private double cardDecknums;
public double NumOfShuffles;
public double loop_counter;


// -----------------CONSTRUCTOR -----------------

public CardShuffle(double newNumOfShuffles){
NumOfShuffles = newNumOfShuffles;
}

// ----------------METHOD-------------------

/* Main method */
public void shuffleDeck(){
loop_counter = 0;

while(loop_counter < newNumOfShuffles) {
randomPos1 = (int) (Math.random()*52);
randomPos2 = (int) (Math.random()*52);
card1 = cardDecknums[randomPos1];
card2 = cardDecknums[randomPos2];
cardDecknums[randomPos1] = card2;
cardDecknums[randomPos2] = card1;
loop_counter++;
}

// Populate array

public void populatenumsArray(){
while(loop_counter != 52) {
cardDecknums[loop_counter] = index;
loop_counter ++;
index ++;
}
}

please cna someone help with the code:
the question ive posted a few days ago is as follows:

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 then "shuffled" and output again. The number of swaps must be at least 1 and not more than 1000.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>please cna someone help with the code:
>the question ive posted a few days ago is as follows:

and the responses were for you to post what specifically is giving you problems.

Your (near) duplicate post in the advanced forum started
"I challenge anyone to write...."
which indicates (to me) you're after a hand-out.
 
Chris Lavery
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and.....
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, just from "eyeballing" the code, here are some obvious errors:

- randomPos1 and randomPos2 are declare as doubles when you used them as int everywhere.
- cardDecknums is not declared as an array, but you use them everywhere as an array.
- deck variable is not used.
- loop counter is not initialized in your populate method.

Hope this helps,
Henry
 
Chris Lavery
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks henry,
any idea how to inislise loop counter??
im having prblems with it
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chris Lavery:
thanks henry,
any idea how to inislise loop counter??
im having prblems with it



The same way you did it for your other method.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>and.....

"Do you want fries with that?"
 
Chris Lavery
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx again,
i think this should be it:

class CardShuffle{

// -----------------FIELDS------------------

private int [] cardDecknums = new int[52];
public double shuffleDeck;
public int randomPos1;
public int randomPos2;
private int card1;
private int card2;
public int NumOfShuffles;
public int loop_counter;
public int index = 1;


// -----------------CONSTRUCTOR -----------------

public CardShuffle(int newNumOfShuffles){
NumOfShuffles = newNumOfShuffles;
}

// ----------------METHOD-------------------

// Populate array

public void populatenumsArray(){
loop_counter = 0;

while(loop_counter != 52) {
cardDecknums[loop_counter] = index;
loop_counter ++;
index ++;
}
}

// Shuffle
public void shuffleDeck(){
loop_counter = 0;

while(loop_counter < NumOfShuffles) {
randomPos1 = (int) (Math.random()*52);
randomPos2 = (int) (Math.random()*52);
card1 = cardDecknums[randomPos1];
card2 = cardDecknums[randomPos2];
cardDecknums[randomPos1] = card2;
cardDecknums[randomPos2] = card1;
loop_counter++;
}
}
}

any ideas for how to divide into sets eg clubs, diamonds, hearts and spades???
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>any ideas for how to divide into sets eg clubs, diamonds, hearts and spades???

String[] suit = {"Hearts","Diamonds","Clubs","Spades"};
String[] face = {"Ace","Two","Three"....."King"};

int card = 21;

System.out.println(face[card%13]+" of "+suit[card/13]);

but you should explore making Card objects
 
Chris Lavery
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class CardShuffle{

// -----------------FIELDS------------------

private int [] cardDecknums = new int[1000];
public int randomPos1;
public int randomPos2;
private int card1;
private int card2;
public int NumOfShuffles;
public int loop_counter;
public int index = 1;
public int suit;
public int face;

// -----------------CONSTRUCTOR -----------------

public CardShuffle(int newNumOfShuffles){
NumOfShuffles = newNumOfShuffles;
}

// ----------------METHOD-------------------

// Populate array

public void populatenumsArray(){
loop_counter = 0;

while(loop_counter != 52) {
cardDecknums[loop_counter] = index;
loop_counter ++;
index ++;
}
}


// Suit
public void suit(){

String[] suit = {"Hearts","Diamonds","Clubs","Spades"};
String[] face = {"1","2","3", "4","5","6","7","8","9","10","Jack","Queen","King"};

int card = NumOfShuffles;

System.out.println (face[card%13]+" of "+suit[card/13]);
}

// Shuffle
public void shuffleDeck(){
loop_counter = 0;

while(loop_counter < NumOfShuffles) {
randomPos1 = (int) (Math.random()*52);
randomPos2 = (int) (Math.random()*52);
card1 = cardDecknums[randomPos1];
card2 = cardDecknums[randomPos2];
cardDecknums[randomPos1] = card2;
cardDecknums[randomPos2] = card1;
loop_counter++;
}
}
}


any ideas as to why the cards wont shuffle more than 52 times as opposed to 1000???
 
Chris Lavery
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry, i mean to add, when executed the error reads:
for the value 666:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 51
at CardShuffle.suit(CardShuffle.java:49)
at ShuffleApp.main(CardShuffleApp.java:47)
 
Chris Lavery
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
also, any ideas as to how to get the program to ouptut all the results of the shuffles as opposed to just the final end result
thanks everyone for your help
Chris
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) Your first problem is here
private int [] cardDecknums = new int[1000];

cardDecknums is what you are shuffling, in shuffleDeck(), so presumably
cardDecknums should represent the 52 cards i.e.
private int [] cardDecknums = new int[52];

2) if NumOfShuffles > 51 here
int card = NumOfShuffles;
System.out.println (face[card%13]+" of "+suit[card/13]);

you will get an array bounds error here
suit[card/13]
i.e. suit element numbers are 0 to 3
(replace suit() with below printDeck())

3) Not sure you really want the result of each individual shuffle, perhaps
printing the deck before, and after, shuffling the deck is all you need.

 
Chris Lavery
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you michael
last question i promise,
my code now works and will do what i ask, only problem is that for any number i enter the result is always 9 of diamonds unless i manually change the line:
int card = 21;
in the suit method.
any ideas

p.s. You have no idea how grateful i am
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
scrap the suit() method, unless you want to print just an individual card
then it would be something like this (changing name suit to printCard)

 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chris Lavery:
thank you michael
last question i promise,
my code now works and will do what i ask, only problem is that for any number i enter the result is always 9 of diamonds unless i manually change the line:
int card = 21;
in the suit method.
any ideas



Chris, you are taking what people are providing too literally. That code snippet Michael provided was an example, set card variable to the actual card you want to pick.

Henry
 
Chris Lavery
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so would that not mean selecting NumOfTerms for my int card value so that it take the number input by the user???
whenever i do that however it will do the values of 1 - 52, just one deck, however if i want more, i get the error message up again:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 51
at CardShuffle.suit(CardShuffle.java:49)
at ShuffleApp.main(CardShuffleApp.java:47)
 
Chris Lavery
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry, meant to say NumOfShuffles
 
Chris Lavery
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my code as follows:

class CardShuffle{

// -----------------FIELDS------------------

private int [] cardDecknums = new int[52];
public int randomPos1;
public int randomPos2;
private int card1;
private int card2;
public int numOfShuffles;
public int loop_counter;
public int index = 1;
public int suit;
public int face;

// -----------------CONSTRUCTOR -----------------

public CardShuffle(int newnumOfShuffles){
numOfShuffles = newnumOfShuffles;
}

// ----------------METHOD-------------------

// Populate array

public void populatenumsArray(){


while(loop_counter != 52) {
cardDecknums[loop_counter] = index;
loop_counter ++;
index ++;
}
}


// Print Deck
public void printDeck(){

String[] suit = {"Hearts","Diamonds","Clubs","Spades"};
String[] face = {"1","2","3", "4","5","6","7","8","9","10","Jack","Queen","King"};

int card = numOfShuffles;

System.out.println (face[card%13]+" of "+suit[card/13]);
}

// Shuffle
public void shuffleDeck(){

while(loop_counter < numOfShuffles) {
randomPos1 = (int) (Math.random()*52);
randomPos2 = (int) (Math.random()*52);
card1 = cardDecknums[randomPos1];
card2 = cardDecknums[randomPos2];
cardDecknums[randomPos1] = card2;
cardDecknums[randomPos2] = card1;
loop_counter++;
}
}
}

CardShuffleApp file

import java.io.*;

class CardShuffleApp {


// ----------------FIELDS--------------------

// Create BufferedReader class instance

public static InputStreamReader input =
new InputStreamReader(System.in);

public static BufferedReader keyboardInput = new
BufferedReader(input);

// ---------------METHODS--------------------

/* Main Method*/

public static void main(String[] args) throws IOException {

// Input no.of shuffles
System.out.println ("Please enter number of times for cards to be shuffled");
int numOfShuffles = Integer.parseInt(keyboardInput.readLine());

//Check value
if (numOfShuffles > 1000 || numOfShuffles < 1){
System.out.println("Please enter between 1 and 1000");
}

else{
// Create Instance

CardShuffle newCardShuffle = new CardShuffle(numOfShuffles);

// Calculate array

newCardShuffle.populatenumsArray();

// Calculate suit

newCardShuffle.printDeck();

// Shuffle

newCardShuffle.shuffleDeck();

}
}
}
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public int index = 1;

should be
public int index = 0;

and printDeck() is not as per my previous post
 
Chris Lavery
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can u try compiling the code yourself, cause i cant get it working by following what your saying
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Compiles/runs OK, but I had changed the capitalization of cardDecknums
to cardDeckNums (personal pref.)

Here's printDeck() to math your capitalization



should work OK for you now.
 
Chris Lavery
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for you help,
now iv discovered the problem that the shuffle method doesnt seem to shuffle the cards.
any suggestions??
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Post your current (working) program, so the 'unshuffled' output can be seen
 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Random thought on a cleaner implementation of shuffling: Collections.shuffle(List list)
 
Hey cool! They got a blimp! But I have a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic