Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Creates a Matrix Class in Java with private Int Column and 2d array as variables. WIth following 5 methods . - public Matrix() The default

Creates a Matrix Class in Java

with private Int Column and 2d array as variables.

WIth following 5 methods .

- public Matrix()

The default constructor for Matrix class which will initialize your instance variables row, column to 0 and the 2D integer array to null.

- public Matrix(int row, int column)

The constructor for Matrix class which takes the integer inputs row and column and will be used for initializing your instance variables row, column and the 2D integer array. For example, creating an object as new Matrix(2,2) should create a matrix with 2 rows and 2 columns. If the parameters passed are invalid (negative numbers) then initialize your instance variables row, column to 0 and the 2D integer array to null

- public Matrix(Matrix mat)

The deep copy constructor for Matrix class which takes the Matrix object mat as an input and creates a deep copy of the matrix. The purpose of a deep copy constructor is to copy the contents of one object to another object and changing the copied object shouldn't change the contents of the original.

- public boolean setElement(int r, int c, int element)

A setter method that will set the value of a particular element in the matrix. It should take the row number, column number (in the zero-based indexing format) and the element to be entered in that particular cell as the inputs. For example, setElement(1,1,20) should set the element at 2nd row and 2nd column to the value 20. If the passed indices are valid, then the element can be set and it should return true after setting the element, otherwise return false. An invalid index would be any negative number or a number that exceeds the number of rows and columns in the matrix (as per zero indexing).

- public Integer getElement(int r, int c)

A getter method that will return the value of the element at the specified index (in the zero-based indexing format). It will take the row number and column number as the inputs and will return the value of the element at that index as an Integer. Integer class is a wrapper class for the primitive type int. However, you need not worry about creating an Integer instance. Just returning a variable of type int should suffice as it will be converted to an Integer object. For example, getElement(1,1) should return the element at 2nd row and 2nd column. If the passed indices are invalid, then return null (since the return type is an Integer class). An invalid index would be any negative number or a number that exceeds the number of rows and columns in the matrix (as per zero indexing).

public Matrix add(Matrix y)

A method called add that will perform matrix addtion. The method will be called as follows: x.add(y) where x and y are Matrix objects. Thus, the method will take a matrix object as the input parameter and will perform addition by considering the object x that is calling the function as the first matrix and the matrix object y passed to the function as the second matrix. So you need to perform x+y and return the sum matrix object. If addition cannot be performed on the two matrices, print the contents of ADD_ERROR string and return null. If a null object is passed to the method, it should just return null.

Remember that the original x and y matrices should not be modified.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Management Systems

Authors: Patricia Ward, George A Dafoulas

1st Edition

ISBN: 1844804526, 978-1844804528

More Books

Students also viewed these Databases questions

Question

6. Name the six virtues, and one related strength for each.

Answered: 1 week ago