Consider the following class definition
public class Book {
private double cost;
public double getCost() {
return cost;
}
// There may be instance variables, constructors, and methods not shown.
}
The following method appears in a class other than Book. It is intended to sum all the cost instance variables of the Book objects in its ArrayList parameter.
/** Precondition: bookList is not null */
public static double getTotal(ArrayList bookList) {
double total = 0.0;
/* missing code */
return total;
}
Which of the following can replace /* missing code */ so the getTotal method works as intended?
I.
for (int x = 0; x < bookList.size(); x++) {
total += bookList.get(x).getCost();
}
II.
for (Book b : bookList) {
total += b.getCost();
}
III.
for (Book b : bookList) {
total += getCost(b);
}
A. I only
B. II only
C. III only
D. I and II
E. I and III