an assignemnt in programing using java class obj

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

  ---   }   --- } ---


Tutor Answer

(Top Tutor) Studypool Tutor
School: Purdue University
Studypool has helped 1,244,100 students
flag Report DMCA
Similar Questions
Hot Questions
Related Tags

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors