CIT 591 Testing Fractions and Complex Numbers
Fall 2004, David Matuszek

Notes on grading:

As I said in the assignment,

We will use our own programs and our own unit tests for grading purposes--therefore, you must have names and parameter types exactly as shown.

I supplied the exact names and exact signatures for all your constructors and methods. You can assume that the Fraction and Complex classes that I write, and test with your JUnit tests, will have exactly those constructors and methods. You can not assume the existence of any other constructors, methods, or variables. If you assume the existence of something that my number class does not have, you will get syntax errors and will be graded accordingly.

I do not consider this to be a new, last-minute requirement, as it was implicit in the assignment.

Legal constructor arguments: Fractions

The following are examples of legal arguments to the Fraction(String) constructor:

"1/2"    "-1/2"    "1/-2"    "-1/-2"    "5"    "-5"    "0/5"    " 17 /  100  "

The following should give an ArithmeticException:  "5/0"    "0/0"

I don't care whether the following are legal or illegal: "7 000 / 25 000"    "- 1/2"

Other forms should give a NumberFormatException. In particular, the constructor is no place to try to do arithmetic, such as "1/2 + 3/4".

Legal constructor arguments: Complex numbers

The following are examples of legal arguments to the Complex(String) constructor:

"1+2i"   "-1+2i"   "1-2i"   "-1.5-2.5i"   "5"   "-5"   "3.0i"   " 12 + 5.3i "

Nothing that I can think of should give an ArithmeticException.

I don't care whether the following are legal or illegal:

"7 000 + 25 000 i "     "1+2 i"     "2i+1"     "1+i2"

Other forms should give a NumberFormatException. In particular, the constructor is no place to try to do arithmetic, such as "1+2+3i".

Testing constructors

The constructors that take one or two numbers, and just store them in private variables, are not easy to test,
but they are also so simple that you hardly need to test them. This is true for the Complex class and is almost true for the Fraction class.

I said in the assignment that

The fraction is always kept in the lowest terms, that is, the Greatest Common Divisor (GCD) of n and d is 1 (use Euclid's algorithm); and
The denominator is never negative

This implies that the two-argument constructor for Fraction should normalize its values. Whether it does so or not is something that should be invisible outside the class, so we won't test for this (but it does simplify some other things. In case you were uncertain, the constructor can call other methods in the class.

Hence, for testing purposes, the obvious thing to do is to call the Fraction constructor with non-normalized values (such as new Fraction(4, 12) and make sure they are equal to the normalized values (such as new Fraction(1, 3)). Whether you do this or not is up to you.

Note, however, that new Fraction(4, 12).toString() should return "1/3", and this is a testable result.

The constructors (for both Complex and Fraction) that take a String as an argument should be tested with a variety of strings, legal and illegal, and in each case the result should be compared to a value created by a simpler constructor.

A note on naming tests: Any method with the signature public void test*() will be executed by JUnit; any method whose signature does not follow this pattern will not be executed by JUnit. There is a convention for naming method tests, so that testing method doThis(int X, int Y) would be public void testDoThis(). and you should follow the convention where appropriate. There is not, so far as I know, a convention for naming constructor tests, so you should do whatever seems reasonable to you, as long as it is recognized by JUnit.

Style

One last time:

Coding style (indentation and spacing) will be considered in grading this project. See the Java™ Style Guide for Sun's official rules, at http://wwws.sun.com/software/sundev/whitepapers/java-style.pdf.