CS100J Spring 2003 Assignment 8 Maintaining Bowling Scores II (games)
Submit your solution on the course management system by the deadline: 23:59, Monday, 7 April.
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 7 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 Thursday, 3 April.
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 frame. We suggest that you make frame an array of size 13. frame[0] won't be used, frame[1..10] contain the frames, and frame[11] and frame[12] are null. This will make processing easier; there will be fewer cases to check. We have placed declarations in the class for you.
There is also a field p, which is used for communication between the constructor and another method. Communicating between methods using such fields is not always a good practice. We do it here because the alternatives would mean having you learn about a new class StringBuffer, and you have enough to do at the moment.
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 this way to provide for initial testing is a little trick that makes things easier. 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. Here's its specification. You must follow this specification exactly, because our automatic testing program will be testing it.
/** = String representation of the game. This is toString representation of the
10 frames, each pair being separated by a blank. Here's an example
"(10) (10) (10) (10) (10) (10) (10) (8,1) (9,1) (8,2,10)" */
public String toString()
Step 2. Write function card. Write a function card, corresponding to function card in class Frame. This is similar to function toString, in that it contains a loop that process the ten frames. Here is its specification. Again, you must follow it exactly.
/** = the "card"s of the individual frames, with a blank character separating adjacent pairs.
(The "card" for a frame[i] is found using the call frame[i].card(). Here's an example:
"X X X X X X X 81 9/ 8/X" */
public String card()
Step 3. Write function score. Write a function score, corresponding to function score in class Frame. This is similar to function toString, in that is contains a loop that process the ten frames. Here is its specification. Again, you must follow it exactly.
/** = the scores for the game. This is a list of scores for the individual frames, each exactly 3 characters
(with preceding blanks, if necessary), followed by a blank. Here's an example:
" 30 60 90 120 150 178 197 206 224 244"*/
public String score()
Step 4. Write function total. Write a function total. Here's its specification:
/** = the score for the game */
public int total()
Step 5. Write function getName. Write a getter method for the person's name. Here's its specification:
/** = name of person */
public String getName()
Step 6. 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 10 10 10 10 10 10 10 9 1 10 */
public Game(String n, String s)
Your constructor has to extract the integers from String s and construct the ten frames, filling in array frame (as well as field name).
Step6a. Write the following method:
/** Strip off the next integer (which must exist) in field p and return it. For example, if p is " 4 10 5 ", then the call
stripInt()
changes field p to " 10 5 " and returns the value 4. */
private static int stripInt()
The body of stripInt can best work as follows: (a) remove preceding blanks from p, so that p[0] is a digit, (b) append a blank to p, (c) find the index i of the first blank, (d) store p[0..i-1] in a new String variable res, (e) delete p[0..i-1] from p, and (f) return the value Integer.parseInt(p.substring(0,i)).
Check this method out before proceeding! To check out the method, you might want to make field p and this method public so that you can reference them in the interaction pane of DrJava. When finished, make them both private again.
Step6b. Write the constructor. This requires (among other things) copying parameter s into field p, having a loop call that creates frame[1..9], and then creating frame[10].
Step 7. Submit your file Game.java, by the deadline.