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

JAVA I will be testing through eclipse Question: Triangle Pattern public static

ID: 669986 • Letter: J

Question

JAVA I will be testing through eclipse

Question: Triangle Pattern
public static void triangle(int m, int n)
// Precondition: m <= n
// 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:
triangle(3, 5) will print this:
***
****
*****
*****
****
***
*/
Hint: Only one of the arguments changes in the recursive call. Which one?
(

Explanation / Answer

class Pyramid
{
public static void main(String args[])
{
int i,j;
for(i=1;i<=3;i++)
{
for(j=1;j<=i;j++)
System.out.print("*");
System.out.print(" ");
}
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
System.out.print("*");
System.out.print(" ");
}
}
}