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:

The Exercise

Instructions Part 1 - What is an API, Why do we have it, What can we do with it?

  1. Open the Java API.
  2. Navigate to the BigInteger class and look at all the information.
  3. Why is this class useful (i.e. why not just use "int"s?)
  4. Navigate to Comparable and figure out what it is all about.

What is an Interface?

  1. Does BigInteger need to have a compareTo() method? (why?....)
  2. Navigate back to BigInteger and find the compareTo() method. Notice that it implements the Comparable interface.
  3. Navigate to the ArithmeticObject interface. This interface is not part of the Java library - we created it.

Instructions Part 2 - Create the project

  1. Open Eclipse, and make sure the Package Explorer is visible in the left window.
  2. Create a New Project named BigRational.
  3. Save the ArithmeticObject.java file (in Angel) into your project's src folder.
  4. Compare ArithmeticObject.java and ArithmeticObject.html. What similarities do you see?
  5. 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.)
  6. Create a new Class called BigRational.
  7. Choose "Add..." to add a new interface; type "Comparable", make sure that "Inherit Abstract Methods" is checked, and choose OK.
  8. Repeat this process to add in the ArithmeticObject interface also. Choose Finish.
  9. 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.
  10. 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?
  11. Make sure all your doc comments are complete.

Instructions Part 3 - Write the code and execute the project

  1. 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.)
  2. 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).
  3. Interfaces do not specify constructors. Why not? Consider: what constructor(s) do you think that your BigRational class should have?
  4. Interfaces do not specify fields. Why not? Consider: what fields(s) do you think that your BigRational class should have?
  5. Implement toString() and equals().
  6. 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()).
  7. 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)

  1. 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.
  2. Generate the javadoc again to make sure it is still complete.
  3. Select your new JUnit files and choose Team → Add, then commit your changes.

Instructions Part 5 - Throw exceptions where necessary (Thurs class)

  1. Make sure your code throws an ArithmeticException whenever you attempt to divide by zero (or to have zero as a denominator).
  2. Perform a final check-in of your code to be graded.