Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Lab 1 Implementing an immutable object to represent a matrix of integers is your first project. You can just as well make the class be

Lab 1

Implementing an immutable object to represent a matrix of integers is your first

project. You can just as well make the class be of type double if you so choose

to. For the class, use three private constant fields to represent the matrix. A 2D

array to represent each element of the matrix and two integer values to describe

the dimensions of the matrix (row, column). You are free to use as many fields

as you need however it is advised to try and keep things clean and less cluttered.

3.1 What would your Main method consist of?

Your submission should consist of two classes. A driver class which you will

include the main method and helper refactoring methods. A Matrix class which

you will make instances of and test in the main class. What you call your

driver class is entirely up to you, but will just refer to it as the main or driver

class, you, on the other hand, could name it Lab1 or Test. In your main class,

you need to write a command line interface for the user to select the

size of

his/her desired matrices

(Meaning the size of matrix 1 and the size of matrix

2) and the operation to be executed on the two matrices whose elements will be

randomly generated. Prompt the user to input the size of two of the parameters,

such as the columns, the rows, or both at the same time. Once that is done,

prompt the user to select an operation to use. They can be numbered from 1

to 4 as such:

Select the operation to executed:

1. Add

2. Subtract

3. Multiply

4. Hadamard

>

3.2 Constructor

Write three different constructors for the Matrix class. The first constructor

should initialize the matrix without any input parameter. The second construc-

tor takes in the dimensions of the matrix and creates a matrix object of these

dimensions populated by random numbers. The third constructor should take

in an array as an input parameter and produces an equivalent matrix object

with that particular array.

public Matrix()

{

this(2,2)

}

public Matrix(int rows, int columns)

{

// Initialize the object's fields (class variables)

// given the sizes provided as input parameters

// then populate the matrix with random numbers

// if you have it so that the constructor takes

// a number for the range of random number then

// use a diffrent range

//IntStream.range(0, rows).parallel().mapToObj

// (i -> new Random().ints(min,max).limit(columns).toArray())

// .toArray(int[][]::new);

}

}

public Matrix(int [][] matrixArray)

{

// transform this Array into a new matrix object

// remember Arrays are mutable so you might want to

// make a new copy of the input parameter before using it

// you can use Arrays.copyOf(matrixArray[i], matrixArray[i].length);

}

3.3 Addition and subtraction

You will need to impediment two methods for adding and subtracting matrices

together. The method should take in another matrix and return the result as

a matrix.

public Matrix add(Matrix otherMatrix)

{

// add two matrices of the same size

// Hint: use 2 nested streams

// I would advise that you start the

// stream like you would normally

// start a for loop

// IntStream.range(0, rows).parallel().mapToObj

// (i -> IntStream.range(0, rows).parallel().mapToObj (j -> ...).toArray())

// .toArray(int[][]::new);

}

public Matrix subtract(Matrix otherMatrix)

{

// subtract two matrices of the same size

// Hint: use 2 nested streams like above

// basically what you want is:

// int[][] resultMatrixArray = new int[row][col];

// for (int i = 0; i < row; i += 1) {

// for (int j = 0; j < col; j += 1)

//resultMatrixArray[i][j] = this.elements[i][j] - otherMatrix.elements[i][j];

// }

// return new Matrix(resultMatrixArray);

}

3.4 Multiplication

Like the addition and subtraction methods you will need to also make a mul-

tiplication method. However unlike the addition and subtraction methods you

cant just multiply the equivalent positions in a matrix. The size of the matrix

can vary depending on two input matrices and in some cases it could be illegal

to multiply them both. Check for valid inputs, and be sure to add the additional

inner loop which will be discussed in class.

public Matrix dotProduct (Matrix otherMatrix)

{

// implement a method which takes in another

// matrix as a parameter, and multiplies

// it by the matrix representation stored

// in the object it's self

// hint: It is 3 nested loops which you can do using streams

// the rows of the result matrix

// the columns of the result matrix

// and the calculation of each element

//Basically you want to do this:

// int[][] resultMatrixArray = new int[row][otherMatrix.col];

// for (int i = 0; i < row; i += 1)

// for (int j = 0; j < otherMatrix.col; j += 1)

// for (int k = 0; k < this.col; k += 1) {

// resultMatrixArray[i][j] +=

// this.elements[i][k] * otherMatrix.elements[k][j];

// }

// return new Matrix(resultMatrixArray);

}

3.5 Hadamard product

Hadamard product (also known as the Schur product or the entrywise product)

are like addition and subtraction but multiplies at each step.

public Matrix HadamardProduct (Matrix otherMatrix)

{

// find the Hadamard product of two matrices of the same size

// Hint: use an int or double stream and convert it to a 2D array

// basically it should do this:

// int[][] resultMatrixArray = new int[rows][cols];

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

// for (int j = 0; j < cols; j += 1)

// resultMatrixArray[i][j] = this.elements[i][j] * otherMatrix.elements[i][j];

// }

// return new Matrix(resultMatrixArray);

}

You need to be able to convert your matrix into a string. This method should

return an object of type string and override the inherited toString method form

the Object class.

@Overide

public String toString()

{

// convert the matrix into a single string

// that represents the matrix

// hint: a reduction operation in your stream

// you can use streams to make a string from an array

// like this

//Arrays.stream(linearArray).mapToObj(i-> String.format(" % 6d", i))

// .reduce("", (a,b) -> a + b);

// basically you want to use streams to do this:

// String result = "";

// for (int[] vector : this.elements) {

// result += "[";

//for (int element : vector) {

//result += String.format(" % 6d", element);

//}

//result += "] "; // New line for next row

//}

// return result;

}

You need a method to find if two Matrices are equal

@Overide

public boolean equals()

{

// Look for a method in Arrays that does that.

}

3.6 Clone

Finally you need to be able to clone your matrix into a new instance with the

same values. This method should return an object of type Matrix and override

the inherited Clone method form the Object class.

@Overide

public Matrix Clone()

{

// Take the elements you have and make a new

// Matrix object

//Hint: Use the Arrays class to copy your elements

}

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

(Dont )

The usage of for loops is prohibited for this assignment. The inclusion of

for loops will result in a 23% deduction.

Do not include any print statement in the Matrix class. Inclusion of print

statements will result in a 7.6% deduction.

Do not submit static methods which are mutable in the Matrix class. I

will not even grade your submissions and you will receive a failing grade.

You must submit two classes. Submitting one class with a main method

will result in a 7.6% deduction.

Organize and re-factor your code, Sloppy code will result in a 7.6% deduc-

tion. This is not the first week of COMP 110, you will need to structure

your code so that it is reasonable. Do not include all of the user interface

in one main method, rather call on other methods which perform a specific

task.

Control your user input so that the methods are invoked with valid inputs.

Failure to control for input will result in a 7.6% deduction. Exceptions

are acceptable but they are not the best.

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

Students also viewed these Databases questions

Question

high percentage of women employed outside the home

Answered: 1 week ago