Calling Methods in an Enhanced For Loop
Consider the following Vehicle interface and Fleet class declarations.
public interface Vehicle
{
/** @return the mileage traveled by this Vehicle */
double getMileage();
}
public class Fleet
{
private ArrayList<Vehicle> myVehicles;
/** @return the mileage traveled by all vehicles in this Fleet */
public double getTotalMileage()
{
double sum = 0.0;
for (Vehicle v : myVehicles)
{
sum += /* expression */ ;
}
return sum;
}
// There may be instance variables, constructors, and methods that are not shown.
}
Which of the following expressions can replace /* expression */ so that the getTotalMileage method correctly calculates and returns the total mileage for all vehicles in the fleet?
A
myVehicles.get(v).getMileage()
B
myVehicles[v].getMileage()
C
v.getMileage()
D
getMileage(v)
Question Leaderboard
| Rank | |||||
|---|---|---|---|---|---|
| #1 | kaisuki | 1 | 1 | 0m 00s | 100 |
| #2 | bommasam000 | 1 | 4 | 1m 19s | -9 |
| #3 | gtsak31 | 1 | 1 | 2m 46s | -66 |
| #4 | y.seong2027 | 1 | 2 | 5m 05s | -215 |
Items per page:
10
1 – 4 of 4
