Question

In: Computer Science

Download the SwapMultidimensional.java file, and open it in jGrasp (or a text editor of your choice)....

Download the SwapMultidimensional.java file, and open it in jGrasp (or a text editor of your choice). This program contains two methods which you will need to write:

  • swapRows: Swaps the contents of two rows, given a two-dimensional array and the indices of the rows to swap.
  • swapCols: Swaps the contents of two columns, given a two-dimensional array and the indices of the columns to swap.

main contains some code which will call swapRows and swapCols, for the purpose of informal testing. With this in mind, example output of the program is below:

Before swapping rows 0 and 2:
0 1 2
3 4 5
6 7 8
After swapping rows 0 and 2:
6 7 8
3 4 5
0 1 2

Before swapping columns 1 and 2:
9 8 7
6 5 4
3 2 1
After swapping columns 1 and 2:
9 7 8
6 4 5
3 1 2

-----------------------------------------------------------------------

public class SwapMultidimensional {
    // You must write TWO methods:
    //
    // 1.) A method named "swapRows" that swaps the
    //     contents of two rows in a given two-dimensional
    //     array. This method MUST NOT use loops. As a hint,
    //     since a two-dimensional array is just an array of
    //     arrays where each inner array is a row, this means
    //     that you can swap entire rows just by swapping two
    //     elements of the outer array.
    //
    // 2.) A method named "swapCols" that swaps the contents
    //     of two columns in a given two-dimensional array.
    //     This method MUST use a loop.
    // TODO - write your code below this comment.


    // DO NOT MODIFY print2D!
    public static void print2D(int[][] array) {
        for (int row = 0; row < array.length; row++) {
            for (int col = 0; col < array[row].length - 1; col++) {
                System.out.print(array[row][col] + " ");
            }
            System.out.println(array[row][array[row].length - 1]);
        }
    }

    // DO NOT MODIFY main!
    public static void main(String[] args) {
        int[][] example1 = new int[][]{ new int[]{0, 1, 2},
                                        new int[]{3, 4, 5},
                                        new int[]{6, 7, 8} };
        int[][] example2 = new int[][]{ new int[]{9, 8, 7},
                                        new int[]{6, 5, 4},
                                        new int[]{3, 2, 1} };
        System.out.println("Before swapping rows 0 and 2:");
        print2D(example1);
        swapRows(example1, 0, 2);
        System.out.println("After swapping rows 0 and 2:");
        print2D(example1);

        System.out.println("\nBefore swapping columns 1 and 2:");
        print2D(example2);
        swapCols(example2, 1, 2);
        System.out.println("After swapping columns 1 and 2:");
        print2D(example2);
    }
}

Solutions

Expert Solution

If you have any doubts, please give me comment...

public class SwapMultidimensional {

// You must write TWO methods:

//

// 1.) A method named "swapRows" that swaps the

// contents of two rows in a given two-dimensional

// array. This method MUST NOT use loops. As a hint,

// since a two-dimensional array is just an array of

// arrays where each inner array is a row, this means

// that you can swap entire rows just by swapping two

// elements of the outer array.

//

public static void swapRows(int[][] arr, int r1, int r2){

int temp[] = arr[r1];

arr[r1] = arr[r2];

arr[r2] = temp;

}

// 2.) A method named "swapCols" that swaps the contents

// of two columns in a given two-dimensional array.

// This method MUST use a loop.

// TODO - write your code below this comment.

public static void swapCols(int[][] arr, int c1, int c2){

int rLen = arr.length;

int temp[] = new int[rLen];

for(int i=0; i<rLen; i++)

temp[i] = arr[i][c1];

for(int i=0; i<rLen; i++)

arr[i][c1] = arr[i][c2];

for(int i=0; i<rLen; i++)

arr[i][c2] = temp[i];

}

// DO NOT MODIFY print2D!

public static void print2D(int[][] array) {

for (int row = 0; row < array.length; row++) {

for (int col = 0; col < array[row].length - 1; col++) {

System.out.print(array[row][col] + " ");

}

System.out.println(array[row][array[row].length - 1]);

}

}

// DO NOT MODIFY main!

public static void main(String[] args) {

int[][] example1 = new int[][]{ new int[]{0, 1, 2},

new int[]{3, 4, 5},

new int[]{6, 7, 8} };

int[][] example2 = new int[][]{ new int[]{9, 8, 7},

new int[]{6, 5, 4},

new int[]{3, 2, 1} };

System.out.println("Before swapping rows 0 and 2:");

print2D(example1);

swapRows(example1, 0, 2);

System.out.println("After swapping rows 0 and 2:");

print2D(example1);

System.out.println("\nBefore swapping columns 1 and 2:");

print2D(example2);

swapCols(example2, 1, 2);

System.out.println("After swapping columns 1 and 2:");

print2D(example2);

}

}


