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.
- A set method with three arguments i, j,
x, which assigns floating point value x to the
(i, j) element of the matrix.
- A get method with two arguments i, j,
which returns the value of the (i, j) element of the matrix.
- inquiry methods getNumRows and getNumCols which
respectively return the numbers of rows and columns.
- An add method which adds together two matrices and returns
a new matrix containing the sum of the two matrices, element
by element
- A multiply method which multiplies together the
two matrices according to the proper rules of matrix multiplication,
and returns
a new matrix containing the product of the two matrices.
- A toString method which returns a formatted String
representation of all the elements, laid out in a way suitable for
displaying on a terminal. (This string might include newline characters,
"\n", to separate rows).
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
-
A description of your program, preferably in Microsoft Word,
HTML, or plain text format,
- all Java source code, and
- 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:
-
Functional and clearly written code. Of course the code should meet
the specification given above. It should also be written in a style
that makes it clear to the grader that it is really correct!
In particular, adopt a good indentation policy, avoid long lines and
long method definitions. Break any lines longer than 80 characters.
Consider breaking up any method definition longer than, say, 50 lines into
smaller methods with simpler functionality. For a good set of
stylistic rules for Java programs, see
Sun's Java coding conventions.
-
Clear and well-focussed documentation. Do not write a very long
document (1000 well-chosen words would probably be more than enough), but give
an overall description of the purpose of the software, cover
the principal class interface(s), sketch any relevant implementation details,
and briefly describe how to use the test program.
-
Meaningful comments in the code. No marks for vacuous commentary, but include
short descriptions for each class and method.
Using javadoc comments is not required, but it is encouraged,
and may gain extra credit (javadoc-generated HTML can be
submitted in addition to but not in place of the
conventional documentation described in the previous bullet). Also
give brief implementation comments next to non-obvious pieces of code.
Bear in mind that in general the better written the code, the less the
need should be for implementation comments!
-
Some points will be assigned on the basis of apparent effort and uniqueness.
Useful resources:
Please send questions to Bryan Carpenter at dbc@csit.fsu.edu.