Write a method called printSquare that takes in two integer parameters, a min an
ID: 3630119 • Letter: W
Question
Write a method called printSquare that takes in two integer parameters, a min and a max, and prints the numbers in the range from min to max inclusive in a square pattern. The square pattern is easier to understand by example than by explanation, so take a look at the sample method calls and their resulting console output in the table below.Each line of the square consists of a circular sequence of increasing integers between min and max. Each line prints a different permutation of this sequence. The first line begins with min, the second line begins with min + 1, and so on. When the sequence in any line reaches max, it wraps around back to min.
You may assume the caller of the method will pass a min and a max parameter such that min is less than or equal to max.
Call: printSquare(1,5);
the output should be:
12345
23451
34512
45123
51234
printsquare(0,3);
output:
0123
1230
2301
3012
Explanation / Answer
public static void printSquare(int min, int max)
{
// print max-min rows
for(int row = min; row <= max; row++)
{
// curr is the current number being printed. it starts at row number
int curr = row;
// print max-min columns
for(int col = min; col <= max; col++)
{
// print current number
System.out.print(curr);
// check for wrap-around
if(curr > max)
curr = min;
}
// newline for next row
System.out.println();
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.