0

I have a class Worker which I want to access methods from hive, garden and all the subclasses of the superclass flower. A worker is in an arraylist in hive which is in a garden and the garden also has an arraylist of flowers. I have accessed the methods from hive using the constructor:

public worker(Hive hive){
    this.hive= hive
}

And I want to access the method findFlower() from the garden to get a flower and extractPollen() (which every type of flower inherits from the super class flower) from the flowers I get to get pollen from them. Do I need to make more constructors for garden and for each type of flower or will 2 constructors, 1 for the garden and 1 for the super constructor flower work?

My code so far:

public class Worker extends Bee {

    Hive hive = null; 
    public Garden garden;
    public Flower flower;

    public Worker(Hive hive){
        this.hive=hive;
        this.type = 2;
        this.age=11;
        this.health=3;
    }

    public Bee anotherDay(){
        flower= garden.findFlower();
        flower.extractPollen(int);
        eat();
        age++; 
    }
}

public class Garden{

    ArrayList<Flower> flowerbed = new ArrayList<Flower>();

    public Flower findFlower(){
        //code which returns a random flower from my arraylist
    }
}

public class Flower{

    public boolean extractPollen(int po){
        if(po <= pollen){
            pollen= pollen - po;
            return true;
        }return false;
    }
}
0

2 Answers 2

1

May i suggest this...

A worker can't "work" without both a Hive or Garden. Supply both. Also, flower doesnt need to be a member variable. Its local to anotherDay(). Also, anotherDay() need not return Bee. The caller has reference to your object anyway.

public class Worker extends Bee {
    private Hive hive;
    private Garden garden;

    public Worker(Hive hive, Garden garden){
        super(hive);
        this.hive=hive;
        this.garden = garden;
        this.type = 2;
        this.age=11;
        this.health=3;
    }

    public void anotherDay(){
        Flower flower = garden.findFlower();
        flower.extractPollen(/* some value */);
        eat();
        age++;
    }
}

Another approach is that Hive and Garden are not members at all, but are passed in for each methed call. eg anotherDay(Garden garden); the benefits of this are that your worker can roam gardens. The drawback is that the calling code has to manage the Garden object. These are the trade offs you make as you do OO design :)

13
  • I should have put in my question that there was some code omitted. The another day method needs to return a bee as after a certain number of days the worker will change into another type of bee. The constructor must also be public Worker(Hive hive) as a superclass has that constructor and most of the other subclasses in said superclass need the Hive hive constructor and do not need the garden at all. The anotherDay method is also inherited and the other subclasses which have it would not make sense to take a garden as a paramater!! Thanks for the help so far Dec 10, 2013 at 1:04
  • hmm.. regarding anotherDay() returning Bee. If the method is on an instance of Bee, the return value can only be "this" or another Bee instance all together. I understand you have been given this code to work with, but its not a very good design. The caller would be expected to know that a new Bee may be returned and stop calling the Bee reference it already had. Dec 10, 2013 at 1:13
  • Just a note: Object Inheritance has proven to be much over rated and causes more problems than it solves. There are many articles and opinions on this. The same "results" can be achieved using composition and interfaces. Anyway, I don't mean to start a big discussion about this here. Only to make the point that if you have been given a certain class hierarchy and its not suiting what you need, its not your fault. :) Dec 10, 2013 at 1:27
  • thanks a lot for the help- everything compiles but unfortunately now when I run my code I get a nullpointerexception. Do you know why this is? Dec 10, 2013 at 1:38
  • the stack trace should let you know where that happened. In your above code you have a private Garden garden; but nothing ever sets is. Dec 10, 2013 at 1:45
1

You could try;

public class Garden {

    ArrayList<Flower> flowerbed = new ArrayList<Flower>();
    private static Garden instance;

    public Flower findFlower(){
        //code which returns a random flower from my arraylist
   }

    public static Garden getInstance() {
         if (instance == null) {
              instance = new Garden();
         }
         return instance;
     }
}

This will return an instance of Garden and allow you to access findFlower with

Garden.getInstance().findFlower();

When you call the getInstance() for the first time it will create a new Garden

1
  • i think you forgot the "static" in "private Garden instance;" :) Dec 10, 2013 at 2:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.