Die Klasse TaskList erweitert die Klasse Task und beinhaltet folgende Methoden:
Dabei bekomme ich folgenden Fehler:

Hier wird also die Anzahl der Aufgaben nicht richtig aktualisiert?? - Mir ist leider noch nicht ganz klar, was ich konkret falsch mache bzw. wie ich dies beheben kann??
Danke für Hilfe!
LG Euler
- public TaskList(String title, int capacity)
- public void lock()
- public void unlock()
- public boolean isLocked()
- public int getCount()
- public boolean addTask(Task task)
- public Task removeTask(String title)
Java:
public class TaskList extends Task {
final Task[] tasks; // NOT private!
private int count;
private boolean locked;
public TaskList(String title, int capacity) {
super(title);
tasks = new Task[Math.max(0, capacity)];
count = 0;
locked = false;
}
/**
* Locks the list against additions and removal from tasks.
*
* After this method is called and until the unlock method
* is called, no task additions or removals may succeed
*/
public void lock() {
locked = true;
}
/**
* Unlocks the list. Task additions and removals may succeed
* until lock() is called.
*/
public void unlock() {
locked = false;
}
/**
* @return true if list is locked, false otherwise
*/
public boolean isLocked() {
return locked;
}
public int getCount() {
int actualCount = 0;
for (Task task : tasks) {
if (task != null) {
actualCount++;
if (task instanceof TaskList) {
actualCount += ((TaskList) task).getCount(); // Zählt auch die in verschachtelten Listen enthaltenen Aufgaben
}
}
}
return actualCount;
}
/**
* Same as in A09 (and template code), plus:
*
* @param task to be added
* @return false and does not add task if:
* - this list is locked or if parameter task is the same as this list
* - another task with same title exists in the task array of this list
* The operation succeeds if another task with same title exists in a nested list (but not in this list).
* If parameter task refers to a task list and it is added successfully, then that list becomes locked.
*/
public boolean addTask(Task task) {
if (!locked && task != null && task.getTitle() != null && !this.equals(task) && !contains(task.getTitle())) {
for (int i = 0; i < tasks.length; i++) {
if (tasks[i] == null) {
tasks[i] = task;
if (task instanceof TaskList) {
((TaskList) task).lock();
count += 1 + ((TaskList) task).getCount(); // Aktualisiere count nur, wenn es sich um eine TaskList handelt
} else {
count++;
}
return true;
}
}
}
return false;
}
/**
* Same as in A09 (and template code), plus:
*
* @param title
* @return null and does not remove if list is locked
* If the removed tasks is a list, then that list becomes unlocked
*/
public Task removeTask(String title) {
if (!locked) {
for (int i = 0; i < tasks.length; i++) {
Task t = tasks[i];
if (t != null && t.getTitle().equals(title)) {
tasks[i] = null;
count--;
if (t instanceof TaskList) {
((TaskList) t).unlock();
count -= ((TaskList) t).getCount(); // Aktualisiere count für die entfernte TaskList
}
return t;
}
}
}
return null;
}
/** Helper */
private boolean contains(String title) {
for (Task t : tasks) {
if (t != null && title.equals(t.getTitle()))
return true;
}
return false;
}
Dabei bekomme ich folgenden Fehler:

Hier wird also die Anzahl der Aufgaben nicht richtig aktualisiert?? - Mir ist leider noch nicht ganz klar, was ich konkret falsch mache bzw. wie ich dies beheben kann??
Danke für Hilfe!
LG Euler