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

I have a question on a java homework I have received. Here is the question, issu

ID: 3545389 • Letter: I

Question

I have a question on a java homework I have received. Here is the question, issues with the code snippet, and my code.

Assume  the availability of a method  named   printStars that can be passed a non-negative integer  n and print a line of n asterisks. Write a method  named   printTriangle that receives a non-negative integer  n and prints a triangle of asterisks as follows: first a line of 1 asterisk, followed by a line of 2 asterisks, and then a line of 3 asterisks, and so on and finally a line of n asterisks. For example, if the method  received 5 it would print:

*

* *

* * *

* * * *

* * * * *

The method  must not use a loop of any kind (for, while, do-while) to accomplish its job. The method  should invoke printStars to accomplish the task of printing a single line.


The issues:

n = 1/  * correct

______________________

n = 2/ ** incorrect

           ***

______________________

n = 3/ **** incorrect

           *****

           ******

______________________

n = 8/ "*******? incorrect

_____________________

public void printTriangle(int n) {

if(n==0)

return;

else {

printStars(printTriangleHelp(n));

printTriangle(n-1);

}

}

private static int length = 0;

public static int printTriangleHelp(int n) {

    if((n - 1) < n) {

        length++;

    } //else {

        //return length;

    //}

    return length;

}

Explanation / Answer

public void printTriangle(int n) {

if(n==0)

return;

else {

printTriangle(n-1);

printStars(n);

}

}