a) Consider the following recursive methods. 01. public class Recursion { /* Assume that n is...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
a) Consider the following recursive methods. 01. public class Recursion { /* Assume that n is no less than 0 and no greater than the length of the array a */ 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. public static boolean ml (int[] a, int n) { if (n <= 0) return true; - if (!m2 (a[n 1])) return false; n - 1); else return ml (a, } /Assume that num is public static boolean m2 (int num) { return m3 (num, 2); } private static boolean m3 (int num, int divisor) { if (num < 2 (num > 2 && num & divisor == 0)) return false; 21. 22. 23. } 24. } if (divisor <= Math.sqrt (num)) return m3 (num, divisor + 1); return true; } public static void main(String[] args) { int[] array(6, 2, 5); ml (array, 3); (i) (10 points) Show the sequence of recursive calls to m2, including arguments, and the return value for each of the following calls to the method m2: Call Sequence of recursive calls to m2 m2 (25) never less than 2 */ m2 (2) m2 (14) m2 (7) m2 (13) Return value (ii) (4 points) Describe what is returned by the method call m2 for any n such that n>=2. (iii) (10 points) Show the sequence of recursive calls to m1, including arguments, and the return value for each of the following calls to the method m1. A call m1 ({2, 6, 5}, 3) means that the first parameter integer array a has the values 2, 6, 5 and the second parameter has value 3. Call ml ({2, 6, 5}, 3) m1 ({23, 3, 7, 33}, 3) m1({23, 3, 7, 33}, 4) m1 ({6, 3, 17, 21, 33}, 5) m1({5}, 1) Sequence of recursive Return value calls to m1 (iv) (6 points) Describe what is returned by the method call m1 (arr, n) for any integer array arr and integer n such that 0 <= n <= arr.length. (v) (30 points) Draw the call stack, or the call tree for the entire execution of the program when you execute the command java Recursion. In each stack frame, include the name and value of all arguments and local variables (you can omit the command-line args). If you choose to draw the call tree, for each method call, you are required to give the method name, and the value for each parameter. b) (15 points) Suppose that a piece of Java code is stored using a String array (e.g, String[] code). Each line is one element of this array. Assume there is a method boolean contains MethodCall (String line) that can check whether a line of code specified by the parameter line contains a method call or not. If the parameter line contains a method call, the method containsMethodCall returns true, otherwise returns false. /** * @precondition arr is non-null * @precondition max is non-negative. * @precondition count is non-negative */ public static boolean noMore ThanNMethod (String[] code, int index, int max, int count) Given the method header shown above, write a recursive method to check whether the parameter array code contains more than max methods. Specifically, if the number of method calls in the piece of code specified by the parameter code is more than the number specified by the parameter max, return false, otherwise return true. Use index to keep track of which array index is being inspected in each recursive call and, count as a counter of the number of method calls. Assume that the parameter arr is not null, the parameter max is non-negative and the parameter count is non-negative. Call the method containsMethodCall (String line) to check whether a line of code contains a method call or not. Assume that contains MethodCall works as intended. Assume that there is at most one method call in a line of code. Example: public static boolean ml (int[] a, int n) { if (n <= 0) return true; } if (!m2 (a[n 1])) return false; else return ml (a, n - 1); Considering the code given above. We will use the String array (whose name is codeArray) shown in the table given below to store the code. Then we want to check whether the code has more than 3 method calls by calling the method: noMoreThanNMethod (codeArray, 0, 3, 0). Since the piece of code only has two method calls, namely codeArray[2] and codeArray [3], the noMoreThanNMethod will return true. The String array codeArray Array index 0 1 2 3 4 String public static boolean m1(int[] a, if (n <= 0) return true; if (!m2 (a[n 1])) return false; else return m1(a, n - 1); } int n) { a) Consider the following recursive methods. 01. public class Recursion { /* Assume that n is no less than 0 and no greater than the length of the array a */ 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. public static boolean ml (int[] a, int n) { if (n <= 0) return true; - if (!m2 (a[n 1])) return false; n - 1); else return ml (a, } /Assume that num is public static boolean m2 (int num) { return m3 (num, 2); } private static boolean m3 (int num, int divisor) { if (num < 2 (num > 2 && num & divisor == 0)) return false; 21. 22. 23. } 24. } if (divisor <= Math.sqrt (num)) return m3 (num, divisor + 1); return true; } public static void main(String[] args) { int[] array(6, 2, 5); ml (array, 3); (i) (10 points) Show the sequence of recursive calls to m2, including arguments, and the return value for each of the following calls to the method m2: Call Sequence of recursive calls to m2 m2 (25) never less than 2 */ m2 (2) m2 (14) m2 (7) m2 (13) Return value (ii) (4 points) Describe what is returned by the method call m2 for any n such that n>=2. (iii) (10 points) Show the sequence of recursive calls to m1, including arguments, and the return value for each of the following calls to the method m1. A call m1 ({2, 6, 5}, 3) means that the first parameter integer array a has the values 2, 6, 5 and the second parameter has value 3. Call ml ({2, 6, 5}, 3) m1 ({23, 3, 7, 33}, 3) m1({23, 3, 7, 33}, 4) m1 ({6, 3, 17, 21, 33}, 5) m1({5}, 1) Sequence of recursive Return value calls to m1 (iv) (6 points) Describe what is returned by the method call m1 (arr, n) for any integer array arr and integer n such that 0 <= n <= arr.length. (v) (30 points) Draw the call stack, or the call tree for the entire execution of the program when you execute the command java Recursion. In each stack frame, include the name and value of all arguments and local variables (you can omit the command-line args). If you choose to draw the call tree, for each method call, you are required to give the method name, and the value for each parameter. b) (15 points) Suppose that a piece of Java code is stored using a String array (e.g, String[] code). Each line is one element of this array. Assume there is a method boolean contains MethodCall (String line) that can check whether a line of code specified by the parameter line contains a method call or not. If the parameter line contains a method call, the method containsMethodCall returns true, otherwise returns false. /** * @precondition arr is non-null * @precondition max is non-negative. * @precondition count is non-negative */ public static boolean noMore ThanNMethod (String[] code, int index, int max, int count) Given the method header shown above, write a recursive method to check whether the parameter array code contains more than max methods. Specifically, if the number of method calls in the piece of code specified by the parameter code is more than the number specified by the parameter max, return false, otherwise return true. Use index to keep track of which array index is being inspected in each recursive call and, count as a counter of the number of method calls. Assume that the parameter arr is not null, the parameter max is non-negative and the parameter count is non-negative. Call the method containsMethodCall (String line) to check whether a line of code contains a method call or not. Assume that contains MethodCall works as intended. Assume that there is at most one method call in a line of code. Example: public static boolean ml (int[] a, int n) { if (n <= 0) return true; } if (!m2 (a[n 1])) return false; else return ml (a, n - 1); Considering the code given above. We will use the String array (whose name is codeArray) shown in the table given below to store the code. Then we want to check whether the code has more than 3 method calls by calling the method: noMoreThanNMethod (codeArray, 0, 3, 0). Since the piece of code only has two method calls, namely codeArray[2] and codeArray [3], the noMoreThanNMethod will return true. The String array codeArray Array index 0 1 2 3 4 String public static boolean m1(int[] a, if (n <= 0) return true; if (!m2 (a[n 1])) return false; else return m1(a, n - 1); } int n) {
Expert Answer:
Related Book For
Quantitative Analysis for Management
ISBN: 978-0132149112
11th Edition
Authors: Barry render, Ralph m. stair, Michael e. Hanna
Posted Date:
Students also viewed these programming questions
-
Kilroy Paper Company distributes specialty papers to big-box stores in ten major U.S. metropolitan areas and plans to consolidate its warehouses into one national distribution center (DC). To...
-
From the Data Bank, choose a sample and use the sign test to test one of the following hypotheses. a. For serum cholesterol, test H 0 : median = 220 milligram percent (mg%). b. For systolic pressure,...
-
The members of a population are numbered 150. a. Use Table I in Appendix A to obtain an SRS of size 6 from the population. Start at the two-digit number in line number 10. b. If you have access to a...
-
Norman Freight purchased a building for $1,100,000 and depreciated it on a straight-line basis over a 30-year period. The estimated residual value was $140,000. After using the building for 15 years,...
-
1. A football team lost 5 yards on each of two consecutive plays. Represent the total loss by a product of integers, and find the product. 2. A horse race bettor lost $50 on each of four consecutive...
-
At the end of Roshek Department Store's fiscal year on December 31, 2015, these accounts appeared in its adjusted trial balance. Freight-In ...................... $ 5,600 Inventory...
-
What role does control of female sexuality have in establishing gender hierarchies?
-
Spring Waters, Inc., produces bottled drinks. The New York Division acquires the water, adds carbonation, and sells it in bulk quantities to the New Jersey Division of Spring Waters and to outside...
-
The Dougherty Furniture Company manufactures tables. In March, the production department had budgeted allocation base (i.e. cost driver) of 4,000 machine-hours. The budgeted manufacturing overheads...
-
Formulate a linear program which can be used to generate a comprehensive plan for the whole Far Eastern operations. Clearly define every variable used in your formulation. Start your report with an...
-
Assume that the banking system has total reserves of Rs.150 billion. Assume also that required reserves are 20 percent of checking deposits and that banks hold no excess reserves and households hold...
-
Explain why economic variables are dynamically interdependent.
-
Explain the meaning of cluster-robust standard errors, and describe how they can be used with pooled least squares, fixed effects, and random effects estimators.
-
Explain how to estimate the VEC and VAR models for the bivariate case.
-
Give some examples of economic decisions in which the observed outcome is a binary variable.
-
Explain how to choose an appropriate model for regression analysis with time-series data.
-
Table 2 shows the impacts for 1 liter gasoline and 1 liter bioethanol over their whole life cycle. Please rank the two options using the hierarchical ranking method based on local and global impacts....
-
The executor of Gina Purcells estate has recorded the following information: Assets discovered at death (at fair value): Cash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....
-
The workload in many areas of bank operations has the characteristics of a nonuniform distribution with respect to time of day. For example, at Chase Manhattan Bank in New York, the number of...
-
Solve the maximal-flow problem presented in the network of Figure below. The numbers in the network represent thousands of gallons per hour as they flow through a chemical processingplant. 3 2 3 0 9...
-
Zuckerman Wiring and Electric is a company that installs wiring and electrical fixtures in residential construction. John Zuckerman has been concerned with the amount of time that it takes to...
-
Form small groups of four to five individuals. Using the worksheet Be the Best We Can Be Team Norms, discuss how the answers to the questions can assist the group with developing team norms so that...
-
Explain why teams and groups are not the same.
-
List the teams of which you are a member. Select one of these teams to analyze. Is it a high performing team? If so, why is it? If not, why isnt it? What changes need to be made to increase the the...
Study smarter with the SolutionInn App