Trace this code
show the work
Public static int funcQ1(int m, int n) {
if (m < n)
return 0;
else return 1 + funQ1(m-n, n); }
• What is the value of funcQ1(6, 3), based on the code above?
• What is the value of funcQ1(9, 7), based on the code above?
• What is the value of funcQ1(-5, 1), based on the code above?

1) funcQ1(6, 3) = 1 + funcQ1(3, 3) = 1 + 1 + funcQ1(0, 3) = 1 + 1 + 0 = 2 Answer: 2 2) funcQ1(9, 7) = 1 + funcQ1(2, 7) = 1 + 0 = 1 Answer: 1 1) funcQ1(-5, 1) = 0 (because -5 < 1) Answer: 0
Trace this code show the work Public static int funcQ1(int m, int n) { if (m...
b) Consider the following code. public static int f(int n) if (n == 1) return 0; else if (n % 2 == 0). return g(n/2); else return g(n+1); public static int g(int n) int r = n % 3; if (r == 0) return f(n/3); else if (r == 1) return f(n+2); else return f(2 * n); // (HERE) public static void main(String[] args) { int x = 3; System.out.println(f(x)); (1) (5 points) Draw the call stack as it would...
Use one of these methods for your trace. public static int binarySearchIterative(int[] numbers, int target) { boolean found = false; int first = 0; int last = numbers.length - 1; while (first <= last && !found) { int mid = (first + last) / 2; if (numbers[mid] == target) { targetLocation = mid; found = true; } else if (numbers[mid] < target) { first = mid + 1; } else { // numbers[mid] > target last = mid - 1;...
(b) Trace the following code. // Precondition: n > 0 // <method description here> public static int dothing (int n) { int ans = ; for (int x 1; x <= n; x++) ( for (int y = x; y <= n; y++) ( ans+ } return ans; } (ii) If you were asked to write a one-line description above this method, what would it be? (iii) Could you rewrite the method so that it runs more efficiently?
Consider the following method. public static ArrayList<Integer mystery(int n) ArrayList<Integer seg - new ArrayList<IntegerO; for (int k = n; k > 0; k--) seq.add(new Integer(k+3)); return seq What is the final output of the following Java statement? System.out.println(mystery(3)); a. [1,2,4,5) b. [2,3,4,5) c. [6,5,4,3] d. [7, 7, 8, 8] e. [7,8,9, 8) O Consider the following method: public static int mystery(int[] arr, int k) ifk-0) return 0; }else{ return arr[k - 1] + mystery(arr, k-1):) The code segment below is...
Consider the following method: Linel: public static int mystery(int n) { Line2: if (n < 10) { ine3: return n; Line4: } else { Line5: int a = n/10; Line 6: int b = n % 10; Line 7: return mystery(a + b); Line 8: } Line 9: } What is the result of the following call? System.out.println(mystery(648)); 18 8 12
public static int[] collatz(int start, int numIterations) Given integer start and integer numIterations, return an array containing the Collatz sequence beginning with start up to numIterations. The Collatz function is defined by: 3n + 1 if n is odd n/2 if n is even Given start = 7 and numIterations = 3, this method returns [7, 22, 11, 34] TESTING: collatz(7,3) should return {7, 22, 11, 34} collatz(6,0) should return {6} collatz(6, 5) should return {6, 3, 10, 5, 16,...
public class play { public static final int ONE_PAIR = 1000000; public static final int TWO_PAIRS = 2000000; public static final int THREE_OF_A_KIND = 3000000; public static final int STRAIGHT = 4000000; public static final int FLUSH = 5000000; public static final int FULL_HOUSE = 6000000; public static final int FOUR_OF_A_KIND = 7000000; public static final int STRAIGHT_FLUSH = 8000000; public static boolean isOnepair(Card[] cards) { if (cards.length != 5) return false; if (isFourOfAKind(cards) || isFullHouse(cards) || isThreeOfAKind(cards) ||...
Given the following code: public static void foo3(String s) { if (s.length() >0) { System.out.print(s.charAt(s.length() -1)); foo3(s.substring(0, s.length() -1)); } } What is the output of: foo3(“”); 2, You coded the following in the file Test.java : System.out.println( foo(5)); //more code here public static int foo(int n) //line 9 { if (n = = 0) return 1; else System.out.println(n* foo(n-1) ); } //line 15 At compile time, you get the following error: Text.java: 15: missing return statement } ...
Java, how would i do this
public static void main(String[] args) { int n = 3; int result; result = factorial(n); + public static int factorial(int n) public static int factorial(int n) n = 3 { returns 3* 2 * 1 { if (n == 1) return n; else return (n * factorial(n-1)); if (n == 1) return n; else return (3 * factorial(3-1)); ܢܟ } public static int factorial(int n) n = 2 public static int factorial(int n) returns...
1. public int function(int x, int n) { if (n == 0) return 1; return x * function(x, n -1); } function(3,3) - What is the expected output? 3 12 9 27 2. int fun(int x) { if(x == 0) return 1; else return fun(x - 1); } fun(4) 18 1 24 4 3. Which one of the following calls results 6? int mystery(int n){ if (n == 1) return 1; else return n * mystery(n - 1); } mystery(3)...