Question

In: Computer Science

Using Java: Implement print2D(A) so that it prints out its 2D array argument A in the...

Using Java:

  1. Implement print2D(A) so that it prints out its 2D array argument A in the matrix form.

  2. Implement add2Ds(A,B) so that it creates and returns a new 2D array C such that C=A+B.

  3. Implement multiScalar2D(c,A) so that it creates and returns a new 2D array B such that

    B=c×A.

  4. Implement transpose2D(A), which creates and returns a new 2D array B such that B is

    the transpose of A.

Your output should look like the following:A=

1234 5678 9 10 11 12

B=

2468 10 12 14 16 18 20 22 24

A+B=

3 6 9 12 15 18 21 24 27 30 33 36

5XA=

51015 20 25 30 35 40 45 50 55 60

Transpose of A =

159 2 6 10 3 7 11 4 8 12

Note that your methods should work for 2D arrays of any sizes.

Solutions

Expert Solution

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

public class Matrix{

    public static void main(String[] args) {

        int[][] A = {{1,2,3,4}, {5,6,7,8}, {9, 10, 11, 12}};

        int[][] B = {{2,4,6,8}, {10,12,14,16}, {18,20,22,24}};

        System.out.println("A=");

        print2D(A);

        System.out.println("\nB=");

        print2D(B);

        int[][] C = add2D(A, B);

        System.out.println("\nA+B=");

        print2D(C);

        B = multiScalar(5, A);

        System.out.println("\n5XA=");

        print2D(B);        

        B = transpose2D(A);

        System.out.println("\nTranspose of A=");

        print2D(B);

    }

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

        for(int i=0; i<A.length; i++){

            for(int j=0; j<A[i].length; j++){

                System.out.printf("%3d", A[i][j]);

            }

            System.out.println();

        }

    }

    public static int[][] add2D(int[][] A, int[][] B){

        int rows = A.length;

        int cols = A[0].length;

        int result[][] = new int[rows][cols];

        for(int i=0; i<rows; i++){

            for(int j=0; j<cols; j++){

                result[i][j] = A[i][j] + B[i][j];

            }

        }

        return result;

    }

    public static int[][] multiScalar(int c, int[][] A){

        int rows = A.length;

        int cols = A[0].length;

        int result[][] = new int[rows][cols];

        for(int i=0; i<rows; i++){

            for(int j=0; j<cols; j++){

                result[i][j] = c*A[i][j];

            }

        }

        return result;

    }

    public static int[][] transpose2D(int[][] A){

        int rows = A.length;

        int cols = A[0].length;

        int result[][] = new int[cols][rows];

        for(int i=0; i<rows; i++){

            for(int j=0; j<cols; j++){

                result[j][i] = A[i][j];

            }

        }

        return result;

    }

}


Related Solutions

In Java, write a recursive function that accepts a string as its argument and prints the...
In Java, write a recursive function that accepts a string as its argument and prints the string in reverse order. Demonstrate the function in a driver program.
. Write a program to print * in the following order using 2d array in java...
. Write a program to print * in the following order using 2d array in java                                              *             *             *             *             *                                              *             *             *             *                                              *             *             *                                              *             *                                                          *
Write a java program of a multiplication table of binary numbers using a 2D array of...
Write a java program of a multiplication table of binary numbers using a 2D array of integers.
Program to implement in c++ dont forget to implement the 2D array and print whether the...
Program to implement in c++ dont forget to implement the 2D array and print whether the vehicle is available or not! This is the main part that I dont know how to implement! no good rating if this isnt done. Define a class Vehicle that has the following data members:  Model of the vehicle (e.g., Ford, Toyota) as a standard library string.  The date that vehicle has joined to the fleet use the class Date that has the...
java 1.) a method to append a 2d double array at the right of another 2d...
java 1.) a method to append a 2d double array at the right of another 2d double array, return a new array. E.g. numss1: {{1, 2}, {3, 4, 5}, {6}}, numss2: {{7}, {8, 9}}, return {{1, 2, 7}, {3, 4, 5, 8, 9}, {6}}
Using any existing 2D or 3D graphics library ( such as Java 2D, Java 3D or...
Using any existing 2D or 3D graphics library ( such as Java 2D, Java 3D or OpenGL ) draw a scene featuring various elements. You are to choose from one of the following categories: Any man-made landscape ( using Java 2D, or Java 3D, or OpenGL) Promoting a cause (Using Java 3D or OpenGL ) 3. Any visual landscape element of your choice (Using OpenGL with Java or Python ) You are free to create whatever you choose but it...
few problems example of array and 2d array and the solution code in java language. I...
few problems example of array and 2d array and the solution code in java language. I am new to java and trying to learn this chapter and it is kinda hard for me to understand.
Using Matlab, write a function that takes numeric data as its input argument and prints a...
Using Matlab, write a function that takes numeric data as its input argument and prints a message to the Command Window stating if the number is positive, or negative, or 0. This function should transfer an output value to the Workspace ONLY when the input value is negative. Please include a copiable code and the associated screenshots.
i want a program in java that finds the shortest path in a 2D array with...
i want a program in java that finds the shortest path in a 2D array with obstacles from source to destination using BFS and recursion. The path must be stored in a queue. The possible moves are left,right,up and down.
Using any existing 2D or 3D graphics library ( Java 2D, Java 3D, draw a scene...
Using any existing 2D or 3D graphics library ( Java 2D, Java 3D, draw a scene within one of the following categories: Satire or humor Promote a cause You are free to create whatever you choose but it must conform to the following guidelines.   Show evidence of at least four colors. Have a textual composition on the finished product. Imagery or images Scene composition of at least six (6) elements One of the following 1) Shadows or Glows. May be...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT