Problem of the Day
Problem:
A t-shirt can be considered to have four holes in it: one hole for the head, two for the arms, and one at the bottom for the body. The static method holes
, given a number of shirts num
, is intended to calculate the total number of holes in the those shirts. Which of these static methods works as intended?
public static int holes(int num)
{
return num * 4;
}public static int holes(int num)
{
if (num <= 0)
return 0;
else
return 4 + holes(num - 1);
}public static int holes(int num)
{
int holeCount = 0;
for (int i = 0; i < num; i++)
holeCount += 4;
return holeCount;
}
- I only
- I and II only
- I and III only
- I, II, and III
The correct answer is d. The first strategy uses multiplication to quickly arrive at the answer, the second strategy uses recursion, and the third strategy uses a loop to calculate the total number of holes in the shirts.