CSSE 220 – Object-Oriented Software Development

Homework 15

Objectives

Practice with interfaces and implementing them.

Tasks

  1. Complete the assigned reading for the next session, according to the course schedule.
  2. Complete the assessment exercises over this reading on ANGEL (under Lessons → Assignments).
  3. In Eclipse, checkout the OnToInterfaces project.

  4. Board Game: Begin the board game exercise described in Project 9.2 in Big Java (p. 435). Decide what methods your Game interface should have. Add these methods signatures—remember, methods in interfaces do not have bodies—to the Game interface provided in the boardGames package of today’s Eclipse project. Write javadoc comments for your methods.

    Despite what the last paragraph says in the text, you do not have to provide implementations of your interface yet. However, you should be prepared to describe your interface design to the class in our next meeting. Don’t feel like you have to get this design perfect.

  5. Complete the TODO items in bigRational's BigRational class, in the order that they are numbered.
    1. 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 used before. Do you recall what its use is? Why not just use ints?
      • 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.
    2. 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. Note: until you write equals(), it always returns false, causing your other tests to appear to fail even if your code is correct! Your implementation of toString() should be simple—just format in the form a/b—but be sure to format to 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. 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
        • detect when the denominator is zero, and
        • in that case, execute the following code:
          throw new ArithmeticException("Divide by zero");
          

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!

Here is the grading rubric for this assignment.

Turn-in Instructions

Turn in your programming work by committing it to your SVN repository for this project. Bring your written work to class.