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

Consider the following simple recursive method: public static void printPattern(

ID: 3633054 • Letter: C

Question

Consider the following simple recursive method:
public static void printPattern(int m, int n) {
if (m == n) {
System.out.println();
return;
}

if (m < n) {
System.out.print("<");
printPattern(m + 1, n);
System.out.print("\"); /* prints a single backslash */
} else {
System.out.print("/");
printPattern(m, n + 1);
System.out.print(">");
}
}

To experiment with this method, you should take the following steps:
Download the file PartOne.java and open it in DrJava. Use the File->Save As (or equivalent) option in your browser to put this file in the folder/directory that you're using for your work on this assignment, and open it in DrJava. You should see a class named PartOne that contains the printPattern method and some other code for part I.
Compile the class.
This class doesn't include a main method, so you can't run it. Instead, you should invoke the printPattern method in the Interactions Pane, preceding the method name with the class name. For example:
PartOne.printPattern(2, 6)
Try several different parameters and see what output is produced in each case. Then answer the following questions:
1. Describe briefly what this method does.
2. What is the base case of the method?
3. Will the method ever produce infinite recursion? In other words, is there a combination of initial parameters that would produce a series of recursive calls that never reaches the base case? Explain your answer briefly.
4. Trace the execution of printPattern(1, 4). Use indentation to indicate which statements are performed by a given invocation of the method, following the approach used in the lecture notes to trace printSeries.
5. Modify the method so that the patterns that it prints are reversed. For example, if the method above prints ///>>> for a given combination of parameters, the revised method should print >>>/// for that same set of parameters


public class PartOne {
public static void printPattern(int m, int n) {
if (m == n) {
return;
}

if (m < n) {
System.out.print("<");
printPattern(m + 1, n);
System.out.print("\"); /* prints a single backslash */
} else {
System.out.print("/");
printPattern(m, n + 1);
System.out.print(">");
}
}

public static int mystery(int a, int b) {
if (a * b == 0) {
return a;
} else {
return b + mystery(a - 1, b - 2);
}
}
}

Explanation / Answer

please rate - thanks

1. Describe briefly what this method does.

if n>m it prints n-m < followed by n-m
example if n=8 and m=5 will print
<<<///
if m>n does the same but first prints / followed by >
example m=8, n=5 will print
\>>>

2. What is the base case of the method?
m and n are equal
3. Will the method ever produce infinite recursion? In other words, is there a combination of initial parameters that would produce a series of recursive calls that never reaches the base case? Explain your answer briefly. I don't think so, the difference is always one or the other conditions

4. Trace the execution of printPattern(1, 4). Use indentation to indicate which statements are performed by a given invocation of the method, following the approach used in the lecture notes to trace printSeries.

print(1,4)

       <
       print(2,4)

             print <

                    print(3,4)

                            print <

                                    print(4,4)

                           print

                  print

        print

done

5. Modify the method so that the patterns that it prints are reversed. For example, if the method above prints ///>>> for a given combination of parameters, the revised method should print >>>/// for that same set of parameters

public class PartOne {
public static void printPattern(int m, int n) {
if (m == n) {
return;
}

if (m < n) {
System.out.print("\");
printPattern(m + 1, n);
System.out.print("<");
} else {
System.out.print(">");
printPattern(m, n + 1);
System.out.print("/");
}
}