CIS 5930-04
Spring 2001

Assignment #1

Writing a Java application with class structure

Your first programming assignment is to write a Java application that uses classes and exceptions. The aim is to give you some practice with Java syntax and make sure that you understand the class concept.

Before starting this assignment, you should ensure you can access your account on the CSIT host computer, sirah. I recommend you also familiarize yourself with the editors and Java development environment on this computer. You may, if you prefer, develop the current homework using Java compilers on other systems, possibly using a programming environment such as J++ or Cafe (if you use any of these programming environments, do not use them to generate Java code for you automatically). But you will need the sirah account to submit your homework. Also note that for later homeworks, some use of the editors and/or the JDK system on sirah and associated hosts will almost certainly be required.

The Assignment

Design and implement a class, Matrix to represent matrices. You will probably need three instance variables in the class:
   int nrows ;    // The number of rows
   int ncols ;    // The number of columns
   float [][];    // Array holding the elements of the matrix
Write a constructor that takes two arguments--the number of rows and columns--and allocates an array to hold the elements.

You should also write (at least) the following public methods.

The add and multiply methods must throw exceptions if the shapes of the matrices do not allow the matrices to be combined according to the rules of matrix arithmetic (see below).

The methods add and multiply may be implemented either as instance methods or as static methods on the Matrix class: you should decide which you think is most suitable and explain your reasons.

You must also write a test program for this class: design an application that prompts the user to type in the shape and element values for two matrices. Create instances of the Matrix class for each one. Combine the two matrices using both add and multiply, and use the toString method to print out a description of both results.

Show a couple of runs of the program (for small matrices, e.g. number of rows and columns may be two or three). Be sure to demonstrate a case of successfully combining matrices, and also one which fails because the add or multiply method gives an exception. Make sure your program works for "non-square" matrices--i.e. number of rows not equal to number of columns.

Definitions for matrix arithmetic:


c = a + b

    c.getNumRows() == a.getNumRows()
    c.getNumCols() == a.getNumCols()

    c.get(i, j) == a.get(i, j) + b.get(i, j)
with constraints:
    a.getNumRows() == b.getNumRows()
    a.getNumCols() == b.getNumCols()

c = a * b

    c.getNumRows() == a.getNumRows()
    c.getNumCols() == b.getNumCols()

    c.get(i, j) == Sum over k of terms: a.get(i, k) * b.get(k, j)
with constraint:
    a.getNumCols() == b.getNumRows()

To simplify things, you may assume that indices i, j, etc, are defined to run from 0 to n - 1, rather than the normal mathematical convention (which is 1 to n). Here n is the number of rows or colums, as appropriate.

Submission of your Homework

Your submission should consist of files including
  1. A description of your program, preferably in Microsoft Word, HTML, or plain text format,
  2. all Java source code, and
  3. file or files showing sample output from one or more runs. This may be captured by cutting and pasting from a command-line window.
On sirah you will find a directory called /home/project/ it1spring01/homework1/uid where uid is your login ID. To submit Assignment I, please copy your files into this directory.

Grading

For this assignment, there will be a total of 20 points. Your submission will be assessed on:


Useful resources:
Please send questions to Bryan Carpenter at dbc@csit.fsu.edu.