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 3 of 3

Thread: Loop not creating objects

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Loop not creating objects

    Well I'm trying to use a Linked list and i have a Card class and a Node class. In my deck class I want my default constructor to create a deck full of 52 cards. this is what i have so far:
    package project3;
     
    import java.util.LinkedList;
     
    public class Deck {
     
    	private Node front; 
    	private int remainingCards = 52;
    	LinkedList myDeck;
     
    	public Deck()
    	{
    		for(int i = 0; i < 52; i++)
    		{
    			Node temp = front;
    			if(front == null)
    			{
    				front = new Node(new Card(i), null);
    			}
    			else {
    				temp.setNext(new Node(new Card(i), null));
    				temp.getNext();
    			}
    		}
    	}
        public String toString()
        {
        	String output = new String("Cards \n");
        	Node traveller = front;
        	while(traveller.getNext() != null)
        	{
        		output += traveller.getItem() + "\n";
        		traveller = traveller.getNext();
        	}
        	return output;
        }
    }
    In my main class I have this code :
    		Deck xDeck = new Deck();
    		System.out.println(xDeck.toString());
    which only outputs:
    Cards
    King of clubs
    Why isnt the rest of the deck not printing? Or is the deck not being created properly?

    For reference this is my Card class:
    package project3;
     
    /**
     * <p>Title: The Card class</p>
     *
     * <p>Description: This class will represent a single playing card 
     * that has a value between 1 and 13 and a suit (Clubs, Diamonds, 
     * Hearts or Spades).  A card can return its value, suit, point value 
     * or reference to a String containing the card’s value and suit.  It
     * can check to see if two cards have the same value or the same suit.</p>
     * 
     * @author CSC 120 Instructor
     */
    public class Card
    {
        // instance variables
        private int value;
        private String suit;
     
        /**
         * parameterized Card constructor --
         * gets called when an object of the Card class is instantiated 
         * sending a number as an argument -- it determines the value and 
         * suit of the card based upon the number received
         * @param num a number that gets converted to a value between 1 and 
         * 13 and a suit
         */
        public Card(int num)
        {
            int suitNumber;
            value = num % 13;
            if (value == 0)
                value = 13;
            suitNumber = num / 13;
            if (suitNumber == 0)
                suit = "clubs";
            else if (suitNumber == 1)
                suit = "diamonds";
            else if (suitNumber == 2)
                suit = "hearts";
            else if (suitNumber == 3)
                suit = "spades";
            else
                suit = "ERROR";
        }
     
        /**
         * getValue method -- 
         * returns what's stored in the instance variable value
         * @return the state of the instance variable value
         */
        public int getValue()
        {
            return value;
        }
     
        /**
         * getSuit method -- 
         * returns what's stored in the instance variable suit
         * @return a reference to a String that contains the state of the
         * instance variable suit
         */
        public String getSuit()
        {
            return suit;
        }
     
        /** equalValue method --
         * determines if the otherCard's value is the same as this card's value
         * @param otherCard a reference to the Card object (assumes the object 
         * has been instantiated) to compare to this Card object
         * @return true if the values are equal, false if the values are not equal
         */
        public boolean equalValue(Card otherCard)
        {
            if (this.value == otherCard.value)
                return true;
            else
                return false;
        }
     
        /**
         * equalSuit method -- 
         * determines if the otherCards's suit is the same as this card's suit
         * @param otherCard a reference to the Card object (assumes the object 
         * has been instantiated) to compare to this Card object
         * @return true if the suits are the same, false if they are not
         */
        public boolean equalSuit(Card otherCard)
        {
            if (this.suit.equals(otherCard.suit))
                return true;
            else
                return false;
        }
     
        /**
         * toString method --
         * returns the state of the card object
         * @return a reference to a String object that contains the value 
         * and suit of the card
         */
        public String toString()
        {
            if (value == 1)
                return "Ace of " + suit;
            else if (value == 11)
                return "Jack of " + suit;
            else if (value == 12)
                return "Queen of " + suit;
            else if (value == 13)
                return "King of " + suit;
            else
                return value + " of " + suit;
        }
    }
    and this is my Node class:
    package project3;
     
    /**
     * <p>Title: Node Class</p>
     *
     * <p>Description: Defines a node class capable of storing a 
     * reference to an object and a reference to the next node in
     * a linked list. Accessors and mutators are defined for both.</p>
     *
     * @author Darci Burdge
     */
    public class Node
    {
      private Card data; //a reference to an object of type T
      private Node next; //the address of the next node in the list
     
      /**
       * default constructor - initializes data and next to null 
       */
      public Node()
      {
    	  data = null;
    	  next = null;
      }
     
      /**
       * parameterized constructor - initializes data to the user 
       * specified value; next is set to null
       * @param newItem the value to be stored in the node
       */
      public Node(Card newItem)
      {
        data = newItem;
        next = null;
      }
     
      /**
       * parameterized constructor - initializes data and next to user 
       * specified values
       * @param newItem the object reference to be stored in the node
       * @param nextItem the reference to the next node in the list
       */
      public Node(Card newItem, Node nextItem)
      {
        data = newItem;
        next = nextItem;
      }
     
      /**
       * setItem - stores a new value in data
       * @param newItem the object reference to be stored in the node
       */
      public void setItem(Card newItem)
      {
        data = newItem;
      }
     
      /**
       * setNext - stores a new value in next
       * @param nextItem the reference to be stored in next
       */
      public void setNext(Node nextItem)
      {
        next = nextItem;
      }
     
      /**
       * getItem - returns the reference stored in data
       * @return a reference to the data stored in the node
       */
      public Card getItem()
      {
        return data;
      }
     
      /**
       * getNext - returns the reference stored in next
       * @return the reference stored in next
       */
      public Node getNext()
      {
        return next;
      }
    }


  2. #2
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default Re: Loop not creating objects

    Part of the problem is that temp.getNext(); is not being put into anything so temp does not change? Possibly other issues, this works though

    	public Deck()
    	{
    		Node temp = new Node(new Card(1), null);
    		front = new Node(new Card(0), temp);
    		for(int i = 2; i < 52; i++)
    		{
    				temp.setNext(new Node(new Card(i), null));
    				temp = temp.getNext();
    		}
    	}

    And your not really using linked list are you?

  3. The Following User Says Thank You to Zula For This Useful Post:

    xecure (October 30th, 2010)

  4. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Loop not creating objects

    Quote Originally Posted by Zula View Post
    Part of the problem is that temp.getNext(); is not being put into anything so temp does not change? Possibly other issues, this works though

    	public Deck()
    	{
    		Node temp = new Node(new Card(1), null);
    		front = new Node(new Card(0), temp);
    		for(int i = 2; i < 52; i++)
    		{
    				temp.setNext(new Node(new Card(i), null));
    				temp = temp.getNext();
    		}
    	}

    And your not really using linked list are you?
    I am using a linked list. well atleast I'm supposed to be.

Similar Threads

  1. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  2. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 05:27 PM
  3. Objects
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: January 20th, 2010, 12:50 PM
  4. Replies: 2
    Last Post: October 29th, 2009, 06:13 PM
  5. Object creation from a input file and storing in an Array list
    By LabX in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 14th, 2009, 03:52 AM