CS100J     Spring 2003     Assignment 7     Maintaining Bowling Scores I (frames)

Submit your solution on the course management system by the deadline: 23:59, Monday, 31 March.

You may work in groups of 2. But PLEASE form your group by 29 March, well before you submit!!!!

Introduction

We will have a series of assignments that end up in a program that maintains statistics on bowling games for any number of people. The group of assignments will exercise your knowledge and skill in a number of areas.

Bowling

A game consists of ten frames. In each of the first 9 frames, you roll one or two balls:

Here is how the information is recorded for a frame (the tenth frame can have more in it):

strike: X
spare with 5 on the first ball: 5/
less than 10 pins on the two balls, say 4 and 5 45

In the tenth frame, if you get a spare or a strike, you throw three balls; otherwise, just two balls. Whenever all 10 pins are knocked down, they are set up again. So, you can throw three strikes in the tenth frame: XXX; or a spare and a strike, e.g. 5/X; or, a spare and less than ten pins, e.g. 5/9; or, less than ten pins, e.g. 81. These examples don't include all possibilities.

The scoring is complicated:

Below, we show a game for Mary and one for Harry. The first line of each has the person's name together with what they rolled in each of the ten frames. The second line contains the running total score after each frame --this is how you keep a score.

Mary got a strike in every frame and three strikes in the last frame, for a perfect score of 300. Since Mary got a strike in her first frame, her score there is 10 + what she rolled on the next two balls, which were also strikes. Harry got just 9 pins in the first frame. In the second frame, he got a spare, so the score is 10 plus the next ball, which was a strike in the next frame, so his score for the second frame is 20. And so it goes.

Mary X X X X X X X X X XXX
  30 60 90 120 150 180 210 240 270 300
Harry 45 4/ X 6/ 63 X X X X 54
  9 29 49 65 74 104 134 159 178 187

Class Frame

We give you a skeleton of class Frame, which is the only class you have to write. It compiles, but it contains only what the user would see --specifications of methods and their headers-- together with method bodies that are either empty or contain a simple return statement. You have to add any fields that you need and fill in the bodies of all the methods. You may add extra private methods if they help --make them private because they should not be seen/usable outside the class. Here is a skeleton of class Frame, or get it from the course management system.

We discuss the various pieces of class Frame. We urge you to write them and test them thoroughly, in the steps provided below. Don't go on to the next step until you are absolutely sure the first is correct. Before you begin, read the specs of all the methods so that you know what you have to do.

Step 0. Fields, constructors, and method toString

First, write declarations of whatever instance variables (fields) that you think are necessary. You need to keep track of up to three balls, and you have to know whether it is the tenth frame or not, because that frame is handled differently. Make sure you write definitions of the variables next to them.

Second, fill in the bodies of the three constructors.

Third, fill in method toString.

Fourth, test the four methods thoroughly, by creating instances of the class (in the Interactions pane) and calling toString.

Step 1. Write function card, which produces the information about balls rolled in the frame. The result MUST be THREE characters long, as specified. Pad with blanks, if necessary, to get it three characters long.

This can be a messy function. We suggest that you write a private function to handle the case of the tenth frame, which is totally different. Then, in function card, test whether it is the tenth frame and return its value immediately if it is (by calling the private function).

Suppose it is not the tenth frame. We suggest that you handle one case at a time, returning its value immediately. For example, start off with the case that it is not a spare: if the sum of the two balls is less than 10, say the balls are 5 and 4, then return "54 ". Next, handle the case of a spare, and finally handle the case of a strike. AFTER EACH CASE, TEST IT. Do incremental programming, compiling, and testing. That way, you remain under control and you have confidence in what your are doing.

Write and test the case of the tenth frame in the same way.

Step 2. Write function score, which returns the score for the frame. This one is weird because the score in the frame may depend on what happens in the next one or two frames! Therefore, the function has two parameters, which are the next one or two frames. You can test for the tenth frame easily enough, if you set your fields correctly. Here is how you test for the ninth frame: if parameter p1 is not null and parameter p2 is null, then it is the ninth frame.

Program, compile, and test incrementally --as done in step 1.

Step 3. Submit your file Frame.java, by the deadline.