Ads 468x60px

freak2code is the blog about latest geek news,software reviews ,trends in technology and more informative stuff......

Tuesday 14 August 2012

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


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:

Code java:

 
 
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;
}
}

0 comments:

Post a Comment

Recent Posts