Question

In: Computer Science

Test all of the methods in the code below: Instantiate several Matrix objects of different sizes....

Test all of the methods in the code below:

Instantiate several Matrix objects of different sizes.

Make a Matrix copy from an excisting Matrix

Test the equals method to show one matrix being equal and one matrix not being equal

display 2 matrices using a toString method

display a mtrix object before and after multiplying by a scalar after

display a matrix object before and after multiplying by 0

test the matrix by matrix multiplication with at least 3 different matrix objects that test different outcomes of the method including the exception

Source Code:

public class Matrix {
    private int rows, columns;
    private int[][] data;

    public Matrix(int r, int c) {
        data = new int[r][c];
        rows = r;
        columns = c;
    }

    public Matrix copyMatrix() {
        Matrix m = new Matrix(rows, columns);
        for(int i = 0; i < rows; i++)
            for(int j = 0; j < columns; j++)
                m.data[i][j] = data[i][j];

        return m;
    }

    public boolean equals(Matrix m) {
        if(rows == m.rows && columns == m.columns) {
            for(int i = 0; i < rows; i++) {
                for(int j = 0; j < columns; j++) {
                    if(m.data[i][j] != data[i][j])
                        return false;
                }
            }
            return true;
        }
        else
            return false;
    }

    public String toString() {
        String s = "";
        for(int i = 0; i < rows; i++) {
            for(int j = 0; j < columns; j++) {
                s += String.format("%5d ", data[i][j]);
            }
            s += "\n";
        }

        return s;
    }

    public void scalarMultiply(int k) {
        for(int i = 0; i < rows; i++)
            for(int j = 0; j < columns; j++)
                data[i][j] *= k;
    }


    public Matrix multiply(Matrix m) {
        if(columns != m.rows) //can't perform multiplication
            return null;

        Matrix A = new Matrix(rows, m.columns);
        for(int i = 0; i < rows; i++) {
            for(int j = 0; j < m.columns; j++) {
                for(int k = 0; k < columns; k++)
                    A.data[i][j] += data[i][k] * m.data[k][j];
            }
        }

        return A;
    }

    public void set(int r, int c, int val) {
        if(r >= 0 && r < rows && c >= 0 && c < columns)
            data[r][c] = val;
    }
}

Solutions

Expert Solution

TestMain.java


public class TestMain {
//static method which creates matrix object using two dimensional Array
   public static void createMatrixObject(Matrix matObject,int[][] X,int r,int c)
   {
       for(int i=0;i<r;i++)
           for(int j=0;j<c;j++)
               matObject.set(i, j, X[i][j]);
   }
  
   public static void main(String[] args) {
   //initialize various two dimensional Arrays  
   int[][] A= {{4,3,2},{5,3,1},{3,7,9}};
   int[][] B= { {2,5},{6,8}};
   int[][] C= { {8,9,7},{4,5,6},{1,5,3}};
   int[][] D= { {1,0,0},{0,1,0},{0,0,1}};
   int[][] E= { {5,2},{6,3},{2,7}};
  
//creating Matrix objects
   Matrix matObjA = new Matrix(3,3);
   createMatrixObject(matObjA,A,3,3);
   Matrix matObjB = new Matrix(2,2);
   createMatrixObject(matObjB,B,2,2);
   Matrix matObjC = new Matrix(3,3);
   createMatrixObject(matObjC,C,3,3);
   Matrix matObjD = new Matrix(3,3);
   createMatrixObject(matObjD,D,3,3);
   Matrix matObjE = new Matrix(3,2);
   createMatrixObject(matObjE,E,3,2);
  
   //copying one matrix object into another
   System.out.println("\n Copy Matrix A into another Matrix :");
   Matrix matACopy = matObjA.copyMatrix();
  
   //comapring two matrix objects
   System.out.println("\n Comparing Matrices matObjA and matACopy :"+matObjA.equals(matACopy));
   System.out.println("\n Comapring Matrices matObjA and matObjC :"+matObjA.equals(matObjC));
  
   //displaying two matrix object using toString() method
   System.out.println("\n Displaying Matrix A:\n"+matObjA.toString());
   System.out.println("\n Displaying Matrix E:\n"+matObjE.toString());
  
   //multiplying matrix object with scalar 10
   System.out.println("\n Displaying matObjB before multiplying with a scalar :\n"+matObjB.toString());
   matObjB.scalarMultiply(10);
   System.out.println("\n Displaying matObjB after multiplying with a scalar 10 :\n"+matObjB.toString());
     
   //multiplying matrix object with scalar 0
   System.out.println("\n Displaying matObjE before multiplying with a scalar :\n"+matObjE.toString());
   matObjE.scalarMultiply(0);
   System.out.println("\n Displaying matObjE after multiplying with a scalar 0 :\n"+matObjE.toString());
  
   //multiplying matrix object with another matrix object
   Matrix resMatObj=matObjA.multiply(matObjB);
   System.out.println("\n Multiplying matrix objects A and B :");
   if(resMatObj==null)
       System.out.println("\n Matix multiplication not possible");
   resMatObj=matObjA.multiply(matObjC);
   if(resMatObj!=null)
       System.out.println("\n Multiplying matrix objects A and C :\n"+resMatObj.toString());
   resMatObj=matObjC.multiply(matObjD);
   if(resMatObj!=null)
       System.out.println("\n Multiplying matrix objects C and D :\n"+resMatObj.toString());
   }
}

