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: Populate an Object with Other Objects

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Populate an Object with Other Objects

    I am using inheritance in a pretty standard exercise here.

    I have a Card class and then a few classes that extend the Card class of different Card Types.

    I however have a different type of class that still extends my Card class but is dealing with a LinkedList.

    I have all of this accomplished, however in the exercise, there is a step that states "Have a main() method populate a Billfold object with Card objects, and then call
    printCards()."


    Here is the BillFold class.

    import java.util.*;
     
    public class Billfold extends Card
    {
    	LinkedList<Card> cards;
     
    	Card firstCard;
    	Billfold billf;
     
     
    	public static void main(String[] args)
    	{
     
    		billf = new Billfold();
     
    		printCards();
    	}
     
     
    	public Billfold()
    	{
     
    		cards = new LinkedList<Card>();
     
     
    	}
     
     
    	public void addCard(Card newCard)
    	{
    		if(firstCard == null)
    			firstCard = newCard;
    		else
    			cards.add(newCard);
    	}
     
    	public void printCards()
    	{
    		for(int i = 0; i < cards.size(); i++)
    		{
     
    			if(cards.get(i) != null)
    			{
    				super.print();
    			}
    		}
    	}
    }

    And then here is the original Card class BillFold is extending
       import java.util.*;
       import java.io.*;
     
     
       public class Card
       {
          private String name;
          private Card nextCard;
     
    		public Card()
          {
             name = "";
          }
          public Card(String n)
          {
             name = n;
          }
          public Card getNext()
          {
             return nextCard;
          }
          public boolean isExpired()
          {
             return false;
          }
          public void print()
          {
             System.out.println("Card holder: " + name);
          }
          public void setNext(Card c)
          {
             nextCard = c;
          }
     
       }

    So my question is: What does it mean to populate a Billfold object with Card objects in my Billfold Class?

    *Don't be confused by my inheritance info, this question is not about inheritance.

    Thanks in advance.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,262
    Thanks
    176
    Thanked 820 Times in 763 Posts
    Blog Entries
    5

    Default Re: Populate an Object with Other Objects

    Quote Originally Posted by EmSaint View Post
    So my question is: What does it mean to populate a Billfold object with Card objects in my Billfold Class?
    Your guess is as good as anyone else's. I would presume it means to add cards to the Billfold class.

  3. #3
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    489
    Thanks
    8
    Thanked 45 Times in 43 Posts

    Default Re: Populate an Object with Other Objects

    First and foremost, having Billfold extend Card is totally inappropriate. Inheritance implies a is-a relationship. Would you say Billfold is-a Card?

    db

Similar Threads

  1. Replies: 1
    Last Post: December 22nd, 2011, 08:55 AM
  2. 2D Object makes my object smaller, Why?
    By MassiveResponse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 15th, 2010, 02:33 PM
  3. How to populate a switch statement with a linked list
    By macnasty in forum Collections and Generics
    Replies: 2
    Last Post: May 6th, 2010, 10:47 PM
  4. Objects
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: January 20th, 2010, 11:50 AM
  5. Arrays Of Objects?
    By MysticDeath in forum Object Oriented Programming
    Replies: 2
    Last Post: October 20th, 2009, 09:39 PM