CSSE 220 – Object-Oriented Software Development

Homework 17

Objectives

Practice with interfaces and implementing them.

Tasks

  1. Complete the assigned reading for today's session (Session 17: Big Java, §9.1–9.6), according to the course schedule.
  2.  
  3. Complete the assessment exercises over this reading on ANGEL (under Lessons → Assignments).
  4. Solo Programming:
    1. Your programming work for this part must be done in the OnToInterfaces project inside Eclipse.
    2. In-class exercises:
      1. Create a LinearCharge class that implements the Charge interface.
      2. Add code to ChargeMain that adds some LinearCharge instances to the Space.
    3. Board Game: Begin the board game exercise described in Project 9.2 in Big Java (p. 416). Decide what methods your Game interface should have. Add these methods signatures to the Game interface provided in the boardGames package—remember, methods in interfaces do not have bodies. 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; we’ll refine the design together in class.

    4. BigRational: This exercise is about writing code to meet a specification. The specification is given by a provided interface. This part of the homework is due with Homework 18, but you’ll have some additional work for that homework assignment, so be sure to make a good start on this before our next class session.

      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 discussed 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. 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
          • 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:

Here is the grading rubric for this assignment.

Turn-in Instructions

Turn in your programming work by committing it to your SVN repository.