Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: What's wrong with my code? Card Program.

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What's wrong with my code? Card Program.

    Hey guys. Run into a small problem on a lab exercise. Supposed to use the method "dealACard()" inside of "dealAHand(int handSize)" Here's my actual instructions.

    "6. The dealACard method returns the Card from the array of Card objects in position nextCard. See the diagram below that illustrates how this should work.

    7. The dealAHand method has a loop that deals one Card (using the previous method) at a time until it has dealt handSize Cards. The String returned is a list of the Card objects in the hand – similar to the result of toString except that the String returned here contains only handSize lines."

    I've attached my code, clearly I can't get the method "dealAHand(int handSize) to work, so I'm just leaving it blank. It does have to return on a String. Thanks for the help! The method I need help with, is the bottom one. Thanks!

    import java.util.Random;
    /**
     * ---------------------------------------------------------------------------
     * File name: Deck.java<br/>
     * Project name: CardProject<br/>
     * ---------------------------------------------------------------------------
     * Creator's name and email: Austin Stanley, Stanleyar@Goldmail.etsu.edu<br/>
     * Course:  CSCI1260-002<br/>
     * Creation Date: Feb 11, 2013<br/>
     * Date of Last Modification: Feb 11, 2013
     * ---------------------------------------------------------------------------
     */
     
    /**
     * Enter type purpose here<br>
     *
     * <hr>
     * Date created: Feb 11, 2013<br>
     * Date last modified: Feb 11, 2013<br>
     * <hr>
     * @author Austin Stanley
     */
    public class Deck
    {
    	private Card[] deck;
    	private int nextCard;
     
    	/**
    	 * Default Constructor <br>        
    	 *
    	 * <hr>
    	 * Date created: Feb 11, 2013 <br>
    	 * Date last modified: Feb 11, 2013 <br>
    	 *
    	 * <hr>
    	 */
    	public Deck()
    	{
    		nextCard = 0;
    		deck = new Card[52];
    		for(int n=0; n<52; n++)
    		{
    			deck[n] = new Card(n);
    		}
    	}
     
     
    	/**
    	 * Copy Constructor <br>        
    	 *
    	 * <hr>
    	 * Date created: Feb 11, 2013 <br>
    	 * Date last modified: Feb 11, 2013 <br>
    	 *
    	 * <hr>
    	 * @param deckIn
    	 */
    	public Deck(Deck deckIn)
    	{
    		deck = new Card[52];
    		for(int i = 0; i < deck.length; i++)
    		deck[i] = deckIn.deck[i];
    		nextCard = 0;
     
    	}
     
    	/**
    	 * Enter method description here <br>        
    	 *
    	 * <hr>
    	 * Date created: Feb 11, 2013 <br>
    	 * Date last modified: Feb 11, 2013 <br>
    	 *
    	 * <hr>
    	 * @return
    	 * @see java.lang.Object#toString()
    	 */
    	public String toString()
    	{
    		String result = "";
    		for(int n=0; n < deck.length; n++)
    		{
    			result += deck[n] + "\n";
    		}
    		return result;
    	}
     
    	/**
    	 * Enter method description here <br>        
    	 *
    	 * <hr>
    	 * Date created: Feb 11, 2013 <br>
    	 * Date last modified: Feb 11, 2013 <br>
    	 *
    	 * <hr>
    	 */
    	public void shuffle()
    	{
    		Random rgen = new Random();
     
    		for(int i=0; i<deck.length; i++)
    		{
    			int randomShuffle = rgen.nextInt(deck.length);
    			Card temp = deck[i];
    			deck[i] = deck[randomShuffle];
    			deck[randomShuffle] = temp;
    		}
    	}
     
    	/**
    	 * Enter method description here <br>        
    	 *
    	 * <hr>
    	 * Date created: Feb 11, 2013 <br>
    	 * Date last modified: Feb 11, 2013 <br>
    	 *
    	 * <hr>
    	 * @return
    	 */
    	public Card dealACard()
    	{
    		int currentCard = nextCard;
    		nextCard++;
    		return deck[currentCard];
     
    	}
     
    	/**
    	 * Enter method description here <br>        
    	 *
    	 * <hr>
    	 * Date created: Feb 11, 2013 <br>
    	 * Date last modified: Feb 11, 2013 <br>
    	 *
    	 * <hr>
    	 * @param handSize
    	 * @return 
    	 */
    	public String dealAHand (int handSize)
    	{
    		for(int i = 0; i < handSize; i++)
    		return "Your hand is"; 
     
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: What's wrong with my code? Card Program.

    where do the cards go as they are dealt?
    Call the dealACard() method once for each card that should be dealt to the hand.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Credit Card String Program
    By hotshotennis in forum What's Wrong With My Code?
    Replies: 13
    Last Post: December 6th, 2012, 04:17 PM
  2. Simple sorting program, can't figure out whats wrong with my code
    By USC CSCE in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 29th, 2012, 08:38 AM
  3. [SOLVED] Need help with Pick A Card Program Please
    By gcjava in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 22nd, 2012, 04:46 AM
  4. [SOLVED] Card Program"War" Class help
    By Usoda in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 16th, 2012, 08:43 PM
  5. code for 3 card brag game
    By jay_tee92 in forum Object Oriented Programming
    Replies: 1
    Last Post: December 20th, 2011, 11:51 AM