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 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.
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}}
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.
Java Programm please! Design and implement an algorithm using recursion and backtracking to sort an array...
Java Programm please! Design and implement an algorithm using recursion and backtracking to sort an array of integers into ascending order. Consider the given array as input and produce a sorted array as output. Each time you take an integer from the input array, place it at the end of the output array. If the result is unsorted, backtrack.
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...
java 2D array / recursion explain every method You are requested to write a Java program...
java 2D array / recursion explain every method You are requested to write a Java program of a simple Memory Management Unit. The program should allow the following: 1. The user can create a process asking for memory. The program will return a process ID if the requested memory can be allocated. It will also print the allocated Base and Limit. 2. The user can delete a process by specifying a process ID. The program should do that and free...
In java: 4. Using non-generic techniques, implement a static method getMax() that takes an array of...
In java: 4. Using non-generic techniques, implement a static method getMax() that takes an array of type Comparable and returns the maximum element in the array. (i.e. "public static Comparable getMax(Comparable [] anArray)"). 5. Using the generic techniques to specify super-class relationships, implement a type safe version of the method in 4 named getMaxGen().
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT