Session 8

Using Frames in the BowlingGame

Computer Science II
Object-Oriented Programming

Where Are We?

Last time, we finally implemented strikes in our Frame class. We wrote some code to pass the key tests we designed, but we hadn't yet tested the class thoroughly with cases like a perfect game. Before I posted the code for the session, though, I did, and our class passed the tests perfectly. Hurray!

No we are ready to use Frames in our BowlingGame class.

Remember how we got here...

We're almost back home...

Refactoring BowlingGame to Use Frames

Because we are not yet adding new behavior to the class, we do not have to write new tests. We will use the old tests to ensure that our new implementation still satisfies the program's requirements.

Quick Exercise: What do we need to change in the BowlingGame to make it use Frames?

I would do these in order, one at a time. I might even do them in parallel with the old code until I had made the complete changeover.

... do it together in class ...

Voilá! Our Frame is doing the work for us now. And we know this as well as we can, because our tests -- which we wrote independent of the Frame class! -- tell us so.

Interlude: Compound Tests

... our Frame tests are spread over three different classes. I don't want to have to remember to run all three sets every time I make a change to the class. Even if I remember, I don't want to have to run all three sets. The good sort of laziness in a programmer says, "How can I have mny tools do this work for me?"

JUnit does the job...

Adding the Print Scoresheet Behavior to BowlingGame

Now we are ready for the main event: adding the ability to print a scoresheet to the BowlingGame class.

Quick Exercise: How do we test a printing behavior?

Give the object a method that produces a String that we can verify as correct. Use that method to implement the printing behavior.

Quick Exercise: How do we add this String-producing behavior to the BowlingGame?

I would do these in order, one at a time. Because this is new behavior, we need to write new tests, so I would write a test, write the code that passes the test, and then refactor my class.

... do it together in class ...

Voilá! The BowlingGame satisfies both requirements. And we know this as well as we can, because our tests tell us so.

... wouldn't it have been easier...? ... I don't think so.

Can We Make Our Code Better?

... gameScore() and the printString()s make the same decisions

... the 10th frame behaves differently than the rest

... those conditional tests indicate *kinds of frame*

... we should use different kinds of frame! (continuity)

... how can different kinds play together seamlessly? An interface!

... (Frame (RegularFrame LastFrame))

On Homework 3

New Java examples using BoundedIntegers:

... new approach to requirements ... still, make simplifying assumptions until you have the problem under control....

If the test you just wrote makes you think, "I have no idea how to make this test pass", then you need to write a simpler test!

Wrap Up

Eugene Wallingford ..... ..... February 1, 2005