CSE 8B:
Introduction to Computer Science: Java
(Winter, 2006)
Home | Syllabus |
Schedule | Exams |
Programming Assignments
Programming Assignment 2
Due Monday, January 23 at 11:59 PM.
In this assignment, you'll be working with both rational and complex numbers.
The former, you learned about in Algebra, the latter, you likely learned about in Algebra II/Trigonometry.
However, since you may have forgotten, I've provided a brief description, and some links:
- Rational number: a number that can be written as the ratio of two integers
(where the denominator can't equal 0). For example, 1/3,
25/7, -100/13, 52/1. A rational number can be represented by more than one equivalent ratio, for
example, 1/2 and 2/4 are the same rational number.
A google search on "rational number introduction" yields several promising pages, including:
Ask Dr. Math: Integers, Rational & Irrational Numbers.
- Complex number: the sum of a real number and an imaginary number. An imaginary number is a real number
multiplied by i, where i is the square root of -1.
A google search on "complex number introduction" yields several promising pages, including:
Introduction to
Complex Numbers, an introduction that includes exercises along with the introduction.
Test-Driven Development
In this exercise, you are to use test-driven development in the manner described in class.
You'll write a test, verify the test fails, then write enough code to get the test to pass.
At any point where you've got all tests passing, you can refactor to clean up your implementation code
or your test code.
The example from class is available as a QuickTime movie.
The documentation for junit is also available.
Requirements
I've provide a Rational class that implements rational numbers.
The TestRational unit test
will give you a good example on using the class.
Here are the basics:
- Rational numbers can be created with either one integer (assumes 1 as the denominator), or two integers
(first is numerator, second denominator).
- Rational numbers can be added, subtracted, negated, multiplied, and divided. Division can throw
an ArithmeticError (divide-by-zero).
- You can obtain the numerator or denominator of a rational number.
- The rational number is always simplified: for example, "32/64" will return a numerator of 1
and a denominator of "2".
- The toString method returns a textual representation: "2/3" or just "2", if the denominator
is 1.
There is a webpage with documentation for the Rational class.
Requirements
You need to write a Complex class that follows documentation
I've written.
The method signatures should exactly
match those in the documentation. The documentation also includes a full description of what
each method should do.
Provided files
Copy Rational.class and TestRational.java from the P2 directory in the public directory (~/../public/P2/*).
Turnin
Create a directory on ieng6 with the following files:
- Complex.java
- TestComplex.java
To turn in your assignment, cd to that directory and run the command
bundleP2
and carefully follow the dialog.
If it is before the due deadline and all necessary assignment files are present,
this will turn in your solution files.
You can run bundleP2 more than once if you want; only the last one will count.
Subsequent assignments will be turned in with similar commands, bundleP2, bundleP3 and so on.
Grading
The assignment is worth 20 points:
- 8 points: Correct Complex.java code (satisfies not only your unit tests, but ours as well!).
- 8 points: Correct TestComplex.java code (fails if tested with buggy Complex.java implementations).
- 4 points: Style points for your classes. Good naming, not repeating yourself, correct indentation,
not exceeding 80 characters per line, etc.
As always, an additional 5% (1 point) is available if you turn the assignment in at least 24 hours early.