Write a Java program to display a specified pattern. Within your program you mus
ID: 3605137 • Letter: W
Question
Write a Java program to display a specified pattern. Within your program you must define a recursive method with the following signature. public static void Patternl(int m, int n) Your program will ask the user to input two integers, the first one is for the number of the row in the pattern and the second one, which is between 0 and 9 inclusively, is for the entries in the pattern, and your program invokes the method. A sample run of your program is as follows. Please input the first integer. Please input the second integer. The pattern is as follows. Another sample run of your program is as follows. Please input the first integer. Please input the second integer. The pattern is as follows.Explanation / Answer
import java.util.Scanner;
// Class Pattern definition
public class Pattern
{
// Recursive method to display pattern based on m and n value
// Variable m for number of rows
// Variable n for number to print
public static String pattern1(int m, int n)
{
// Checks if number of rows is less than or equals to zero return null
if( m <= 0 )
return "";
// Recursively calls the method by reducing the size of row by one each time
String p = pattern1(m - 1, n);
// Concatenates the number with string
p = p + n;
// Displays the string
System.out.println(p);
return p;
}// End of method
// main method definition
public static void main(String[] args)
{
// Scanner class object created
Scanner sc = new Scanner(System.in);
// Accepts data from the user
System.out.print(" Please input the first integer: ");
int first = sc.nextInt();
System.out.print(" Please input the second integer: ");
int second = sc.nextInt();
// Calls the method to display the result
pattern1(first, second);
}// End of main method
}// End of class
Sample Run 1:
Please input the first integer: 3
Please input the second integer: 0
0
00
000
Sample Run 2:
Please input the first integer: 5
Please input the second integer: 9
9
99
999
9999
99999
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.