Design and implement a Java class for matrix arithmetic using square matrices (s
ID: 3590083 • Letter: D
Question
Design and implement a Java class for matrix arithmetic using square matrices (same number of rows and columns). Your solution will use a class called Matrix. When designing the class method members, use as a guide the program you wrote for the previous assignment (you should have all the algorithms figured out by now). Provide at least the following methods:
Constructors for default and explicit initialization.
A method to get the size of the matrix
A method to get the element in row r and column c.
A method to set the element in row r and column c to a particular value.
A method to initialize the matrix with random numbers between a lower limit and an upper limit.
Methods for addition, subtraction, and multiplication
A method equals to compare 2 matrices for equality
A method to multiply a matrix by a constant.
A method to transpose a matrix.
A method to find the trace of a matrix.
A method to print a matrix.
A method copy and a method getCopy for a matrix.
To make your job easier, I give you here the layout of the Matrix class (Matrix.java):
//ASSIGNMENT #2: MATRIX ARITHMETIC
//Class Matrix. File: Matrix.java
import java.util.Scanner;
import java.util.Random;
public class Matrix {
public final int MAX = 20;
private int size;
private int[][] table = new int[MAX][MAX];
public Matrix() {... }
public Matrix(int s) {... }
public int getSize() {... }
public int getElement(int r, int c) {... }
public void setElement(int r, int c, int value) {... }
public void init(int low, int up) {... }
public void print() {... }
public Matrix add(Matrix a){... }
public Matrix subtract(Matrix a) {... }
public Matrix multiply(Matrix a) {... }
public Matrix multiplyConst(int whatConst) {... }
public Matrix transpose() {... }
public int trace() {... }
public boolean equals(Matrix a){... }
public void copy(Matrix a) {... }
public Matrix getCopy() {... }
}//close class Matrix
Write the code for class Matrix. Next, write a client that has at least a function menu() with options for the methods implemented and an option to exit. Your program should loop until the user chooses to exit. In this loop you are required to use a switch statement for all possible cases (similar design as the one used in the previous program). Look at the sample output to figure out how the client should work. Again, to make your job easier, I get you started with the client program:
//ASSIGNMENT #2: MATRIX ARITHMETIC
//Client for class Matrix. File: MatrixClient.java
import java.util.Scanner;
import java.util.Random;
public class MatrixClient {
public static final int MAX = 20;
public static final int LOW = 1;
public static final int UP = 10;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int choice; //operation to be executed from menu
int numCommands = 0; //display counter
int size; //for subarray processing
int value; //multiply matrix by this constant
int tr; //return from trace()
//missing code: input size. Valid type? Valid range?
Matrix first = new Matrix(size);
Matrix second = new Matrix(size);
Matrix result = new Matrix(size);
choice = menu(); //priming read;
while (choice != 0) {
---
}
---
}
---
Turn in the programs Matrix.java,and MatrixClient.java. Also, attach a copy of the output screen, including testing for all possible cases.
Explanation / Answer
Note :I will also write the code for client program...thank you.
___________
Matrix.java
import java.util.Random;
public class Matrix {
public final int MAX = 20;
private int size;
private int[][] table = new int[MAX][MAX];
public Matrix()
{
for(int i=0;i<MAX;i++)
{
for(int j=0;j<MAX;j++)
{
table[i][j]=0;
}
}
}
public Matrix(int s)
{
for(int i=0;i<MAX;i++)
{
for(int j=0;j<MAX;j++)
{
table[i][j]=s;
}
}
}
public int getSize()
{
return size;
}
public int getElement(int r, int c)
{
return table[r][c];
}
public void setElement(int r, int c, int value)
{
table[r][c]=value;
}
public void init(int low, int up)
{
Random r = new Random();
for(int i=0;i<MAX;i++)
{
for(int j=0;j<MAX;j++)
{
table[i][j]=r.nextInt((up - low) + 1) + low;
}
}
}
public void print()
{
for(int i=0;i<MAX;i++)
{
for(int j=0;j<MAX;j++)
{
System.out.print(table[i][j]+" ");
}
System.out.println();
}
}
public Matrix add(Matrix a)
{
Matrix m=new Matrix();
m.init(1,100);
for(int i=0;i<MAX;i++)
{
for(int j=0;j<MAX;j++)
{
m.setElement(i, j,m.getElement(i,j)+a.getElement(i,j));
}
}
return m;
}
public Matrix subtract(Matrix a)
{
Matrix m=new Matrix();
m.init(1,100);
for(int i=0;i<MAX;i++)
{
for(int j=0;j<MAX;j++)
{
m.setElement(i, j,m.getElement(i,j)-a.getElement(i,j));
}
}
return m;
}
public Matrix multiply(Matrix a)
{
Matrix m=new Matrix();
m.init(1,100);
for(int i=0;i<MAX;i++)
{
for(int j=0;j<MAX;j++)
{
m.setElement(i, j,m.getElement(i,j)*a.getElement(i,j));
}
}
return m;
}
public Matrix multiplyConst(int whatConst)
{
Matrix m=new Matrix();
m.init(1,100);
for(int i=0;i<MAX;i++)
{
for(int j=0;j<MAX;j++)
{
m.setElement(i, j,m.getElement(i,j)*whatConst);
}
}
return m;
}
public Matrix transpose()
{
Matrix m=new Matrix();
m.init(1,100);
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
m.setElement(j, i, table[i][j]) ;
}
}
return m;
}
public int trace()
{
int sum=0;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if(i==j)
{
sum+=table[i][j];
}
}
}
return sum;
}
public boolean equals(Matrix a)
{
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if(a.getElement(i, j)!=table[i][j])
{
return false;
}
}
}
return true;
}
public void copy(Matrix a)
{
for(int i=0;i<MAX;i++)
{
for(int j=0;j<MAX;j++)
{
table[i][j]=a.getElement(i,j);
}
}
}
public Matrix getCopy()
{
Matrix m=new Matrix();
for(int i=0;i<MAX;i++)
{
for(int j=0;j<MAX;j++)
{
m.setElement(i, j, table[i][j]);
}
}
return m;
}
}
_________________
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.