[JAVA] The first two are to rewrite methds in Java, the 3rd one deals with heap
ID: 3807701 • Letter: #
Question
[JAVA] The first two are to rewrite methds in Java, the 3rd one deals with heap referencepoint and nodes I believe.
l. Rewrite the following Java method in a functionally equivalent way that does not use any recursion. void f (int n) 1 System.out. println(n); if (n 1) if ((n & 1) e) f(n/2); else f (3*n+1 2. Rewrite the following Java method in a functionally equivalent way that does not use any loop int f(int a int k) f int i -1; for (int j 0; j a length; j++) if (a[j] k) i return i 3. A Java class is defined as class Node Node next; In the following program fragment, determine the reference counts for the three Node objects at the end of each line. 1. Node p new Node 2 Node q new Node 3 Node r new Node 4. p next q; 5. q.next E r, 6. r next p; 7. p q r nullExplanation / Answer
Ans 1:
void f(int n)
{
System.out.println(n);
if(n>1)
{
while((n&1)==0)
{
n=n/2;
System.out.println(n);
}
}
else
{
n=3*n+1;
while(n<=1)
{
if(n>1)
{
while((n&1)==0)
{
n=n/2;
System.out.println(n);
}
}
}
}
}
Ans 2:
int fu(Integer[]a,int k)
{
int i=-1;
i=Arrays.asList(a).indexOf(k);
return i;
}
Ans 3:
Line P Q R
1 1 0 0
2 0 1 0
3 0 0 1
4 1 0 0
5 0 1 0
6 0 0 1
7 1 1 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.