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: Bluej problem with adding an object from one class to another.

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Bluej problem with adding an object from one class to another.

    Bluej problem with adding object from another class to a different class?
    Hi there i am trying to create a fish tank which has fish and a fish feeder, i have created methods and objects for the fish and the fish feeder. But now i am having trouble trying to create a field in the tank class to store the fish feeder object.

    here is the code from the tank class where i am having trouble creating the field for the feeder and also creating code for adding and removing a feeder:

     
     
    public class Tank
    {
    private Fish myFish;
    private boolean full;
    private FishFeeder newFishFeeder;
     
    /**
    * Constructor for objects of class Tank
    */
    public Tank()
    {
    full = false;
    myFish = null; 
    newFishFeeder = null;
    }
     
    /**
    * Add a feeder to the tank
    */
     
    public void addFeeder(FishFeeder feeder)
    {
    if(!full)
    { 
    newFishFeeder = feeder;
    full = true;
    } 
    else
    {
     
    System.out.println("Sorry there is no room in the tank for another feeder");
    }
    }
     
    /**
    * Add a fish to the Tank.
    * @param fish, the fish to the tank
    */ 
    public void addFish(Fish fish)
    {
    // Can only have one fish
    if(!full)
    {
    myFish = fish; 
    full = true;
    }
    else
    {
    System.out.println("Sorry there is no room in the tank. It is already occupied and the resident fish will not share. ");
    } 
    }
     
    /**
    * Remove the fish - e.g. if it has died.
    */ 
    public void removeFish()
    {
    // Can only remove if there is a fish there
    if(full)
    {
    myFish = null;
    full = false;
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Bluej problem with adding an object from one class to another.

    having trouble creating the field for the feeder
    Please explain what the problem is.

    creating code for adding and removing a feeder:
    Please explain what "adding" a feeder means
    Also what "removing" a feeder means
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. bluej some questions about a simple car details class
    By infernocy in forum Object Oriented Programming
    Replies: 5
    Last Post: April 9th, 2012, 05:32 AM
  2. Problem in passing an object class through socket
    By aladin in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 4th, 2012, 08:34 PM
  3. [Help] Problem with Class arrays, adding/editing and deleting
    By Grant_Searle in forum Object Oriented Programming
    Replies: 7
    Last Post: December 16th, 2011, 10:10 AM
  4. adding an object to an arrayList
    By urpalshu in forum Object Oriented Programming
    Replies: 1
    Last Post: November 5th, 2011, 01:04 AM
  5. Replies: 7
    Last Post: July 21st, 2011, 02:29 PM