Write a recursive method with the following specification and header: // Paramet
ID: 3812514 • Letter: W
Question
Write a recursive method with the following specification and header:
// Parameters: m - number of asterisks in the first line
// n - number of asterisks in the middle 2 lines // Precondition: m <= n, m > 0, n > 0
// Postcondition: The method has printed a pattern of 2*(n-m+1) lines
// to the standard output. The first line contains m asterisks, the next
// line contains m+1 asterisks, and so on up to a line with n asterisks.
// Then the pattern is repeated backwards, going n back down to m. /* Example output:
Hint: Only one of the arguments changes in the recursive call. Which one?
***For this method, write a program that would test it. The program should prompt the user to enter value(s) of parameter(s) and execute the method.
Explanation / Answer
public static void triangle(int m, int n)
{
if(m>n)
{
return;
}
for(int i=0;i<m;i++)
{
System.out.print("*");
}
System.out.println("");
triangle(m+1,n);
for(int i=0;i<m;i++)
{
System.out.print("*");
}
System.out.println("");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.