# an assignemnt in programing using java class obj

*label*Computer Science

*timer*Asked: Mar 9th, 2016

**Question description**

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) {

--- } --- } ---