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

ASSIGNMENT P11 is the only class in this assignment. You can exercise your metho

ID: 3708939 • Letter: A

Question

ASSIGNMENT

P11 is the only class in this assignment. You can exercise your methods more by extending the main method. We will test your code using a testClient class. Do not change the signature of the specified methods. You can however, and are advised to, create your own helper methods.

You will use ***RECURSION ONLY*** in this assignment, i.e., there will be no loops in any of the methods you implement. Any programs with loops found in them will be given a zero. Here are the methods in P11 that you will implement.

void printPattern(int n)

Precondition: n>0
Postcondition: print a pattern of n+1 lines ( 0 to n ):
line i (i = 0 to n) has i stars ("*"), followed by (n-i) stripes ("-")

For example, printPattern(3) prints:

hint: use a private recursive helper function, with more parameters, that does the work, e.g.:


You may need another helper function...

int convertNum(int[] num)

Precondition: num.length > 0, and num contains digits between 0 and 9 (inclusive)
Postcondition: return int representation of num

For example

hint: use a private recursive helper function, with more parameters, that does the work, e.g.:

Note: Your helper method signature may not match this, it is offered only as an example.

ArrayList intersection( ArrayList AL1, ArrayList AL2)

Precondition: AL1 and AL2 are not empty, and elements in AL1 are unique, and elements in AL2 are unique, (but AL1 and AL2 can contain the same elements)
Postcondition: return an ArrayList with elements that are in both AL1 and AL2, *** in the order they occur in AL1 ***, and *** leave AL1 and AL2 unchanged ***

For example:

P11.JAVA

import java.util.ArrayList;

public class P11 implements IP11 {

@Override

public void printPattern(int n) {

if (n > 0) {

helpPrintPattern(0,n,n);

}

}

private void helpPrintPattern(int stars, int stripes, int count) {

if(stars>count){

return;

}else{

System.out.println();

printStars(stars);

printStripes(count - stars);

helpPrintPattern(++stars,--stripes,count);

}

}

private void printStars(int i){

if (i!=0){

System.out.print("*");

printStars(i-1);

}

}

private void printStripes(int j){

if (j!=0){

System.out.print("-");

printStripes(j-1);

}

}

@Override

public int convertNum(int[] num) {

if (num.length>0){

return helpConvertNum(num, 0, num.length-1, 0);

}return 0;

}

private int helpConvertNum(int[] num, int index, int result, int factor){

if(index <= result){

factor = (int) (((num[index]*Math.pow(10,result))))

}else{

return result;

}

return convertNum(num, --index, result, factor * 10);

}

@Override

public ArrayList<String> intersection(ArrayList<String> AL1, ArrayList<String> AL2) {

}

private void intersection(ArrayList AL1, ArrayList AL2, int AL1Index, int AL2Index, ArrayList output){

}

public static void main(String[] args) {

P11 A3 = new P11();

ArrayList AL1 = new ArrayList();

ArrayList AL2 = new ArrayList();

AL1.add("a"); AL1.add("b"); AL1.add("c");

AL2.add("b"); AL2.add("c"); AL2.add("d"); AL2.add("e");

ArrayList intersect = A3.intersection(AL1,AL2);

System.out.println(AL1 + " intersect " + AL2 + " = " + intersect);

/*prints:

[a, b, c] intersect [b, c, d, e] = [b, c]*/

}

}

IP11.JAVA

import java.util.ArrayList;

public interface IP11 {

/* Method: printPattern

Precondition: n>0

Postcondition: Print a pattern of n+1 ( 0 to n ) lines

Postcondition: line i (i = 0 to n) has i stars ("*") followed by (n-i) stripes ("-")

*/

public void printPattern(int n);

/* Method: convertNum

Precondition: num.length > 0

Postcondition: return int representation of num, (e.g num:{1,2,3} returns int: 123)

*/

public int convertNum(int[] num);

/* Method intersection

  

Precondition: AL1 and AL2 are not empty

Precondition: The elements in AL1 are unique within AL1

Precondition: The elements in AL2 are unique within AL2

(but AL1 and AL2 can contain the same elements)

Postcondition: return an ArrayList with elements that are in both AL1 and AL2

*** in the order they occur in AL1 ***

Postcondition: The elements in AL1 and AL2 are unchanged.

*/

public ArrayList<String> intersection( ArrayList<String> AL1, ArrayList<String> AL2);

}

I HAVE DONE ALL OF THIS SO FAR, I JUST NEED HELP WITH CONVERTNUM, HELPCONVERTNUM, INTERSECTION, AND HELPINTERSECTION. PLEASE NO LOOPS.

Explanation / Answer

Hi Dear,

Please find my implementation using RECURSION .

Output:

testing printPattern
pattern 5:
-----
*----
**---
***--
****-
*****

testing convertNum
[1, 2, 3]: 123

testing intersection
[a, b, c] intersect [b, c, d, e] = [b, c]

Process finished with exit code 0