CSSE 220 – Object-Oriented Software Development

Homework 13

Objectives

Practice with interfaces and implementing them.

Tasks

  1. Complete the assigned reading for the next session: §11.1-11.7 If there is something you do not understand, make note of it so you can ask about it in class. Chances are good that someone else does not understand either, and will benefit by hearing your question and my answer.
  2. Take the Big Java Reading Quiz over this reading material.  As before, you can take it a second time if you want to improve your score after doing more reading.
  3. Programming:
    1. Your programming work for this part must be done in the OnToInterfaces project inside Eclipse. Use the SVN Repository Exploring perspective to check out this project, then switch back to the Java perspective.
    2. In-class exercises:
      1. Complete the TODO items in polygon.Polygon.
      2. Create a LinearCharge class that implements the Charge interface.
      3. Add code to ChargeMain that adds some LinearCharge instances to the Space.
    3. Board Game: (8 points) 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; we’ll refine the design together in class.

    4. BigRational: (20 points) 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 on the day of Session 15, but you’ll have some additional work fue then from Homework 14, so be sure to make a good start on this before Session 14 class. I suggest writing about 7 of the 10 required methods by then.  This exercise should be straightforward for you, since it will reinforce many ideas that we worked on together for the Fraction class.
      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. 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 5/-6)
          • 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 formally exceptions in Java yet, but all you need to do is
          • detect when the divisor is zero, and
          • in that case, execute the following code:
            throw new ArithmeticException("Divide by zero");
            

Turn-in Instructions

We will grade your ANGEL quiz using ANGEL. Turn-in your programming work by committing it to your SVN repository.