Summe aller Positiven Integer aus einer Liste mit ActionObject

DeepThought

Neues Mitglied
Hallo erstmal,
ich bin neu hier im Forum.Falls ich also irgendwelche Fehler mache, bitte nicht gleich böse sein. Macht mich einfach darauf aufmersam und ich werde das dann in Zukunft beherzigen.
Nachdem bei google nicht fündig geworden bin und die Suche in diesem Forum auch nicht zum Erfolg geführt hat, hoffe ich auf eure Unterstützung.

Für die Uni soll ich diese Aufgabe lösen:
Lösen Sie mit Hilfe von Aktionsobjekten für generische Listen folgende Probleme:
a) Es soll überprüft werden, ob die Integer-Zahl 0 in der Liste vorkommt.
b) Es soll die Summe aller positiven Integer-Zahlen in der Liste ermittelt werden.
Hinweis: Sie müssen das
Java:
interface ActionObject{
/**
* Abstract function whose realizations
* operate on list nodes
*/
public void action(Node n);
}
für jede Aktion implementieren. Die Knoten sehen wie folgt aus:
Java:
public class Node {
public Object data;
public Node next;
}

Schreiben Sie nun ein kurzes Programm, das mit Hilfe einer gegebenen Liste (List aus der Vorlesung) die
Aktionsobjekte aus a) und b) verwendet und das Ergebnis ausgibt!
Java:
public void showContainsZero(List list) {
// hier soll das Aktionsobjekt aus a) verwendet
// und das Ergebnis ausgegeben werden
}
public void showPosSum(List list) {
// hier soll das Aktionsobjekt aus b) verwendet
// und das Ergebnis ausgegeben werden
}

Was ich programmiert habe funktioniert soweit(bis auf die Ausgabe der Summe). Allerdings hab ich das nicht so gemacht, wie die aufgabe es fordert. Hier der Javacode:
Java:
interface ActionObject{
/**
* Abstract function whose realizations
* operate on list nodes
*/
public void action(Node n);
}

class ContainsZeroAction implements ActionObject{
	public void action(Node n){
		if(n.data instanceof Integer){
			Integer tmp = (Integer)n.data;
			if(tmp.intValue() == 0){
				Out.println("Die Liste enthält die Integerzahl 0");
			}
		}	
	}
}

class PosSumAction implements ActionObject{
	int sum = 0;
	
	public void action(Node n){
		if(n.data instanceof Integer){
			Integer tmp = (Integer)n.data;
			if(tmp.intValue() > 0){
				sum += tmp.intValue();
			}
		}
		Out.println("Die Summe ist: "+sum);
	}
}

class Node {
	public Object data;
	public Node next;
	
	public Node(int data){
		this.data = data;
	}
}

class List{
	Node head = null;
	Node tail = null;
	
	public void add(int e) {
		Node p = new Node(e);
		if(head == null){
			head = p;
		}else{
			tail.next = p;
		}
		tail = p;
	}
	
	public void traverseAndApply(ActionObject p){
		for(Node cursor = head; cursor != null; cursor = cursor.next){
			p.action(cursor);
		}
	}
	
}

public class TestActionObject{
	public static void main(String[] args){
		List l = new List();
		l.add(1);
		l.add(0);
		l.add(2);
		ContainsZeroAction z = new ContainsZeroAction();
		PosSumAction ps = new PosSumAction();
		l.traverseAndApply(z);
		l.traverseAndApply(ps);
	}
}
Mein Probleme sind Folgende:
1. Ich hab ja die beiden forgegebenen Methoden showPosSum und showContainsZero nicht verwendet. Ich hab versucht den code so abzuändern:
Java:
	public void showContainsZero(List list){
		ContainsZeroAction z = new ContainsZeroAction();
		list.traverseAndApply(z);
	}

	public void showPosSum(List list) {
		PosSumAction ps = new PosSumAction();
		list.traverseAndApply(ps);
	}

	public static void main(String[] args){
		List l = new List();
		l.add(1);
		l.add(0);
		l.add(2);
		showContainsZero(l);
		showPosSum(l);
	}
}
Das hat aber auch nicht geklappt. Kann mir jemand Helfen den Code so zu ändern, dass er der Aufgabe entspricht?

Über jegliche Ideen würde ich mich freuen,
Gruß John
 
Lösen Sie mit Hilfe von Aktionsobjekten für generische Listen folgende Probleme

Generisch ist die Liste bei weitem nicht. Da fehler ein paar gezackte Klammern dafür ;-)

Darfst du das Inteface anfassen? Mach doch einfach eine printResult Methode und speicher das Ergebnis im Objekt (ActionObjec). Am Schluss in den show Methoden printResult und gut ists.

EDIT
Ein wenig Code:
Java:
interface ActionObject {
void action(Node n);
void printResult();
}
 
class ContainsZeroAction implements ActionObject{
   
   private int count = 0;
   
   public void action(Node n){
        if(n.data instanceof Integer){
            Integer tmp = (Integer)n.data;
            if(tmp.intValue() == 0){
                count++;
            }
        }   
    }
   
   public void printResult() {
      System.out.println (count);
   }
}
 
class PosSumAction implements ActionObject{
    
   private int sum = 0;
    
    public void action(Node n){
        if(n.data instanceof Integer){
            Integer tmp = (Integer)n.data;
            if(tmp.intValue() > 0){
                sum += tmp.intValue();
            }
        }
       
    }
    
    public void printResult() {
       System.out.println (sum);
    }
}
 
class Node {
    public Object data;
    public Node next;
    
    public Node(int data){
        this.data = data;
    }
}
 
class List{
    Node head = null;
    Node tail = null;
    
    public void add(int e) {
        Node p = new Node(e);
        if(head == null){
            head = p;
        }else{
            tail.next = p;
        }
        tail = p;
    }
    
    public void traverseAndApply(ActionObject p){
        for(Node cursor = head; cursor != null; cursor = cursor.next){
            p.action(cursor);
        }
    }
    
}
 
class TestActionObject{
    public static void main(String[] args){
        List l = new List();
        l.add(1);
        l.add(0);
        l.add(2);
        ContainsZeroAction z = new ContainsZeroAction();
        PosSumAction ps = new PosSumAction();
        l.traverseAndApply(z);
        l.traverseAndApply(ps);
        
        z.printResult ();
        ps.printResult ();
    }
}
 
Zuletzt bearbeitet:

Zurück
Oben