Question

Lab 03 This lab gives you exercises with using two dimensional arrays of Java and practices...

Lab 03

This lab gives you exercises with using two dimensional arrays of Java and practices with using methods that return arrays. What you should do is to download and complete the instructor’s program lab03.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 =

1 2 3 4

5 6 7 8

9 10 11 12

B =

2 4 6 8

10 12 14 16

18 20 22 24

A + B =

3 6 9 12

15 18 21 24

27 30 33 36

5 X A =

5 10 15 20

25 30 35 40

45 50 55 60

Transpose of A =

1 5 9

2 6 10

3 7 11

4 8 12

NOTE THAT YOUR METHODS SHOULD WORK FOR 2D ARRAYS OF ANY SIZES

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

public class lab03 {

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

}

static int[][] add2Ds(int[][]A, int[][]B) {

}

static int[][] multiScalar2D(int scalar, int[][]A) {

}

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

}

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("Instructor's test program\n");

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

print2D(A);

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

print2D(B);

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

print2D(add2Ds(A,B));

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

print2D(multiScalar2D(5,A));

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

print2D(transpose2D(A));

}

}

Homework Answers

Answer #1
public class MatrixOps1 {

    static void print2D(int[][]A) {
        for(int i = 0;i<A.length;i++){
            for(int j = 0;j<A[i].length;j++){
                System.out.print(A[i][j]+" ");
            }
            System.out.println();
        }
    }

    static int[][] add2Ds(int[][]A, int[][]B) {
        int res[][] = new int[A.length][A[0].length];
        for(int i = 0;i<A.length;i++){
            for(int j = 0;j<A[i].length;j++){
                res[i][j] = A[i][j]+B[i][j];
            }
        }
        return res;
    }

    static int[][] multiScalar2D(int scalar, int[][]A) {
        int res[][] = new int[A.length][A[0].length];
        for(int i = 0;i<A.length;i++){
            for(int j = 0;j<A[i].length;j++){
                res[i][j] = A[i][j];
            }
        }
        for(int i = 0;i<A.length;i++){
            for(int j = 0;j<A[i].length;j++){
                res[i][j] = scalar*A[i][j];
            }
        }
        return res;
    }

    static int[][] transpose2D(int[][]A) {
        int result[][] = new int[A[0].length][A.length];
        for(int i = 0;i<A.length;i++){
            for(int j = 0;j<A[0].length;j++){
                result[j][i] = A[i][j];
            }
        }
        return result;
    }

    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("Instructor's test program\n");

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

        print2D(A);

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

        print2D(B);

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

        print2D(add2Ds(A,B));

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

        print2D(multiScalar2D(5,A));

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

        print2D(transpose2D(A));

    }
}

Know the answer?
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
Similar Questions
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
JAVA please Arrays are a very powerful data structure with which you must become very familiar....
JAVA please Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must be of the same type. If the array is an integer array then all the objects in the array must be integers. The object in the array is associated with an integer index which can be used to locate the object. The first object of the array has index 0. There are many problems where...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as possible: What is a checked exception, and what is an unchecked exception? What is NullPointerException? Which of the following statements (if any) will throw an exception? If no exception is thrown, what is the output? 1: System.out.println( 1 / 0 ); 2: System.out.println( 1.0 / 0 ); Point out the problem in the following code. Does the code throw any exceptions? 1: long value...
Java question Consider the following: 1 class SnoopDogg { 2 int count; 3 } 4 class...
Java question Consider the following: 1 class SnoopDogg { 2 int count; 3 } 4 class Test { 5 public static void main ( String [] args ) { 6 SnoopDogg myCount = new SnoopDogg (); 7 myCount.count = 0; 8 int times = 0; 9 increment( myCount, times ); 10 System.out.println( myCount.count ); 11 System.out.println( times ); 12 } 13 public static void increment (SnoopDogg sd, int times ) { 14 sd.count = sd.count + 1; 15 times =...
JAVA question Consider the following: 1 class SnoopDogg { 2 int count; 3 } 4 class...
JAVA question Consider the following: 1 class SnoopDogg { 2 int count; 3 } 4 class Test { 5 public static void main ( String [] args ) { 6 SnoopDogg myCount = new SnoopDogg (); 7 myCount.count = 0; 8 int times = 0; 9 increment( myCount, times ); 10 System.out.println( myCount.count ); 11 System.out.println( times ); 12 } 13 public static void increment (SnoopDogg sd, int times ) { 14 sd.count = sd.count + 1; 15 times =...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
Build two arrays[ ] (Integer and String) and convert them to two ArrayLists and write two...
Build two arrays[ ] (Integer and String) and convert them to two ArrayLists and write two overloaded generic static search method to find the index locations of a specified value. One of the search methods applies to the array type while the other (overloaded) search method applies to the collection type. Implement the following generic linear search method and write a client program to display results: (Here is the header) public static <E extends Comparable<E>> int search(E[] list, E key)...
USING C++ The purpose of this assignment is the use of 2-dimensional arrays, reading and writing...
USING C++ The purpose of this assignment is the use of 2-dimensional arrays, reading and writing text files, writing functions, and program planning and development. You will read a data file and store all of the input data in a two dimensional array. You will perform calculations on the data and store the results in the 2 dimensional array. You will sort the array and print the results in a report. Instructions You will read in the same input file...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT