Question: Consider the following method: public static int f(int a) { if (a < 0) { return -1; } int n = a; while (n >

Consider the following method:

public static int f(int a)
{
if (a < 0) { return -1; }
int n = a;
while (n > 0)
{

if (n % 2 == 0) // n is even
{
n = n / 2;
}
else if (n == 1) { return 1; }
else { n = 3 * n + 1; }
}
return 0;
}
Perform traces of the computations f(-1), f(0), f(1), f(2), f(10), and f(100).

Step by Step Solution

3.46 Rating (156 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

1 f1 1 will be returned public static int f1 if1 0 condition is true and hence 1 will be returned ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Java Concepts Late Objects Questions!