it was a lab question for me so can anyone please help me with this lab ! please
ID: 3818987 • Letter: I
Question
it was a lab question for me so can anyone please help me with this lab ! please include the proper java coding
2. Examine this pattern of asterisks and blanks. write a recursive method that can generate exactly this pattern. write the complete demo program to test your recursive method, your recursive method should be capable of producing larger or smaller patterns of the same variety. Also, demonstrate that in the output of the program. Comment in your prog how this fracta. recursive thinking. the method needs or of code (including recursive calls).Explanation / Answer
//Class definition
public class Pattern
{
int value = 1, count = 2, sp = 0;
//To display the pattern
public void disp(int no)
{
//If value is less than 8 and counter is 2
if(value < 8 && count == 2)
{
//If value is odd
if(value % 2 != 0)
{
//Display space
for(int x = 0; x < sp; x++)
System.out.print(" ");
//Display star
System.out.println("* ");
for(int x = 0; x < sp; x++)
System.out.print(" ");
System.out.println("* *");
for(int x = 0; x < sp+2; x++)
System.out.print(" ");
System.out.println("* ");
//Increase the space counter by 4
sp+=4;
//Recursively call the method
disp(value +=3);
}//End of if
//If value is even
else
{
//Display star
for(int x = 0; x < value; x++)
System.out.print("* ");
//Next line
System.out.println();
//Recursively call method by increasing value
disp(++value);
}//End of else
}//End of outer if
//If value reaches 8 decrease the counter by 1
if(value == 8)
count--;
//If counter is 1 and value is less than or equal to 8
if(count == 1 && value <= 8)
{
//if Value is less than one exit
if(value < 1)
System.exit(0);
//If even
if(value % 2 == 0)
{
//if value is not 8 give space
if(value != 8)
for(int x = 0; x < sp; x++)
System.out.print(" ");
//Display star
for(int x = 0; x < value; x++)
System.out.print("* ");
//Next line
System.out.println();
//Recursively call the method by decreasing value
disp(--value);
}//End of inner loop
//if odd
else
{
//Display space
for(int x = 0; x < sp; x++)
System.out.print(" ");
//Display star
System.out.println("* ");
for(int x = 0; x < sp; x++)
System.out.print(" ");
System.out.println("* *");
for(int x = 0; x < sp+2; x++)
System.out.print(" ");
System.out.println("* ");
//Recursively call method by decreasing value
disp(value -=3);
}//End of else
}//End of outer if
}//End of method
//Main method
public static void main(String ss[])
{
Pattern p = new Pattern();
p.disp(8);
}//End of main method
}//End of class
Output:
*
* *
*
* * * *
*
* *
*
* * * * * * * *
*
* *
*
* * * *
*
* *
*
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.