Write a method called printSquare that takes in two integer parameters, a min an
ID: 3654062 • 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 lss than or equal to max.
Explanation / Answer
Please rate...
program SquarePattern.java
=====================================================
class SquarePattern
{
public static void main(String args[])
{
printSquare(1,5);
System.out.println("==========================");
printSquare(0,3);
System.out.println("==========================");
printSquare(5,5);
}
public static void printSquare(int min,int max)
{
int i,j,c=0;
int diff=max-min+1;
for(i=min-1;i<max;i++)
{
for(j=i+1;c<diff;j++)
{
System.out.print(j);
if(j>=max)j=min-1;
c++;
}
c=0;
System.out.println();
}
}
}
====================================================
Sample output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.