CSSE 220 – Object-Oriented Software Development
Big Rational
Objectives
Practice with interfaces and implementing them. Practice pair programming.
Tasks
BigRational: This exercise is about writing code to
meet a specification. The specification is given by a provided
interface.
Complete the TODO
items
in bigRational
's BigRational
class, in the
order that they are numbered.
- Background research: (Questions
here are just for you to think about. You
don’t need to provide written answers.)
- Open
the
Java API.
- Review
the
BigInteger
class,
which we’ve discussed before. Do
you recall what its use is? Why not
just use int
s?
- Navigate to
the
Comparable
interface
and figure out what it is all
about. What method(s) must a class
implement if it implements
Comparable
?
-
Review
this documentation for the
ArithmeticObject interface. This
interface is not part of the Java
library—we created it.
- Implementing the interface:
Commit your code to SVN often, say after you
get each method to work. That way you’ll
have a backup of each phase of your work.
- Note that since we are dealing
with fractions, we should
give all answers in lowest terms
(reduced). Hint: A
careful perusal of
the
BigInteger
class
should reveal a method that will save
you some time here.
- Note
that
BigRational
is
an immutable
class. Each math method
should return a brand new
object, leaving the operands
untouched.
- Interfaces do not specify
constructors, but I have stubbed in
two for you. Implement them
first. Interfaces also do not specify
fields, so you’ll have to decide
what field(s) to use.
- Implement the stubbed
in
equals()
and toString()
methods. Your implementation
of toString()
should be
simple—just display in the
form a/b—but
be sure to display the reduced form of
the fraction. Examples:
- 2/3 (rather than
4/6)
- 4 (rather than 4/1)
- -6/5 (negative in
numerator, rather than
6/-5)
- 1/2 (no double negative,
rather than -1/-2)
- 0 (rather than 0/3)
- Implement each of the stubbed-in
methods, running the included JUnit
tests as you do so. The tests are in
two
classes
BasicBigRationalTest
and LargeBigRationalTest
. They
are commented. Be sure to uncomment
them by highlighting the code and
pressing the Ctrl + /
keys together. You can run both by
right-clicking the
bigRational
package and
choosing Run As → JUnit
test.
- The divide method specifies that
it throws an “exception”
on division by zero. We haven’t
covered exceptions in Java yet, but
all you need to do is
Remember, in all your code:
- Write appropriate comments:
- Javadoc comments for public fields and methods.
- Explanations of anything else that is not obvious.
- Give self-documenting variable and method names:
- Use name completion in Eclipse, Ctrl-Space, to keep typing cost low and readability high.
- Use Ctrl-Shift-F in Eclipse to format your code.
- Take care of all auto-generated TODO’s.
- Then delete the TODO comment.
- Correct ALL compiler warnings.
- Quick Fix is your friend!
Grading
Each test case is worth 0.5 points, up to a max of 50 points, minus usual stuff for style.
Turn-in Instructions
Turn in your programming work by committing it to your SVN repository.