Related Solutions

Download the AddValueNewArray.java file, and open it in jGrasp (or a text editor of your choice)....
Download the AddValueNewArray.java file, and open it in jGrasp (or a text editor of your choice). This program behaves similarly to the AddValueToArray program from before. However, instead of modifying the array in-place, it will return a new array holding the modification. The original array does not change. For simplicity, the array used is “hard-coded” in main, though the method you write should work with any array. Example output with the command-line argument 3 is shown below: Original array: 3...
Download the Take.java file, and open it in jGrasp (or a text editor of your choice)....
Download the Take.java file, and open it in jGrasp (or a text editor of your choice). This program will “take” a given number of elements from a given array, using the command-line arguments as an array, and the hard-coded value of 3 elements to take. Example output of this program with the command-line arguments foo bar baz is shown below: foo bar baz In the above example, three elements were taken. However, it's possible that we may request to take...
Download the AllEqual.java file, and open it in jGrasp (or a text editor of your choice)....
Download the AllEqual.java file, and open it in jGrasp (or a text editor of your choice). This program takes a number of command line arguments, converts them to integers, and then determines if they all have the same value. An example run of the program is below, using the command-line arguments 1 2 3: Are equal: false A further example is shown below, with the command-line arguments 2 2: Are equal: true In the event that the program is given...
Download the Compass.java file, and open it in jGrasp (or a text editor of your choice)....
Download the Compass.java file, and open it in jGrasp (or a text editor of your choice). This program will randomly print out a compass direction, given a seed value for produce a random number with java.util.Random. Compass directions are mapped to integers according to the following table: Compass Direction Integer ID North 0 Northeast 1 East 2 Southeast 3 South 4 Southwest 5 West 6 Northwest 7 Further details are in the comments of Compass.java. import java.util.Random; import java.util.Scanner; public...
Word Frequencies (Concordance)    1. Use a text editor to create a text file (ex: myPaper.txt)...
Word Frequencies (Concordance)    1. Use a text editor to create a text file (ex: myPaper.txt) It should contain at least 2 paragraphs with around 200 or more words. 2. Write a Python program (HW19.py) that asks the user to provide the name of the text file. Be SURE to check that it exists! Do NOT hard-code the name of the file! Use the entry provided by the user! read from the text file NOTE: (write your program so that...
Use Vi text editor or ATOM to create a bash script file. Use the file name...
Use Vi text editor or ATOM to create a bash script file. Use the file name ayaan.sh. The script when ran it will execute the following commands all at once as script. Test your script file make sure it works. Here is the list of actions the script will do: It will create the following directory structure data2/new2/mondaynews off your home directory. inside the mondaynews, create 4 files sports.txt, baseball.txt, file1.txt and file2.txt. Make sure file1.txt and file2.txt are hidden...
Download the file data.csv (comma separated text file) and read the data into R using the...
Download the file data.csv (comma separated text file) and read the data into R using the function read.csv(). Your data set consists of 100 measurements in Celsius of body temperatures from women and men. Use the function t.test() to answer the following questions. Do not assume that the variances are equal. Denote the mean body temperature of females and males by μFμF and μMμMrespectively. (a) Find the p-value for the test H0:μF=μMH0:μF=μM versus HA:μF≠μM.HA:μF≠μM. Answer (b) Are the body temperatures...
Python: Word Frequencies (Concordance) 1. Use a text editor to create a text file (ex: myPaper.txt)...
Python: Word Frequencies (Concordance) 1. Use a text editor to create a text file (ex: myPaper.txt) It should contain at least 2 paragraphs with around 200 or more words. 2. Write a Python program (HW19.py) that asks the user to provide the name of the text file. Be SURE to check that it exists! Do NOT hard-code the name of the file! Use the entry provided by the user! read from the text file NOTE: (write your program so that...
For the text editor application of your choice, give examples of how its features help in creating quality web content
  For the text editor application of your choice, give examples of how its features help in creating quality web content a) State which editor you are using Describe a CSS-related feature of this editor and explain how the feature helps in web development Upload a screenshot that illustrates the feature you have chosen b) State which editor you are using. Describe a JavaScript-related feature of this editor and explain how the feature helps in web development Upload a screenshot...
Python class Select a free ebook on gutenberg . org and download the plain text file...
Python class Select a free ebook on gutenberg . org and download the plain text file (utf8). the ebook is (The Devil's Dictionary by Ambrose Bierce) Write a program that does the following: Read in your ebook (any book, any language) Break the book into words (look at .split() for strings) Make a dictionary of all words and how often they occur: for instance if the word'the'happened 2000 time and the word'a' happened 1940 times word_dict= {'the' : 2000, 'a'...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT