CS 307Assignment, Implementing a Class - Mathematical Matrix

"Linear algebra is a fantastic subject On the one hand it is clean and beautiful. If you have three vectors in 12 dimensional space, you can almost see them."
    - Gilbert Strang, Linear Algebra and its Applications

Programming Assignment 3: This is a pair assignment. You may work with one other person on this assignment using the pair programming technique. Review this paper on pair programming. You are not required to work in a pair on the assignment. If you begin working with one partner and do not wish to finish the assignment with that partner you must complete the assignment individually. If you work with a partner the intent is that work together, at the same computer, on the assignment. One person "drives" (does the typing and explains what they are doing) and the other person "navigates" (watches and asks questions when something is unclear). You should not partition the work, work on your own, and then put things together. You may not acquire, from any source (e.g., another student or student pair or an internet site), a partial or complete solution to a problem or project that has been assigned. You may not show another student or student pair your solution to an assignment. You may not have another person (current student, former student, tutor, friend, anyone) “walk you through” how to solve an assignment. Review the class policy on collaboration from the syllabus.

If you work with a partner you will turn in one version of your code. Pick which account to submit the code to.

If you are working with a partner and want to use slip days you must both have the required number of slip days and both students use slip days. If the assignment is turned in 1 day late each student in a pair must have at least 1 slip day and it costs each student in the pair 1 slip day.

The purposes of this assignment are

  1. To practice implementing a stand alone class.
  2. To work with two dimensional arrays.
  3. To learn to use the jar tool.

    jar is a program included in the standard edition of Java loaded on the computers in the Microlab and that you have on your computer if you downloaded Java. See Sun's page for using jar and my tips for using jar. Some IDEs provide the ability to create jar files. Here is a link to how to create jar files in Eclipse. (If you follow the instructions at that site be sure to select the source code files, .java, not the .class files.)

    On this assignment you will turn in 2 files archived into a single file. The archive tool we use in the class is called jar. (Java ARchive) I strongly recommend learning to use that tool well before the due date. In the past students have had trouble turning in the assignment on time because they did not allow enough time to learn to use jar. jar can be used to create executable Java programs and to archive multiple files into a single file. You will be using jar to create an archive, not to create an executable, so there is no need to include the .class files.

    You must include the .java files in the jar, not just the .class files. If you do not turn in the correct files in your jar file you will either get a 0 on the assignment or use slip days correcting the problem, thus it is important not to wait until the last minute to learn how to create jar files.

    If you need help creating jar see the instructor, a TA, or a proctor during lab hours.

Description: Implement a class that represents a mathematical matrix. You are implementing a stand alone class that is a new data type.

Mathematical matrices are used  to solve systems of linear equations. Matrices are used in applications such as physics, engineering, probability and statistics, economics, biology, and computer science. (especially in the area of computer graphics. For example here is a page on how matrices are used to perform rotations on 3d objects in a 2d graphics system. ) 

Matrices appear in the following form:

    

These matrices represent this system of linear equations:

  x +  5y + 10z + 5w =   4
 6x +  4y + 12z + 4w =   5
10x +  5y + 12z + 11w = 12
 5x + 11y + 23z + 9w  =  7

The above matrix has 4 rows and 4 columns, but the number of rows and columns do not have to be equal. In other words mathematical matrices do not need to be square, but they must be rectangular. Each entry can be an integer or real number. For this assignment you will only deal with matrices of integers. You will implement a class, MathMatrix, that models a mathematical matrix and supports various operations on matrices. See this page for an explanation of the mathematical operations you are implementing. The Wikipedia article may also be useful. After calculus, most students then take a course entitled Matrices and Matrix Calculations.

Provided Files:

  File Responsibility
Implementation MathMatrix.java Provided by me and you. (Okay, mostly you.)
Documentation MathMatrix.html Provided by me.
Implementation Stopwatch.java (For use in experiments) Provided my me
Documentation Stopwatch.html Provided by me
Testing MathMatrixTester.java Provided by me and you
Submission A3.jar. JAR of the program,
which will include MathMatrix.java and MathMatrixTester.java.
Provided by you.

Requirements: The provided source file MathMatrix.java contains a skeleton implementation of a class for modeling mathematical matrices. MathMatrix.java

Implement all of the methods in MathMatrix.java under the constraints of the general requirements.


Experiment: In addition to completing the MathMatrix.java class and adding tests to the MathMatrixTester.java class, perform the following experiments and answer the following questions. Your results and answers will be placed in a comment at the bottom of MathMatrixTester.java.

The code that conducts the experiments is to be included in the MathMatrixTester.java class, but commented out.

Use the Stopwatch class to record the time it takes to perform various operations on MathMatrix objects.

Stopwatch s = new Stopwatch();
s.start();
//code to time
s.stop();

The Stopwatch class can show the elapsed time in seconds or nanoseconds. See the Stopwatch class documentation for more details.

Experiment 1:

Experiment 2:

Questions. Answer the following questions. Place your answers in your comment at the bottom of MathMatrixTester.java.

  1. Based on the results of experiment 1, how long would you expect the add method to take if you doubled the dimension size of the MathMatrix objects again?

  2. Based on the results of experiment 2, how long would you expect the multiply method to take if you doubled the dimension size of the MathMatrix objects again?

  3. How large a matrix can you create before your program runs out of heap memory? Estimate the amount of memory your program is given based on the largest possible matrix object it can create successfully. (Recall, an int in Java takes up 4 bytes.)

  4. The logic of the add and subtract methods of the MathMatrix class are very similar. Code so similar should make you want to generalize it into a method. You are not required to do this on the assignment, but what do you think could be done to generalize the add and subtract methods?


Submission: Fill in the header for MathMatrix.java and MathMatrixTester.java. Replace <NAME> with your name. Note, you are stating, on your honor, that you did the assignment on your own or with your partner, as required.

When finished turn in a jar file named A3.jar that contains the following files: MathMatrix.java and MathMatrixTester.java. Use the turnin program to turn in the jar file named A3.jar.

. Ensure you jar the .java files not the .class files.

Checklist: Did you remember to:


Tips:

  1. Be clear on the difference between MathMatrix objects and the 2d array of ints that serves as the storage container for the ints that make up a MathMatrix object.

    Assume the 2d array of int that is the instance variable for each MathMatrix object is named myCells.

    public MathMatrix add(MathMatrix rightHandSide)
    {   MathMatrix result = new MathMatrix(numRows(), numCols(), 0);
        int valueFromThisMathMatrix = myCells[0][0];
        int valueFromRightHandSide = rightHandSide.myCells[0][0];
        int valueFromResult = result.myCells[0][0];

        // following line results in syntax error
        // valueFromRightHandSide = rightHandSide[0][0];
     
  2. Familiarize yourself with the concept of deep copying. (As opposed to shallow copying.) One of the constructors requires you make a deep copy of a 2d array of ints. Here is the Wikipedia article on object copying.
     
  3. If possible use other methods from the MathMatrix class instead of repeating code.
     
  4. An explanation of the requirements for the toString method.

    In the String that is returned from the toString method the space for each "cell" is equal to the longest value in the matrix plus 1. (Don't forget to consider a minus sign in on of the values.) All cell entries are right justified with newline characters  between rows. The last row does not end in a newline character. For example, given the following MathMatrix.

    10 100 101 -1000
    1000 10 55 4
    1 -1 4 0

    You should return a String that would appear like this. Use newline characters ("\n") to create line breaks.

        10   100   101 -1000
      1000    10    55     4
         1    -1     4     0

    In example above it can be hard to tell how many spaces there are between numbers. In this example the spaces have been replaced by periods to the number of "spaces" is more clearly shown.
     
    ....10...100...101.-1000
    ..1000....10....55.....4
    .....1....-1.....4.....0

    Note, the last line includes a newline character.

    One way of finding the length of an int is to convert it to a String and find the length of the String. Here is an example:

    int x;
    //code to give x a value.
    String s = "" + x;
    int lengthOfInt = s.length();

    //or more simply given an int x
    int lengthOfX = ("" + x).length();

    Doing the toString method using just loops and Strings and if statements is actually a very interesting exercise. Or you can learn how to use the format method from the String class and formatting string syntax. Here is an introduction to formatting String syntax.
     

  5. The isUpperTriangular method determines if the MathMatrix is an upper triangular matrix. A matrix is upper triangular if it is a square matrix and all values below the main diagonal are 0. The main diagonal is all the cells whose row and column are equal. The values of the elements on the main diagonal don't have to be zero, just the ones below it. A 1 by 1 matrix is considered upper triangular.

Back to the CS 307 homepage.