BigRational
Work on this exercise by yourself,
but ask questions of your instructor, student assistants and classmates as desired.
Goals
The goals of this exercise are to:
- Learn about some important software engineering principles
- Write to a specification (i.e. implement an interface)
- Implement code by using documented stubs
- Learn some things about the Object class and when its methods should be overridden
The Exercise
Instructions Part 1 - What is an API, Why do we have it, What can we do with it?
- Open the Java API.
- Navigate to the BigInteger class and look at all the information.
- Why is this class useful (i.e. why not just use "int"s?)
- Navigate to Comparable and figure out what it is all about.
What is an Interface?
- Does BigInteger need to have a compareTo() method? (why?....)
- Navigate back to BigInteger and find the compareTo() method. Notice that it implements the Comparable interface.
- Navigate to the ArithmeticObject interface. This interface is not part of the Java library - we created it.
Instructions Part 2 - Create the project
- Open Eclipse, and make sure the Package Explorer is visible in the left window.
- Create a New Project named BigRational.
- Save the ArithmeticObject.java file (in Angel) into your project's src folder.
- Compare ArithmeticObject.java and ArithmeticObject.html. What similarities do you see?
- The html is generated automatically from the javadoc comments above each method. In this class,
we will always write a javadoc comment for each non-private method, field, and class we write.
However, your instructor will teach you some tricks to save you some work.
(You'll have to tweak a setting to make happen.)
- Create a new Class called BigRational.
- Choose "Add..." to add a new interface; type "Comparable", make sure that
"Inherit Abstract Methods" is checked, and choose OK.
- Repeat this process to add in the ArithmeticObject interface also. Choose Finish.
What is different about your project?
- Generate the html documentation for your code by selecting your project in the Package Explorer
and choosing Project → Generate Javadoc..., then select all the defaults.
Note that you may have to browse into your Java installation bin directory to locate the javadoc.exe
file.
- Look at the generated files, which will be in a
doc
folder in your project
(if they aren't there, repeat step 9 with help). Do you understand what happened?
- Make sure all your doc comments are complete.
Instructions Part 3 - Write the code and execute the project
- Note that since we are dealing with fractions, we should give all answers in lowest terms.
(Hint: a careful perusal of the BigInteger class could reveal a method that could save you some time here.)
- You now have a contract to follow. Implement each of the stubs.
(A stub is a method with an empty body, ready to be implemented).
- Note that these stubs each take an argument of type Object (following the general interface).
You can't change the parameter to BigRational, because that will break the contract.
Instead, you should typecast the input argument to BigRational (see the text for simple examples if need be;
1.4.4 gives a simple example with ints and doubles; examples with objects are found in 4.1.9).
- Note also that each math method should return a brand-new object,
leaving the addends untouched (so it's like c=a+b, not like a+=b)
- Interfaces do not specify constructors. Why not?
Consider: what constructor(s) do you think that your BigRational class should have?
- Interfaces do not specify fields. Why not?
Consider: what fields(s) do you think that your BigRational class should have?
- Implement toString() and equals().
- Add three additional methods to BigRational (not the interface) that enhance
the capability of BigRational.
Note that these cannot simply make visible any functions or data that already exist
(for example, accessors or reduce()).
- Add your BigRational project to version control in the same repository where you placed HelloWorld. Don't forget to write useful log comments!
Instructions Part 4 - Add unit tests (Tues class)
- Create unit tests for each arithmetic method. You will be graded on how well you choose your test cases,
as we discussed during the Unit Testing capsule.
- Generate the javadoc again to make sure it is still complete.
- Select your new JUnit files and choose Team → Add, then commit your changes.
Instructions Part 5 - Throw exceptions where necessary (Thurs class)
- Make sure your code throws an
ArithmeticException
whenever you attempt to divide by zero (or to have
zero as a denominator).
- Perform a final check-in of your code to be graded.