Skip to content

Instantly share code, notes, and snippets.

@kbredemeier
Created December 19, 2011 00:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kbredemeier/1494970 to your computer and use it in GitHub Desktop.
Save kbredemeier/1494970 to your computer and use it in GitHub Desktop.
se1 uebung 9
public class Activity extends ActivityComponent {
private int time;
public Activity(int t) {
this.time = t;
}
public int count() {
return time;
}
}
public abstract class ActivityComponent {
public void add(ActivityComponent c) {
throw new UnsupportedOperationException();
}
public void remove(ActivityComponent c) {
throw new UnsupportedOperationException();
}
public int count() {
throw new UnsupportedOperationException();
}
}
public class ActivityComponentTest {
public static void main(String[] args) {
ActivityComponent ac = new Process();
ActivityComponent p1 = new Process();
ActivityComponent a1 = new Activity(6);
ActivityComponent a2 = new Activity(5);
ActivityComponent a3 = new Activity(4);
ac.add(a1);
ac.add(p1);
p1.add(a2);
p1.add(a3);
System.out.println(ac.count());
}
}
import java.util.ArrayList;
import java.util.Iterator;
public class Process extends ActivityComponent {
ArrayList components = new ArrayList();
@Override
public void add(ActivityComponent c) {
components.add(c);
}
@Override
public void remove(ActivityComponent c) {
components.remove(c);
}
@Override
public int count() {
int time = 0;
Iterator i = components.iterator();
while(i.hasNext()) {
ActivityComponent ac = (ActivityComponent) i.next();
time = time + ac.count();
}
return time;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment