CS100J     Fall2003     Assignment 6     Maintaining Bowling Scores II (games)

Submit your solution on the course management system by the deadline: 23:59, Friday, 7 November.

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

Introduction

This is the second in a series of three assignments that deal with maintaining bowling scores.

You can see how a program is broken up logically into pieces, each dealing with a different aspect. Classes are used to organize this information in a suitable manner.

Bowling

See the assignment 5 handout for a description of the game.

Class Frame

You wrote your own class Frame for the previous assignment. You may continue to use that class, or you can use our solution, which will be put on the web site on ....

Class Game

Here is a skeleton of class Game that you can use to get started. Here is our class Frame, which we suggest you use because we will use it to check out your Game.

We discuss the various pieces of class Game, which you are supposed to write for this assignment. 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 and a constructor. A game represents a complete game; it maintains the name of the person playing and the scores for the ten frames. Therefore, it makes sense to have a field that contains the person's name and a Frame array, call it frames. We suggest that you make frames an array of size 13. Array element frames[0] won't be used, frames[1..10] contain the frames, and frames[11] and frames[12] are null. This will make processing easier; there will be fewer cases to check. We have placed declarations in the class for you.

We have written a constructor Game(), which initializes the instance to a particular game. This constructor is to be used only for testing. Later, it can be removed. Take a look at it. Setting up a constructor to provide for initial testing is a little trick that makes things easier. Use this trick often. To test a method, say toString, use in DrJava:

Game g= new Game();
g.toString()

Step 1. Write function toString. Write function toString first, so that you can then see easily what the rolls in each frame are. Its specification, which must be followed exactly, is given in the skeleton that we give you.

Step 2. Write function score. Write a function score, which computes the score for a game. Its specification, which must be followed exactly, is given in the skeleton that we give you.

Step 3. Write function card. Write a function card, corresponding to function card in class Frame. Its specification, which must be followed exactly, is given in the skeleton that we give you.

Step 4. Write function getName. Write a getter method for the person's name. Here's its specification:

/** = name of person */
public String getName()

Step 5. Writing a real constructor. This is the most difficult part of the assignment. Below, we give a specification of the constructor. You may write it any way you wish. However, after the spec, we provide some guidelines that will help you develop it.

/** Constructor: a Game for person n with balls rolled given by s. s contains the number of
   balls rolled on each ball, with one or more blanks between them. For example, String s might be
   " 7 3 7 2 0 10 10 10 10 10 10 10 9 1 10 */
public Game(String n, String s)

Your constructor has to extract the integers from s and construct the ten frames, filling in array frame (as well as field name).

Step5a. Write the following method:

/** Strip off the next integer in sb --which must exist-- and return it */
public int stripInt(StringBuffer sb)

For example, if sb contains the string

"####10#9##4####8#3" (with # denoting a blank),

then change sb to

"#9##4####8#3"

and return the value 10. Note that there may or may not be blanks preceding the first integer and following the last integer.

The parameter has class-type StringBuffer, not String. This is because an instance of StringBuffer is MUTABLE --the value in the instance can be changed. The body of stripInt can best work as follows:

Step5b. Write the constructor.

Step 6. Submit your file Game.java, by the deadline.