I 
		
				
			
		Incubus
Gast
Hilfeeeeee!  :bahnhof: 
Ich habe folgendes Problem:
Ich soll mit Hilfe des Composite Patterns einen Ausdruck richtig ausgeben.
Ausdruck --> (((a+b)*(a-c))+((b*d)-a)) --> Die Klammern werden in der Ausgabefunktion hard gecoded
Bisher habe ich folgenden Code welcher aber noch nicht wirklich richtig funktioniert:
	
	
	
	
	
		
	
Die Ausgabe bei diesem Test lautet jetzt aber "+ +ab *cd" und nicht wie gewünscht a+b + c*d
Der Binärbaum wird leider Falsch abgearbeitet... :autsch:
Kann mir bitte einer helfen???
Thnx im Vorraus
			
			Ich habe folgendes Problem:
Ich soll mit Hilfe des Composite Patterns einen Ausdruck richtig ausgeben.
Ausdruck --> (((a+b)*(a-c))+((b*d)-a)) --> Die Klammern werden in der Ausgabefunktion hard gecoded
Bisher habe ich folgenden Code welcher aber noch nicht wirklich richtig funktioniert:
		Code:
	
	interface Component {
   public String defaultMethod();
   public ArrayList<Component> getChildren();
   public boolean addComponent(Component c);
   public boolean removeComponent(Component c);
}
class Composite implements Component {
   private String id;
   private ArrayList<Component> components = new ArrayList<Component>();
   public Composite(String identification)         { id = identification; }
   [b]public String defaultMethod() { 
       String s =  id ; 
       for (Component child : getChildren())
           s = s +   child.defaultMethod();
       return s ;[/b]
   }
   public ArrayList<Component> getChildren()       { return components; }
   public boolean addComponent(Component c)        { return components.add(c); }
   public boolean removeComponent(Component c)     { return components.remove(c); }
}
   
class Leaf implements Component {
   private String id;
   public Leaf(String identification)              { id = identification; }
   public String defaultMethod()                   { return id; }
   public ArrayList<Component> getChildren()       { return null; }
   public boolean addComponent(Component c)        { return false; }
   public boolean removeComponent(Component c)     { return false; }
}
class CompositePattern {
   public static void main(String[] args) {
       Composite Plus = new Composite("+");
       Composite Plus1 = new Composite("+");
       Composite Mal = new Composite("*");
       Leaf a = new Leaf("a");
       Leaf b = new Leaf("b");
       Leaf c = new Leaf("c");
       Leaf d = new Leaf("d");
       Plus.addComponent(Plus1);
       Plus.addComponent(Mal);
       Plus1.addComponent(a);
       Plus1.addComponent(b);
       Mal.addComponent(c);
       Mal.addComponent(d);
       
        
       System.out.println( Plus.defaultMethod() );
  
   }
}
	Die Ausgabe bei diesem Test lautet jetzt aber "+ +ab *cd" und nicht wie gewünscht a+b + c*d
Der Binärbaum wird leider Falsch abgearbeitet... :autsch:
Kann mir bitte einer helfen???
Thnx im Vorraus