allPositive recurses and will eventually reach the tail which triggers the empty message. It will also call getHead on empty elements.
If you consider an empty list to be all positive, and you don't need the warning message, use:
static boolean allPositive(List a) {
if (a.isEmpty()) {
return true;
} else {
return (a.getHead() >= 0 && allPositive(a.getTail())
}
}
If you need the warning message on empty lists, you would need to do something like:
static boolean allPositive(List a) {
if (a.isEmpty()) {
System.out.println("No elements in list!");
}
// This internal method won't warn on the empty list encountered during recursion.
return allPositiveInternal(a);
}
static boolean allPositiveInternal(List a) {
if (a.isEmpty()) {
return true;
} else {
return (a.getHead() >= 0 && allPositiveInternal(a.getTail())
}
}