DOC PREVIEW
Berkeley COMPSCI 61B - Matrix

This preview shows page 1 out of 3 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

A matrix is a rectangular table of numbers. For example, a 3x3 matrix might look like1 0 00 1 00 0 1. You canadd two matrices of the same dimension by simply adding together their corresponding elements. For this prob-lem, we will build a simple integer matrix data structure based on lists. You may ONLY use variables of (static)type MyList and int/Integer, eg, MyList<Integer> a = ... would be okay while Integer[] a =... and MyArrayList<Integer> a = ... would not be acceptable. Fill in the following program sothat it conforms to the comments. Also, the interface MyList has been provided for you for ease of reference.// Interface for a Matrix of Integers, DO NOT MODIFYpublic interface IntMatrix {// Sets the specified element of the matrix to new_val// returns false if the modification failedpublic boolean set(int row, int col, Integer new_val);// Returns the specified element of the matrixpublic Integer get(int row, int col);// Returns the number of rows in the matrixpublic int getRowNum();// Return the number of columns in the matrixpublic int getColNum();// Add this matrix to addee and store the result in// THIS. Returns false if the addition failspublic boolean add(IntMatrix addee);// Print entire content of this matrix in any// format so long as user can know when one row// begins and another ends.public void print();}// Our implementation of IntMatrixpublic class ListIntMatrix implements IntMatrix{// Declare your private data members here.private MyList<MyList<Integer>> cols;// Default constructor creates a 1x1 matrix. DO NOT MODIFYpublic ListIntMatrix() {this(1,1);}// Creates a matrix of size row_num x col_num. The matrix is// initially filled completely with 0’s. FILL INpublic ListIntMatrix(int row_num, int col_num) {cols = new MyArrayList<MyList<Integer>>();1for (int i=0; i<row_num; i+=1) {MyList<Integer> temp = new MyArrayList<Integer>();for (int j=0; j<col_num; j+=1)temp.add(0);cols.add(temp);}}// FILL IN any other methods you define here.public boolean set(int row, int col, Integer new_num) {if (row>=0 && col>=0 && row<getRowNum() && col<getColNum())return cols.getEntry(row).replace(col, new_num);elsereturn false;}public Integer get(int row, int col){if (row>=0 && col>=0 && row<getRowNum() && col<getColNum())return cols.getEntry(row).getEntry(col);elsereturn null;}public int getRowNum() {return cols.size();}public int getColNum() {return cols.getEntry(0).size();}public boolean add(IntMatrix addee) {if (addee.getRowNum()==getRowNum() && addee.getColNum()==getColNum()) {for (int i=0; i<getRowNum(); i+=1) {MyList<Integer> temp = cols.getEntry(i);for (int j=0; j<getColNum(); j+=1)temp.replace(j, addee.get(i,j)+temp.getEntry(j));}return true;} elsereturn false;}public void print() {for (int i=0;


View Full Document

Berkeley COMPSCI 61B - Matrix

Documents in this Course
Lab

Lab

4 pages

Numbers

Numbers

14 pages

Lectures

Lectures

12 pages

Project 1

Project 1

24 pages

Exam

Exam

8 pages

Load more
Download Matrix
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Matrix and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Matrix 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?