Results 1 to 7 of 7
  1. #1
    alpdog14 is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default build a hybrid tree of fruits

    Ok so I have these interfaces and classes:

    Java Code:
    interface Tree extends Cloneable { int size(); }
    class Fruit implements Tree {
    @Override public int size() { 
    
    return this.size();
    
    }
    }
    class Branch implements Tree {
    private List<Tree> children = new LinkedList<Tree>();
    public List<Tree> getChildren() { return Collections.unmodifiableList(children); }
    public void addChild(Tree tree) { children.add(tree); }
    @Override public int size() { 
    
    int size = 0;
    for(Tree tree: children) size += tree.size();
    return size;
    
    }
    }
    class Mango extends Fruit { /* intentionally left empty */ }
    class Peach extends Fruit { /* intentionally left empty */ }

    And I need to create code that structures this type of hybrid tree:

    the trunk (main branch) of the tree
    a branch with two mangoes
    another branch with
    a (sub)branch with two mangoes
    one peach

    I got the first part:

    Branch trunk = new Branch();

    But not sure how to code the next parts, can anyone help me with this?

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    trunk.addChild(new Mango()) ?

  3. #3
    alpdog14 is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    So would it make sense to nest all the banches together:

    Java Code:
    Branch trunk = new Branch(){ 
    	trunk.addChild(new Mango());
    	trunk.addChild(new Mango()){
    		Branch trunk2 = new Branch(){
    		trunk2.addChild(new Mango());
    		trunk2.addChild(new Mango());
    		trunk2.addChild(new Peach());
    		}
    	}
    }
    Does this look like it would work to output this:

    the trunk (main branch) of the tree

    a branch with two mangoes

    another branch with

    a (sub)branch with two mangoes

    one peach

  4. #4
    jojo is offline Member
    Join Date
    Dec 2009
    Posts
    36
    Rep Power
    0

    Default

    idk what you tried in the code above, but i would simply do it like this:

    Java Code:
    class Tree { List<Branch> branches; addBranch(Branch b) }
    class Branch { List<Branch> subBranches; addFruit(Fruit f) addBranch(Branch b) }
    class Fruit
    class Mango extends Fruit
    class Apple extends Fruit
    
    Tree t = new Tree();
    
    Branch b1 = new Branch();
    b1.addFruit(new Mango());
    b1.addFruit(new Mango());
    t.addBranch(b1);
    
    Branch b2 = new Branch();
    b2.addFruit(new Mango());
    
    Branch sub = new Branch();
    sub.addFruit(new Mango());
    sub.addFruit(new Mango());
    
    b2.addBranch(sub);
    
    t.addBranch(b2);
    Hope it helps!!!

  5. #5
    alpdog14 is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    thanks jojo for your reply, this is helpful. just wondering though I see that you re-instantiated the class I indicated before. My original Tree is an interface that extends Cloneable, if you were to use my original set up, would the output code you indicated still be the same? Or would there be changes?

  6. #6
    jojo is offline Member
    Join Date
    Dec 2009
    Posts
    36
    Rep Power
    0

  7. #7
    alpdog14 is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    Ok Jojo, thanks for confirming, so since you have been such a great help, I thought maybe you can help me further, so with this, I have this example:

    Cluster c1 = new Cluster(); c1.setChild(new Mango()); c1.setMultiplier(3);
    Cluster c2 = new Cluster(); c2.setChild(new Peach()); c2.setMultiplier(5);
    Branch trunk = new Branch(); trunk.addChild(c1); trunk.addChild(c2);

    and I need to implement this class that would make the example work:

    Java Code:
    class Cluster implements Tree {
    
    
    
    public void setChild(Tree child) {
    
    }
    public Tree getChild() {
    
    }
    public void setMultiplier(int howmany) {
    
    }
    public int getMultiplier() {
    
    }
    @Override public int size() {
    
    }
    @Override public Cluster clone() { 
    // make this a deep clone also
    
    }
    }
    Can you help me build these methods out properly? I would be most appreciative.

Similar Threads

  1. HELP: how and where was this build?
    By timosilver in forum New To Java
    Replies: 1
    Last Post: 12-15-2010, 11:53 AM
  2. Writing a hybrid aplication
    By bassfero in forum Java Applets
    Replies: 8
    Last Post: 07-14-2010, 05:33 PM
  3. build search tree on minimax algorithm...
    By me26 in forum Java Gaming
    Replies: 2
    Last Post: 06-29-2010, 09:24 AM
  4. Replies: 0
    Last Post: 04-04-2010, 08:40 AM
  5. Creating a Tree and then saving the Tree
    By jackmatt2 in forum New To Java
    Replies: 0
    Last Post: 08-22-2009, 01:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •