Question

Can someone help me with this? Create a mini program including a main() method and void...

Can someone help me with this?

Create a mini program including a main() method and void methods where arrays are nxn i.e. same no. of rows & columns

1. displayArray(): Takes one parameter, a 2D symmetrical int array called "symmetric" & print the array as shown in the sample output.

2. shiftTheRowsBelow(): Takes one parameter, a 2D symmetrical int array called "symmetric". It will take the given values in the last row of the array, move them to the first row of the array, and shift all the other rows down by one.

P.S. Methods should be able to take in 2D integer arrays of any size & contents. The bottom row is transferred to the top & the one at the top is moved one below & so on.

Inside the main() i/p & o/p:

int[][] n =

{{20,40,60},

{25,45,65},

{30,40,50},

{35,45,55}};

        System.out.println("Prior Shift ");

displayArray(n);

shiftTheRowsBelow(n);

System.out.println("After the Shift ");

         displayArray(n);

O/P:

Prior shift

[20] [40] [60]

[25] [45] [65]

[30] [40] [50]

[35] [45] [55]

After the Shift

[35] [45] [55]

[20] [40] [60]

[25] [45] [65]

[30] [40] [50]

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

Source Code in Java:

class Matrix
{
static void displayArray(int symmetric[][]) //method to display the matrix
{
for(int i=0;i<symmetric.length;i++) //we use nested loops to print the 2D array
{
for(int j=0;j<symmetric[0].length;j++)
System.out.print(symmetric[i][j]+" ");
System.out.println();
}
}
static void shiftTheRowsBelow(int symmetric[][]) //method to shift the last row to the top and the other rows down
{
//we use an temporary 1D array to store the last row of the 2D array
int temp[]=new int[symmetric[0].length];
for(int j=0;j<symmetric[0].length;j++)
temp[j]=symmetric[symmetric.length-1][j];
//we transfer the rows down
for(int i=symmetric.length-1;i>=1;i--)
{
for(int j=0;j<symmetric[0].length;j++)
symmetric[i][j]=symmetric[i-1][j];
}
//we bring the last row stored in the 1D array to the top row
for(int j=0;j<symmetric[0].length;j++)
symmetric[0][j]=temp[j];
}
public static void main(String args[])
{
//testing the functions
int[][] n={{20,40,60},{25,45,65},{30,40,50},{35,45,55}};
System.out.println("Prior Shift ");
displayArray(n);
shiftTheRowsBelow(n);
System.out.println("After the Shift ");
displayArray(n);
}
}

Output:

Add a comment
Know the answer?
Add Answer to:
Can someone help me with this? Create a mini program including a main() method and void...
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
  • Java Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO:...

    Java Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO: Declare matrix shell size // TODO: Create first row // TODO: Generate remaining rows // TODO: Display matrix } /** * firstRow * * This will generate the first row of the matrix, given the size n. The * elements of this row will be the values from 1 to n * * @param size int Desired size of the array * @return...

  • Create a class called Lab7b and in it implement all of the methods below. Also, write...

    Create a class called Lab7b and in it implement all of the methods below. Also, write a main method with test calls to all of these methods. Don’t forget to turn in your file to Canvas before the end of the lab today. int[][] random(int N, int start, int end) returns an N-by-N matrix of random integers ranging from start to end; int rowSum(int[][] a, int i) returns the sum of the elements in row i of the 2-D array...

  • Create a new class called DemoSortingPerformacne Create a private method called getRandomNumberArray. It returns an array...

    Create a new class called DemoSortingPerformacne Create a private method called getRandomNumberArray. It returns an array of Integer and has 2 parameters: arraySize of type int and numberOfDigits of type int. This method should create an array of the specified size that is filled with random numbers. Each random numbers should have the same number of digits as specified in the second parameter In your main method (or additional private methods) do the following: Execute selection sort on quick sort...

  • Can someone finish this class for me? There are comment instructions with what to do in...

    Can someone finish this class for me? There are comment instructions with what to do in the methods already. public class ArrayDemo   {                               public void demonstrateTask1(){                                System.out.println("Demonstrate   Task   1");                                                               int[]   numbers =   null;   //   note   that   numbers   references   nothing   (null)   initially                           ...

  • Assignment is designed to develop your ability to create static methods and manipulate 1D and 2D...

    Assignment is designed to develop your ability to create static methods and manipulate 1D and 2D arrays in Java. Create a single Java class Matrix and inside the class create the specified static methods as described Task # Description 1 Create a matrix (known components) 2 Create a matrix (random components) 3 Create a matrix from vectors 4 Compare two matrices 5 Add two matrices 6 Subtract two matrices 7 Multiply a matrix by a scalar 8 Multiply two matrices...

  • 8.18 Ch 8, Part 3: Tabular Output Write this program using Eclipse. Comment and style the...

    8.18 Ch 8, Part 3: Tabular Output Write this program using Eclipse. Comment and style the code according to CS 200 Style Guide. Submit the source code files (.java) below. Make sure your source files are encoded in UTF-8. Some strange compiler errors are due to the text encoding not being correct. The program you are going to write will produce a String containing a tabular representation of a 2D String array. For the 2D arrays, assume that the first...

  • Java : Please help me correct my code: create a single class (Program11.java) with a main...

    Java : Please help me correct my code: create a single class (Program11.java) with a main method and some auxiliary methods to input a 2-D array from a disk file, input some “transactions” to change the 2-D array and output the changed 2-D array to another file. Your main method will be minimal (see below). Most of the work will go on in your methods. In main, declare (but do not instantiate) 2-D array. Then call the three methods. The...

  • This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static...

    This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static void main(String args[]) throws IOException { Scanner inFile = new Scanner(new File(args[0])); Scanner keyboard = new Scanner(System.in);    TwoDArray array = new TwoDArray(inFile); inFile.close(); int numRows = array.getNumRows(); int numCols = array.getNumCols(); int choice;    do { System.out.println(); System.out.println("\t1. Find the number of rows in the 2D array"); System.out.println("\t2. Find the number of columns in the 2D array"); System.out.println("\t3. Find the sum of elements...

  • Can someone tell me why this is not printing in the screen. I know i commented...

    Can someone tell me why this is not printing in the screen. I know i commented out the first couple functions but the last one is not working. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- #include <stdio.h> #include <stdlib.h> #include <time.h> #define ROWS 5 #define COLS 5 #define FREE 2 #define SCALE 15 #define SHIFT 1 #define MAXVAL 75 #define FALSE 0 #define TRUE 1 void welcomeScreen(); void clearScreen(); void displayExplicitCard(); void displayCard(); void displayRandomCard(); void fillCardRand(); void setValue(); void displayBingoCard(); void initializeArrays (); int main...

  • Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method...

    Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method, but for now leave it empty. Then write a Java method getAverage which takes an array of integers as it’s argument. The method calculate the average of all the elements in the array, and returns that average. The method should work for an array of integers of any length greater than or equal to one. The method signature is shown below: public static double getAverage(...

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