Rewrite the following Java method (in JAVA) in a functionally equivalent way tha
ID: 3800450 • Letter: R
Question
Rewrite the following Java method (in JAVA) in a functionally equivalent way that does not use any of the ++ and -- operators and the switch ,break, and continue statements
public static int fun(int a int b) t System. out .printf("a %d, b %d n'', a, b) a a-- 2 ++a b) System. out .printf (''a %d, b %d n", a, b) int X b++ ++a; System. out .printf("a %d, b %d,x %d n'', a, b, x) out for (int i 1; i 100, i++) f int j b; if ((x i) 3 0) System. out printf("continue a %d, b %d,x %d n'', a, b, x) Continue while (j 1) if (x++ 5 1) t System. out .printf ("break out a %d, b %d,x %d n'', a, b, x); break out if (j a) System. out printf ("break: a %d,b %d,x %d n'', a, b, x); break X System. out. printf a %d, b %d,x-%d a, b, x); n'', System. out. printf("a %d, b %d,x %d n'', a, b, x); switch (a 5) t case 0: Case 1: X++ break Case 2: default: x x 1 System. out. printf ("a %d,b %d,x %d n", a, b, x) return XExplanation / Answer
public static int fun(int a, int b)
{
System.out.printf("a=%d,b=%d ",a,b);
a += a + 2*(a+b);
System.out.printf("a=%d,b=%d ",a,b);
int x = b + (a+1);
b = b+1;
a = a+1;
System.out.printf("a=%d,b=%d,x=%d ",a,b,x);
for(int i=1;i<=100;i++)
{
int j = b;
if((x+i)%3 ==0)
{
System.out.printf("continue : a=%d,b=%d,x=%d ",a,b,x);
}
else
{
while(j > 1)
{
if(x%5==1)
{
x = x+1;
System.out.printf("break out :a=%d,b=%d,x=%d ",a,b,x);
i=101;
j=0;
}
else
{
x = x+1;
j--;
if(j < a)
{
System.out.printf("break: a=%d,b=%d,x=%d ",a,b,x);
j = 0;
}
else
{
x += i;
System.out.printf("a=%d,b=%d,x=%d ",a,b,x);
}
}
}
}
}
System.out.printf("a=%d,b=%d,x=%d ",a,b,x);
if(a%5 == 0)
{
x = x-1;
x = x+1;
}
else if(a%5 == 1)
{
x = x+1;
}
else if(a%5 == 2)
{
x = x-b;
x = x<<1;
}
else
{
x = x<<1;
}
System.out.printf("a=%d,b=%d,x=%d ",a,b,x);
return x;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.