Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please solve them and attach screenshot also TOL2P Pinal Summer T 2016-17 Part (

ID: 3866976 • Letter: P

Question

Please solve them and attach screenshot also

TOL2P Pinal Summer T 2016-17 Part (a), (4 points). Given the following recursive Java method f0 public int f(int nl, int n2) public int f(int n1, int n2) [ if (n2 != 0) return f(n2, n1%n2); else return n1; What would be the return value if the following calls are made? (2 points) Call f (25, 10) f ( 4, 22) Return Value Describe what the method does in a simple sentence: (2 points) Part (b), (6 points). Write a recursive lava method maxElement () which takes a 1-D integer arrays and an integer n as parameters and returns the maximum element in the first n elements of the array. For example, given the following 1-D array: int [ ] arr = {8, 9, 6, 0, 14); 4) will return a value of 9, maxElement maxElement (arr, (arr, n) will return a value of-1 if n is equal to 0. You may assume that arr only contains distinct positive-integers less than 100 and n is always greater than or equal to zero. You are not allowed to use any loop in your implementation. public int maxElement (int[] arr, int n) f Page 12

Explanation / Answer

Question 14 a)

f1(25,10) = 5

Function calls are as follows:

n1 = 25 n2 = 10
n1 = 10 n2 = 5
n1 = 5 n2 = 0
f1(25,10) = 5

f1(4,22) = 2

Function calls are as follows:

n1 = 4 n2 = 22

n1 = 22 n2 = 4

n1 = 4 n2 = 2
n1 = 2 n2 = 0
f1(4,22) = 2

The function returns n1 if n2 is 0 otherwise the function replaces n1 with n2 and n2 with n1%n2, and makes recursive call.

Question 14 b)

Create a file Code.java and paste given code into it.(Filename must be Code.java)

Code.java

import javax.swing.JOptionPane;
import java.util.Scanner;
import java.text.DecimalFormat;

public class Code
{
public static int maxElement(int[] arr, int n)
{
if(n <= 0)
{
return -1;
}
else if(n == 1)
{
return arr[0];
}
else
{
if(arr[n-1] > arr[n-2])
{
int tmp = arr[n-1];
arr[n-1] = arr[n-2];
arr[n-2] = tmp;
return maxElement(arr, n-1);
}
else
{
return maxElement(arr, n-1);
}
}
}
  
public static void main(String[] args)
{
int[] arr = {8,9,6,0,14};
int out1 = maxElement(arr,4);
System.out.println("maxElement(arr,4) = " + out1 + " ");

}
}

Sample Output:

maxElement(arr,4) = 9

Question 15)

Create a file Code.java and paste given code into it.(Filename must be Code.java)

Code.java

import javax.swing.JOptionPane;
import java.util.Scanner;
import java.text.DecimalFormat;
import java.util.*;

public class Code
{
public static Stack<Integer> copyStack(Stack<Integer> OriginalStack)
{
Stack<Integer> odd = new Stack();; Stack<Integer> even = new Stack();
while(!OriginalStack.empty())
{
int a = OriginalStack.pop();
if(a%2 == 1)
{
odd.push(a);
}
else
{
even.push(a);
}
}
Stack<Integer> out = new Stack();
  
while(!even.empty())
{
int a = even.pop();
out.push(a);
}
while(!odd.empty())
{
int a = odd.pop();
out.push(a);
}
return out;
}
  
public static void main(String[] args)
{
Stack<Integer> inp = new Stack();
inp.push(4);inp.push(5);inp.push(6);inp.push(7);inp.push(8);
  
Stack<Integer> out = copyStack(inp);

System.out.println("Output Stack");
while(!out.empty())
{
int a = out.pop();
System.out.println(a);
}


}
}

Sample Output:

Output Stack
7
5
8
6
4