OUTPUT :


Related Solutions

Using Java Summary Create a Loan class, instantiate and write several Loan objects to a file,...
Using Java Summary Create a Loan class, instantiate and write several Loan objects to a file, read them back in, and format a report. Project Description You’ll read and write files containing objects of the Loan class. Here are the details of that class: Instance Variables: customer name (String) annual interest percentage (double) number of years (int) loan amount (double) loan date (String) monthly payment (double) total payments (double) Methods: getters for all instance variables setters for all instance variables...
Write and submit a test driver program employeeTest.java, the pseudocode is below. start instantiate a Employee...
Write and submit a test driver program employeeTest.java, the pseudocode is below. start instantiate a Employee object get input from user for worker name loop while user does not enter "" Set workers name get input from user for employeeId set workers employeeId get input from user for shift (day/night) set workers shift (input "day" = true , input "night" = false) get input from user workers hourly pay set workers hourly pay get input from user total hours worked...
Guidelines for the program: All methods listed below must be public and static. If your code...
Guidelines for the program: All methods listed below must be public and static. If your code is using a loop to modify or create a string, you need to use the StringBuilder class from the API. Keep the loops simple but also efficient. Remember that you want each loop to do only one "task" while also avoiding unnecessary traversals of the data. No additional methods are needed. However, you may write additional private helper methods, but you still need to...
4. Which methods would be appropriate to treat these objects to remove microorganisms? List all methods...
4. Which methods would be appropriate to treat these objects to remove microorganisms? List all methods that would be applicable. A. Glassware used for culturing bacteria - B. Ground beef being sold in a store– C. Orange juice – D. Disposable syringes: once packaged and prior to use – : once used – F. Endoscope – H. Skin prior to a medical procedure –
The data below are comparing two different methods of measuring blood alcohol concentration. Test the hypothesis...
The data below are comparing two different methods of measuring blood alcohol concentration. Test the hypothesis that these two methods differ, with the assumption that these data are not normally distributed, with an α = 0.01. Which test would be most appropriate? Provide critical value and calculated test statistic, and then state your conclusion. Subject Method 1 Method 2 1 0.55 0.49 2 0.4 0.38 3 2.4 2.48 4 0.87 0.82 5 1.68 1.67 6 1.81 1.81 7 1.44 1.42...
The cost of a leading liquid laundry detergent in different sizes is given below. Size (ounces)...
The cost of a leading liquid laundry detergent in different sizes is given below. Size (ounces) Cost ($) 16 3.49 32 4.39 64 5.19 200 10.09 Calculate the least squares line. Put the equation in the form of: ŷ = a + bx. (Round your answers to three decimal places.) Find the correlation coefficient r. (Round your answer to four decimal places.) r = If the laundry detergent were sold in a 20-ounce size, find the estimated cost. (Use your...
The cost of a leading liquid laundry detergent in different sizes is given below. Size (ounces)...
The cost of a leading liquid laundry detergent in different sizes is given below. Size (ounces) Cost ($) 16 3.89 32 4.79 64 5.69 200 10.59 c) Calculate the least squares line. Put the equation in the form of: ŷ = a + bx. (Round your answers to three decimal places.) ŷ =_____+_____x d) Find the correlation coefficient r. (Round your answer to four decimal places.) r = _____ e) If the laundry detergent were sold in a 50-ounce size,...
The cost of a leading liquid laundry detergent in different sizes is given below. Size (ounces)...
The cost of a leading liquid laundry detergent in different sizes is given below. Size (ounces) Cost ($) Cost per ounce 16 3.79 32 4.89 64 5.49 200 10.99 Calculate the least squares line. Put the equation in the form of: ŷ = a + bx. Find the correlation coefficient r If the laundry detergent were sold in a 50-ounce size, find the estimated cost. If the laundry detergent were sold in an 86-ounce size, find the estimated cost. What...
(Experiment) Measure all the dimension of the four objects and fill out the table below. As...
(Experiment) Measure all the dimension of the four objects and fill out the table below. As the ‘proof’ that you did the experiments, take a screenshot of your PC screen for at least one dimension of each and insert (copy and paste) the screenshots to the end of this data sheet. (For Window’s users, ‘snipping tool’ is convenient to take a screenshot. You can search the tool by searching ‘snipping tool’ on Windows Search. For Mac users, press ‘Command’ +...
java code Add the following methods to the LinkedQueue class, and create a test driver for...
java code Add the following methods to the LinkedQueue class, and create a test driver for each to show that they work correctly. In order to practice your linked list cod- ing skills, code each of these methods by accessing the internal variables of the LinkedQueue, not by calling the previously de?ined public methods of the class. String toString() creates and returns a string that correctly represents the current queue. Such a method could prove useful for testing and debugging...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT