Question

Write a method named printGrid that accepts two integer parameters rows and cols. The output is...

Write a method named printGrid that accepts two integer parameters rows and cols. The output is a comma-separated grid of numbers where the first parameter (rows) represents the number of rows of the grid and the second parameter (cols) represents the number of columns. The numbers count up from 1 to (rows x cols). The output are displayed in column-major order, meaning that the numbers shown increase sequentially down each column and wrap to the top of the next column to the right once the bottom of the current column is reached. Assume that rows and cols are greater than 0.

Here are some example calls to your method and their expected results:

Call: printGrid(3, 6); printGrid(5, 3); printGrid(4, 1); printGrid(1, 3);
Output:
1, 4, 7, 10, 13, 16
2, 5, 8, 11, 14, 17
3, 6, 9, 12, 15, 18
1, 6, 11
2, 7, 12
3, 8, 13
4, 9, 14
5, 10, 15
1
2
3
4
1, 2, 3

Method problem: For this problem, you are supposed to write a Java method as described. You should notwrite a complete Java class; just write the method(s) described in the problem statement.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The follwoing is the required method as per the requirments

Method:

public static void printGrid(int r,int c) int count=0; for (int i=1;i<=r ;i++) coun t=1 ; for (int j-1:jK c:j++) System.out.p

Sample output:

Enter the size of the grid 3 6 h, 4, 7, 10, 13, 16 2, 5, 8, 11, 14, 17 3, 6, 9, 12, 15, 18

Enter the size of the grid 4 1 2 4

Code to copy:

public static void printGrid(int r,int c)

     {

          int count=0;

         

          for(int i=1;i<=r;i++)

          {

              count=i;

              for(int j=1;j<=c;j++)

              {

                   System.out.print(count);

                   if(j!=c)

                        System.out.print(", ");

                  

                   count=count+ r;

                  

              }

              System.out.println();

          }

         

         

     }

Add a comment
Answer #2

publicstaticvoidprintGrid(intiRows,intiCols)
{
StringBuildersb=newStringBuilder();
for(intr=1;r<=iRows;++r)
{
for(intc=1;c<=iCols;++c)
{
if(c>1)
{
sb.append(",");
}
sb.append((iRows*(c-1))+r);
}
sb.append("\n");
}

System.out.println(sb.toString());
}

Add a comment
Know the answer?
Add Answer to:
Write a method named printGrid that accepts two integer parameters rows and cols. The output is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a method named printGrid that accepts two integer parameters rows and cols. The output is...

    Write a method named printGrid that accepts two integer parameters rows and cols. The output is a comma-separated grid of numbers where the first parameter (rows) represents the number of rows of the grid and the second parameter (cols) represents the number of columns. The numbers count up from 1 to (rows x cols). The output are displayed in column-major order, meaning that the numbers shown increase sequentially down each column and wrap to the top of the next column...

  • Write a method named printGrid that accepts two integer parameters rows and cols. The output is...

    Write a method named printGrid that accepts two integer parameters rows and cols. The output is a comma-separated grid of numbers where the first parameter (rows) represents the number of rows of the grid and the second parameter (cols) represents the number of columns. The numbers count up from 1 to (rows x cols). The output are displayed in column-major order, meaning that the numbers shown increase sequentially down each column and wrap to the top of the next column...

  • Write a method named factorial that accepts an integer n as a parameter and returns the...

    Write a method named factorial that accepts an integer n as a parameter and returns the factorial of n, or n!. A factorial of an integer is defined as the product of all integers from 1 through that integer inclusive. For example, the call of factorial(4) should return 1 2 3 4, or 24. The factorial of 0 and 1 are defined to be 1. You may assume that the value passed is non-negative and that its factorial can fit...

  • ASSEMBLY LANGUAGE The matrix (two-dimensional array) with ROWS and COLS dimensions of integer values is given....

    ASSEMBLY LANGUAGE The matrix (two-dimensional array) with ROWS and COLS dimensions of integer values is given. Perform various matrix processing activities according to the algorithms below. Store the results into the output vector (one-dimensional array) with appropriate size. For Grade 7) Count the number of odd values (n mod 2 <> 0) for each row. For Grade 9) Calculate the sum of positive values for each column. To obtain inputs and return the results, define appropriate type C/C++ functions. Please...

  • In Java Write a method factorial that accepts an integer parameter n and that uses recursion...

    In Java Write a method factorial that accepts an integer parameter n and that uses recursion to compute and return the value of n factorial (also known as n!). Your method should throw an IllegalArgumentException if n is negative. Several calls and their return values are shown below. Call Output factorial(0); 1 factorial(1); 1 factorial(3); 6 factorial(5); 120 factorial(10); 3628800 factorial(-4); IllegalArgumentException

  • Would somebody be able to assist me with this C++ problem ? E. What would the...

    Would somebody be able to assist me with this C++ problem ? E. What would the code segment in Listing 4 output when it is executed? Listing 4: Code Segment 1 const int ROWS = 2; 2 const int COLS = 3; 3 int x[ROWS] [COLS] = {{2,3,5}, {7,11 , 13}}; 4 int i, j; 5 for (j=0; j<COLS; j++) 6 { cout <<"Column"<<j +1 <<": "; for (i=0; i<ROWS; i++) 11 cout <<x[i][j]<<" "; } cout <<endl; 13 }

  • Assignment: Write a C function that accepts the pointer to a matrix of integer numbers (i.e....

    Assignment: Write a C function that accepts the pointer to a matrix of integer numbers (i.e. a two-dimensional array), number of rows and number of columns as input and prints the matrix after rotating it 90 degrees clockwise. Example: void rotate-matrix (int* matrix, int row, int column) Inputs: row 4, column-6 and the pointer to the matrix below: 2 3 6 5 8 0 Output:

  • Java arrays Create method named repeatedN that takes two integer parameters named range and n and...

    Java arrays Create method named repeatedN that takes two integer parameters named range and n and returns an integer array Method should return an integer array that has numbers 0 - range repeated in order n times If range is less than or equal to 0; return an array that has 0 repeated n times Create a second method named printArray that takes an integer array as a parameter. The method should print out every element on the same line...

  • ****WRITE A JAVA PROGRAM THAT : Write a method named stretch that accepts an array of...

    ****WRITE A JAVA PROGRAM THAT : Write a method named stretch that accepts an array of integers (that the user inputs) as a parameter and returns a new array twice as large as the original, replacing every integer from the original array with a pair of integers, each half the original. If a number in the original array is odd, then the first number in the new pair should be one higher than the second so that the sum equals...

  • Using java : In this exercise, you need to implement a class that encapsulate a Grid. A grid is a useful concept in crea...

    Using java : In this exercise, you need to implement a class that encapsulate a Grid. A grid is a useful concept in creating board-game applications. Later we will use this class to create a board game. A grid is a two-dimensional matrix (see example below) with the same number of rows and columns. You can create a grid of size 8, for example, it’s an 8x8 grid. There are 64 cells in this grid. A cell in the grid...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT