Source: "Agile Software Development: Principles, Patterns, and Practices" by Robert C. Martin. Available at www.objectmentor.com/PPP. Includes intermediate stages of development, as shown in the book.
Code for pp. 46-48

import junit.framework.*;

public class TestGame extends TestCase
{
	public TestGame(String name)
	{
		super(name);
	}

	public void testAddOneThrow() 
	{
		Frame f = new Frame();
		f.add(5);
		assertEquals(5,f.getScore());
	} 

}


public class Frame 
{
	public int getScore() 
	{
		return itsScore;
	}

	public void add(int pins) // was public void add(Throw t)
	{
		itsScore += pins;	
	}

	private int itsScore = 0;
}


Introducing the Game class (pp. 49-50):

public class Game
{
	public int score()
	{
		return itsScore;
	}

	public void add(int pins) 
	{
		itsScore += pins;	
	}

	private int itsScore = 0;	
}
Based on the test case testFourThrowsNoMark, adding a method scoreForFrame(int frame) to the Game class. First version of this:

public class Game
{
	public int score()
	{
		return itsScore;
	}

	public void add(int pins) 
	{
		itsThrow[itsCurrentThrow++] = pins;
		itsScore += pins;	
	}

	public int scoreForFrame(int frame) 
	{
		int score = 0;
		for (int ball = 0; 
		 frame > 0 && (ball < itsCurrentThrow);
		 ball += 2, frame--)
		{
			score += itsThrows[ball] + itsThrows[ball+1];
		}
		return score;
	}

	private int itsScore = 0;
	private int[] itsThrows = new int[21];
	private int itsCurrentThrow = 0;	
}

As the result of refactoring, the loop changes to:

		for (int currentFrame = 0; 
		 currentFrame < theFrame; // theFrame is the parameter
		                          // to the method
		 currentFrame++)
		{
			score += itsThrows[ball++] + itsThrows[ball++];
		}
What's the problem with the loop?
Adding a method setUp() to the testing class. What does it do? API?
The current Game:

public class Game
{
	public int score()
	{
		return itsScore;
	}

	public void add(int pins) 
	{
		itsThrow[itsCurrentThrow++] = pins;
		itsScore += pins;	
	}

	public int scoreForFrame(int theFrame) 
	{
		int score = 0;
		for (int currentFrame = 0; 
		 currentFrame < theFrame; 
		 currentFrame++)
		{
			int firstThrow = itsThrows[ball++];
			int secondThrow = itsThrows[ball++];
			int frameScore = firstThrow + secondThrow;
			if (frameScore == 10) 
				score += frameScore + itsThrows[ball++];
			else
				score += frameScore;
		}
		return score;
	}

	private int itsScore = 0;
	private int[] itsThrows = new int[21];
	private int itsCurrentThrow = 0;	
}

This is an example from CSci 3601 course.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.