Share
Embed
Imperative Bowling Kata - 20 Years On - Delegating Menial Tasks to AI Coding Tool Claudeย Code
Imperative Bowling Code Kata - 20 Years On(2005 - 2025) - Delegating Menial Tasks to AI Coding Tool 'Claude Code'.
Keywords: bowling game code kata, code kata, TDD, Refactoring, AI code tool, Claude Code, Imperative programming, Object Mentor, Robert Martin, Uncle Bob, Java
Philip Schwarz PRO
February 15, 2026
Transcript
170 slides
-
Imperative Bowling Kata 20 Years On Delegating Menial Tasks to
AI Coding Tool Claude Code A Very First Attempt Bowling Game Code Kata Claude Code Agentic code tool ๐ง๐ป @philip_schwarz slides by https://fpilluminated.org/ -
As can be seen on the next slide, in late
2000 Uncle Bob (Robert Martin) and Bob Koss pair programmed (in a hotel room) the Bowling Game, and (two years later) the ensuing programming episode was captured in glorious detail in the fourty or so pages constituting chapter six of the book Agile Software Development, Principles, Patterns, and Practices. The two Bobs were both associated members of Object Mentor, a now defunct company which was founded by Uncle Bob in 1991, and which advocated object-oriented software technologies and cutting-edge Agile development methodologies. In 1998, Object Mentor helped to define the Agile development method known as Extreme Programming. In 2000, Object Mentor helped form the Agile Alliance โ a group of professionals dedicated to the advocacy of lean and emergent software methods. By 2005 Uncle Bob had practised the Bowling Game code kata hundreds of times (see slide after next for an overview of the term code kata), and although one of the key aspects of a code kata is that most times (if not each time) we โdoโ a particular kata, we go through a unique learning experience, he decided to capture, in a deck called Bowling Game Kata, an example of the sequence of TDD steps that one may go through when performing the kata (see subsequent slides for where to find the original deck). The sequence illustrated in the deck is not meant to be โtheโ ideal sequence, or a typical sequence. It is just a sequence whose length and particulars make it both amenable to capturing in a deck, and didactically suitable as a demonstration of โdoingโ that kata. Here is what we are going to do in this deck (it will become perfectly obvious once we actually do it, so donโt worry if you find some or all of the following unclear): โข Fire up coding tool Claude Code โข Go through the original Bowling Game Kata deck, one slide at a time, and do the following for each original slide: โข Show a copy of the original slide that has been modified as described in the next two bullet points โข If relevant, highlight the code that is being added and/or modified in the TDD step illustrated by the original slide โข Add a slide number โข Add a slide showing: โข how the code in the current original slide differs from the code in the previous original slide โข a command that causes Claude to make code changes that result in the above differences โข Add one or more slides showing an interaction in which we issue Claude with the command, and Claude obeys it, keeping us informed of actions taken, and their outcome. @philip_schwarz โข Original slides are going to be marked with these two icons and a with page number โข Highlighted code in original slides will look like this โข Modified code in original slides will look like this โข The Claude commands that weโll be issuing will look like this run the test suite green 11 orange -
A Programming Episode Design and programming are human activities; forget
that and all is lost. โBjarne Stroustrup, 1991 In order to demonstrate the XP programming practices, Bob Koss (RSK) and Bob Martin (RCM) will pair program a simple application while you watch like a fly on the wall. We will use test-driven development and a lot of refactoring to create our application. What follows is a pretty faithful reenactment of a programming episode that the two Bobs did in a hotel room in late 2000. We made lots of mistakes while doing this. Some of the mistakes are in code, some are in logic, some are in design, and some are in requirements. As you read, you will see us flail around in all these areas, identifying and then dealing with our errors and misconceptions. The process is messy - as all human processes are. The resultโฆ well, itโs amazing the order that can arise out of such a messy process. The program calculates the score of a game of bowling, so it helps if you know the rules. If you donโt know the rules of bowling, check out the box on page 99 Image ยฉ Jennifer M. Kohnke -
The next four slides provide a bit of context for
the Bowling Game Kata deck, and show where the deck can be found. If you are already familiar with the deck, and already know where to find it, you might want to skip the slides, at least on a first reading. -
With that preamble out of the way, letโs start going
through our lightly annotated copies of the slides of the Bowling Game Kata deck, and issuing relevant commands for Claude to obey. Be warned that there is nothing prescriptive or exemplary about the chosen commands. Just because the commands had the intended consequences, it doesnโt mean that they are of any particular merit. The commands are not the embodiment of any planned strategy and/or tactics aimed at securing a desired outcome in terms of e.g. optimality, effectiveness, efficiency, productivity. On the contrary, the commands are just whatever came to mind in a first and only session with Claude, the sessionโs proceedings being shown โwarts and allโ. As a first example of this, while we begin by issuing commands purely consisting of prose, we later find it expedient to include in the commands some code fragments, small ones at first, and larger ones later on. As an other example, our prose commands are almost certainly more verbose than necessary given Claudeโs ability to figure out the intent of commands. -
Bowling Game Kata Object Mentor, Inc. fitnesse.org Copyright ยฉ 2005
by Object Mentor, Inc All copies must retain this page unchanged. www.junit.org www.objectmentor.com blog.objectmentor.com 1 -
Scoring Bowling. The game consists of 10 frames as shown
above. In each frame the player has two opportunities to knock down 10 pins. The score for the frame is the total number of pins knocked down, plus bonuses for strikes and spares. A spare is when the player knocks down all 10 pins in two tries. The bonus for that frame is the number of pins knocked down by the next roll. So in frame 3 above, the score is 10 (the total number knocked down) plus a bonus of 5 (the number of pins knocked down on the next roll.) A strike is when the player knocks down all 10 pins on his first try. The bonus for that frame is the value of the next two balls rolled. In the tenth frame a player who rolls a spare or strike is allowed to roll the extra balls to complete the frame. However no more than three balls can be rolled in tenth frame. 2 -
The Requirements. โข Write a class named โGameโ that has
two methods โ roll(pins : int) is called each time the player rolls a ball. The argument is the number of pins knocked down. โ score() : int is called only at the very end of the game. It returns the total score for that game. 3 -
A quick design session The tenth frame has two or
three rolls. It is different from all the other frames. 7 -
A quick design session The score function must iterate through
all the frames, and calculate all their scores. 8 -
A quick design session The score for a spare or
a strike depends on the frameโs successor 9 -
create an empty java project called claude-practice using the latest
versions of java and junit as of 2005 9 10 -
By the way: I have chosen project name claude-practice, rather
than bowling-kata, just in case the latter would prompt Claude to search my laptop for existing related code and/or documents which might skew the results of this experiment. Similarly, in a later slide I choose class name Test, ather than BowlingGameTest. -
$ claude โญโโโ Claude Code v2.0.75 โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ โ โ Tips
for getting started โ โ Welcome back Philip! โ Run /init to create a CLAUDE.md file with instructions for Claude โ โ โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ โ * โ โ โ โ * โ Recent activity โ โ * * โ No recent activity โ โ * โโ โโ * โ โ โ โ โ โ Opus 4.5 ยท Claude Pro ยท โ โ โ [email protected]'s Organization โ โ โ ~/dev/claude-code-experiment โ โ โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ > create an empty java project called claude-practice using the latest versions of java and junit as of 2005 โบ Bash(mkdir -p claude-practice/src/main/java claude-practice/src/test/java claude-practice/lib) โฟ Runningโฆ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Bash command mkdir -p claude-practice/src/main/java claude-practice/src/test/java claude-practice/lib Create project directory structure Do you want to proceed? (Tab to add further instructions) โฏ 1. Yes 2. Yes, and don't ask again for mkdir commands in /Users/philipschwarz/dev/claude-code-experiment 3. No Esc to cancel -
โบ As of 2005, the latest versions were Java 5
(JDK 1.5) and JUnit 3.8.1 (JUnit 4 didn't arrive until early 2006). Ant was the standard build tool at the time. โบ Write(claude-practice/build.xml) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Create file claude-practice/build.xml โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 1 <?xml version="1.0" encoding="UTF-8"?> 2 <project name="claude-practice" default="test" basedir="."> 3 4 <property name="src.dir" value="src/main/java"/> 5 <property name="test.dir" value="src/test/java"/> 6 <property name="build.dir" value="build"/> 7 <property name="build.classes" value="${build.dir}/classes"/> 8 <property name="build.test-classes" value="${build.dir}/test-classes"/> 9 <property name="lib.dir" value="lib"/> 10 11 <path id="classpath"> 12 <fileset dir="${lib.dir}" includes="**/*.jar"/> 13 </path> 14 15 <path id="test.classpath"> 16 <path refid="classpath"/> 17 <pathelement location="${build.classes}"/> 18 <pathelement location="${build.test-classes}"/> 19 </path> 20 21 <target name="clean"> 22 <delete dir="${build.dir}"/> 23 </target> 24 25 <target name="compile"> 26 <mkdir dir="${build.classes}"/> 27 <javac srcdir="${src.dir}" destdir="${build.classes}" 28 source="1.5" target="1.5" 29 classpathref="classpath" 30 includeantruntime="false"/> 31 </target> 32 33 <target name="compile-tests" depends="compile"> 34 <mkdir dir="${build.test-classes}"/> 35 <javac srcdir="${test.dir}" destdir="${build.test-classes}" 36 source="1.5" target="1.5" 37 classpathref="test.classpath" 38 includeantruntime="false"/> 39 </target> 40 41 <target name="test" depends="compile-tests"> 42 <junit printsummary="yes" haltonfailure="yes"> 43 <classpath refid="test.classpath"/> 44 <batchtest> 45 <fileset dir="${test.dir}" includes="**/*Test.java"/> 46 </batchtest> 47 </junit> 48 </target> 49 50 </project> โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to create build.xml? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel -
โบ Now let me download JUnit 3.8.1: โบ Bash(curl -sL
-o /Users/philipschwarz/dev/claude-code-experiment/claude-practice/lib/junit-3.8.1.jar https://repo1.maven.org/maven2/junit/junit/3.8.1/junit-3.8.1.jarโฆ) โฟ Runningโฆ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Bash command curl -sL -o /Users/philipschwarz/dev/claude-code-experiment/claude-practice/lib/junit-3.8.1.jar https://repo1.maven.org/maven2/junit/junit/3.8.1/junit-3.8.1.jar && ls -la /Users/philipschwarz/dev/claude-code-experiment/claude-practice/lib/Download JUnit 3.8.1 jar Do you want to proceed? (Tab to add further instructions) โฏ 1. Yes 2. Yes, and don't ask again for curl commands in /Users/philipschwarz/dev/claude-code-experiment 3. No Esc to cancel โบ The claude-practice project is ready. Here's what was created: claude-practice/ โโโ build.xml # Ant build file (Java 1.5 source/target) โโโ lib/ โ โโโ junit-3.8.1.jar # JUnit 3.8.1 โโโ src/ โ โโโ main/java/ # Production source โ โโโ test/java/ # Test source Versions used (latest as of 2005): - Java 5 (JDK 1.5) โ released Sept 2004 - JUnit 3.8.1 โ released Nov 2003 (JUnit 4 wasn't released until Feb 2006) - Ant build file โ the standard build tool of the era Build commands: ant compile, ant test, ant clean. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > now add a failing test -
Begin. โข Create a project named BowlingGame โข Create a
unit test named BowlingGameTest import junit.framework.TestCase; public class BowlingGameTest extends TestCase { } 11 -
> create a unit test called Test โบ Write(claude-practice/src/test/java/Test.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Create file claude-practice/src/test/java/Test.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 1 import junit.framework.TestCase; 2 3 public class Test extends TestCase { 4 5 } โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to create Test.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel > create a unit test called Test โบ Write(claude-practice/src/test/java/Test.java) โฟ Wrote 6 lines to claude-practice/src/test/java/Test.java 1 import junit.framework.TestCase; 2 public class Test extends TestCase { 3 } โบ Created src/test/java/Test.java โ an empty JUnit 3 test case extending TestCase. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > add a test method -
Begin. โข Create a project named BowlingGame โข Create a
unit test named BowlingGameTest import junit.framework.TestCase; public class BowlingGameTest extends TestCase { } Excute this program and verify that you get the following error: No tests found in BowlingGameTest 12 -
> run the test suite โบ Bash(cd /Users/philipschwarz/dev/claude-code-experiment/claude-practice && ant
test) โฟ Error: Exit code 127 (eval):1: command not found: ant โบ Ant isn't installed. Let me check what's available: โบ Bash(which javac java 2>&1; java -version 2>&1) โฟ Runningโฆ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Bash command which javac java 2>&1; java -version 2>&1 Check Java availability Do you want to proceed? (Tab to add further instructions) โฏ 1. Yes 2. Yes, and don't ask again for java commands in /Users/philipschwarz/dev/claude-code-experiment 3. No Esc to cancel -
โบ No Ant, but Java is available. I'll compile and
run the tests directly: โบ Bash(cd /Users/philipschwarz/dev/claude-code-experiment/claude-practice && mkdir -p build/test-classes && javac -cp lib/junit-3.8.1.jar -d build/test-classes src/tesโฆ) โฟ Runningโฆ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Bash command cd /Users/philipschwarz/dev/claude-code-experiment/claude-practice && mkdir -p build/test-classes && javac -cp lib/junit-3.8.1.jar -d build/test-classes src/test/java/Test.java && java -cp lib/junit-3.8.1.jar:build/test-classes junit.textui.TestRunner Test Compile and run JUnit tests Do you want to proceed? (Tab to add further instructions) โฏ 1. Yes 2. No Esc to cancel โบ Bash(cd /Users/philipschwarz/dev/claude-code-experiment/claude-practice && mkdir -p build/test-classes && javac -cp lib/junit-3.8.1.jar -d build/test-classes src/tesโฆ) โฟ Error: Exit code 1 .F Time: 0.002 There was 1 failure: 1) warning(junit.framework.TestSuite$1)junit.framework.AssertionFailedError: No tests found in Test FAILURES!!! Tests run: 1, Failures: 1, Errors: 0 โบ The test suite compiled and ran successfully. JUnit 3 reports a warning because the Test class has no test methods yet โ that's expected for an empty test case. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > add a test method -
The first test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ public void testGutterGame() throws Exception { Game g = new Game(); } } 13 -
Add to the test suite a test called testGutterGame which
simply defines variable g and sets it to an instance of a non-existent class called Game 12 13 -
> Add to the test suite a test called testGutterGame
which simply defines variable g and sets it to an instance of a non-existent class called Game โบ Read(claude-practice/src/test/java/Test.java) โฟ Read 6 lines โบ Update(claude-practice/src/test/java/Test.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file claude-practice/src/test/java/Test.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 2 3 public class Test extends TestCase { 4 5 + public void testGutterGame() { 6 + Game g = new Game(); 7 + } 8 } โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Test.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Added testGutterGame. It references the non-existent Game class, so it won't compile until that class is created. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > create the Game class so the test compiles โต send โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ -
The first test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ public void testGutterGame() throws Exception { Game g = new Game(); } } public class Game { } 14 -
> create the Game class so the test compiles โบ
Write(claude-practice/src/main/java/Game.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Create file claude-practice/src/main/java/Game.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 1 public class Game { 2 3 } โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to create Game.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Write(claude-practice/src/main/java/Game.java) โฟ Wrote 4 lines to claude-practice/src/main/java/Game.java 1 public class Game { 2 } โบ Created src/main/java/Game.java. The test should now compile. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > run the tests -
The first test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ public void testGutterGame() throws Exception { Game g = new Game(); } } public class Game { } 15 -
> run the tests โบ Bash(cd /Users/philipschwarz/dev/claude-code-experiment/claude-practice && mkdir -p
build/classes build/test-classes && javac -d build/classes src/main/java/Game.javโฆ) โฟ Runningโฆ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Bash command cd /Users/philipschwarz/dev/claude-code-experiment/claude-practice && mkdir -p build/classes build/test-classes && javac -d build/classes src/main/java/Game.java && javac -cp lib/junit-3.8.1.jar:build/classes -d build/test-classes src/test/java/Test.java && java -cp lib/junit-3.8.1.jar:build/classes:build/test-classes junit.textui.TestRunner Test Compile and run JUnit tests Do you want to proceed? (Tab to add further instructions) โฏ 1. Yes 2. No Esc to cancel > run the tests โบ Bash(cd /Users/philipschwarz/dev/claude-code-experiment/claude-practice && mkdir -p build/classes build/test-classes && javac -d build/classes src/main/java/Game.javโฆ) โฟ . Time: 0.001 OK (1 test) โบ 1 test ran, all passing. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > -
The first test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ public void testGutterGame() throws Exception { Game g = new Game(); for (int i=0; i<20; i++) g.roll(0); } } public class Game { } 16 -
Modify testGutterGame so that, after defining g, it invokes twenty
times, with an argument of zero, its non-existent method whose name is roll and whose single parameter is an integer number 15 16 -
> Modify testGutterGame so that, after defining g, it invokes
twenty times, with an argument of zero, its non-existent method whose name is roll and whose single parameter is an integer number โบ Update(src/test/java/Test.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/test/java/Test.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 4 5 public void testGutterGame() { 6 Game g = new Game(); 7 + for (int i = 0; i < 20; i++) { 8 + g.roll(0); 9 + } 10 } 11 } โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Test.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Added the loop calling g.roll(0) twenty times. This won't compile until Game has a roll(int) method. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > create the roll method so the test compiles -
The first test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ public void testGutterGame() throws Exception { Game g = new Game(); for (int i=0; i<20; i++) g.roll(0); } } public class Game { public void roll(int pins) { } } 17 -
Add to Game a method whose name is roll, and
whose parameter is an integer called pins, and whose return type is void, and whose body is empty 15 16 -
> Add to Game a method whose name is roll,
and whose parameter is an integer called pins, and whose return type is void, and whose body is empty โบ Update(src/main/java/Game.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/main/java/Game.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 1 public class Game { 2 3 + public void roll(int pins) { 4 + } 5 } โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Game.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Added the roll(int pins) method to Game. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > run the tests -
The first test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ public void testGutterGame() throws Exception { Game g = new Game(); for (int i=0; i<20; i++) g.roll(0); assertEquals(0, g.score()); } } public class Game { public void roll(int pins) { } } 18 -
Modify testGutterGame so that after calling roll twenty times, it
asserts that the result of invoking g's non-existent method called score is zero 17 18 -
> Modify testGutterGame so that after calling roll twenty times,
it asserts that the result of invoking g's non-existent method called score is zero โบ Update(src/test/java/Test.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/test/java/Test.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 7 for (int i = 0; i < 20; i++) { 8 g.roll(0); 9 } 10 + assertEquals(0, g.score()); 11 } 12 } โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Test.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No โบ Added the assertion. This won't compile until Game has a score() method. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > -
The first test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ public void testGutterGame() throws Exception { Game g = new Game(); for (int i=0; i<20; i++) g.roll(0); assertEquals(0, g.score()); } } public class Game { public void roll(int pins) { } public int score() { return -1; } } expected:<0> but was:<-1> 19 -
Add to Game a zero-argument method whose name is score,
and whose return type is integer, and whose body simply returns -1 run the tests 18 19 -
> Add to Game a zero-argument method whose name is
score, and whose return type is integer, and whose body simply returns -1 โบ Update(src/main/java/Game.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/main/java/Game.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 2 3 public void roll(int pins) { 4 } 5 + 6 + public int score() { 7 + return -1; 8 + } 9 } โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Game.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No โบ Added the score() method returning -1. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > run the tests -
> run the tests โบ Bash(mkdir -p build/classes build/test-classes &&
javac -d build/classes src/main/java/Game.java && javac -cp lib/junit-3.8.1.jar:build/classes -d build/test-classesโฆ) โฟ Runningโฆ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Bash command mkdir -p build/classes build/test-classes && javac -d build/classes src/main/java/Game.java && javac -cp lib/junit-3.8.1.jar:build/classes -d build/test-classes src/test/java/Test.java && java -cp lib/junit-3.8.1.jar:build/classes:build/test-classes junit.textui.TestRunner Test Compile and run JUnit tests Do you want to proceed? (Tab to add further instructions) โฏ 1. Yes 2. Yes, and don't ask again for mkdir, javac, and java commands in /Users/philipschwarz/dev/claude-code-experiment 3. No Esc to cancel > run the tests โบ Bash(mkdir -p build/classes build/test-classes && javac -d build/classes src/main/java/Game.java && javac -cp lib/junit-3.8.1.jar:build/classes -d build/test-classesโฆ) โฟ Error: Exit code 1 .F Time: 0.002 There was 1 failure: 1) testGutterGame(Test)junit.framework.AssertionFailedError: expected:<0> but was:<-1> at Test.testGutterGame(Test.java:10) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) โฆ +2 lines (ctrl+o to see all) โบ The test fails as expected โ score() returns -1 but the test expects 0. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > Make score return 0 -
The first test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ public void testGutterGame() throws Exception { Game g = new Game(); for (int i=0; i<20; i++) g.roll(0); assertEquals(0, g.score()); } } public class Game { public void roll(int pins) { } public int score() { return 0; } } 20 -
19 20 Modify the body of the score method so
that it returns zero rather than -1 run the tests -
> Modify the body of the score method so that
it returns zero rather than -1 โบ Update(src/main/java/Game.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/main/java/Game.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 4 } 5 6 public int score() { 7 - return -1; 7 + return 0; 8 } 9 } โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Game.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Done โ score() now returns 0. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > run the tests -
> run the tests โบ Bash(mkdir -p build/classes build/test-classes &&
javac -d build/classes src/main/java/Game.java && javac -cp lib/junit-3.8.1.jar:build/classes -d build/test-classesโฆ) โฟ Runningโฆ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Bash command mkdir -p build/classes build/test-classes && javac -d build/classes src/main/java/Game.java && javac -cp lib/junit-3.8.1.jar:build/classes -d build/test-classes src/test/java/Test.java && java -cp lib/junit-3.8.1.jar:build/classes:build/test-classes junit.textui.TestRunner Test Compile and run JUnit tests Do you want to proceed? (Tab to add further instructions) โฏ 1. Yes 2. Yes, and don't ask again for mkdir, javac, and java commands in /Users/philipschwarz/dev/claude-code-experiment 3. No Esc to cancel > run the tests โบ Bash(mkdir -p build/classes build/test-classes && javac -d build/classes src/main/java/Game.java && javac -cp lib/junit-3.8.1.jar:build/classes -d build/test-classesโฆ) โฟ . Time: 0.002 OK (1 test) โบ 1 test ran, all passing. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > add a second test called testAllOnes -
The Second test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ public void testGutterGame() throws Exception { Game g = new Game(); for (int i = 0; i < 20; i++) g.roll(0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { Game g = new Game(); for (int i = 0; i < 20; i++) g.roll(1); assertEquals(20, g.score()); } } public class Game { public void roll(int pins) { } public int score() { return 0; } } 21 -
20 21 Add a new test that is called testAllOnes,
and ony differs from testGutterGame in that it invokes roll with one rather than with zero, and it asserts that the result of invoking score is twenty rather than zero -
> Add a new test that is called testAllOnes, and
ony differs from testGutterGame in that it invokes roll with one rather than with zero, and it asserts that the result of invoking score is twenty rather than zero โบ Read(src/test/java/Test.java) โฟ Read 13 lines โบ Update(src/test/java/Test.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/test/java/Test.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 9 } 10 assertEquals(0, g.score()); 11 } 12 + 13 + public void testAllOnes() { 14 + Game g = new Game(); 15 + for (int i = 0; i < 20; i++) { 16 + g.roll(1); 17 + } 18 + assertEquals(20, g.score()); 19 + } 20 } โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Test.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Added testAllOnes. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > run the tests -
The Second test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ public void testGutterGame() throws Exception { Game g = new Game(); for (int i = 0; i < 20; i++) g.roll(0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { Game g = new Game(); for (int i = 0; i < 20; i++) g.roll(1); assertEquals(20, g.score()); } } public class Game { public void roll(int pins) { } public int score() { return 0; } } - Game creation is duplicated - roll loop is duplicated 22 -
The Second test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ public void testGutterGame() throws Exception { Game g = new Game(); for (int i = 0; i < 20; i++) g.roll(0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { Game g = new Game(); for (int i = 0; i < 20; i++) g.roll(1); assertEquals(20, g.score()); } } public class Game { public void roll(int pins) { } public int score() { return 0; } } - Game creation is duplicated - roll loop is duplicated expected:<20> but was:<0> 23 -
> run the tests โบ Bash(mkdir -p build/classes build/test-classes &&
javac -d build/classes src/main/java/Game.java && javac -cp lib/junit-3.8.1.jar:build/classes -d build/test-classesโฆ) โฟ Error: Exit code 1 .F. Time: 0.002 There was 1 failure: 1) testAllOnes(Test)junit.framework.AssertionFailedError: expected:<20> but was:<0> at Test.testAllOnes(Test.java:18) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) โฆ +2 lines (ctrl+o to see all) โบ testGutterGame passes but testAllOnes fails โ score() still returns a hardcoded 0 instead of summing the rolls. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > -
The Second test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ private Game g; protected void setUp() throws Exception { g = new Game(); } public void testGutterGame() throws Exception { for (int i = 0; i < 20; i++) g.roll(0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { for (int i = 0; i < 20; i++) g.roll(1); assertEquals(20, g.score()); } } public class Game { private int score = 0; public void roll(int pins) { score += pins; } public int score() { return score; } } - roll loop is duplicated 24 -
refactor g so that it is a private variable of
Game that is initialised with a Game instance before the execution of each test Add to Game an integer variable named score and initialised to zero Modify roll so that rather than doing nothing it increments variable score by the value of its pins argument Modify score method so that rather than always returning zero it returns variable score run the tests 23 24 -
> refactor g so that it is a private variable
of Game that is initialised with a Game instance before the execution of each test โบ Read(src/test/java/Test.java) โฟ Read 21 lines โบ Write(src/test/java/Test.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Overwrite file src/test/java/Test.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 2 3 public class Test extends TestCase { 4 5 + private Game g; 6 + 7 + protected void setUp() throws Exception { 8 + super.setUp(); 9 + g = new Game(); 10 + } 11 + 12 public void testGutterGame() { 6 - Game g = new Game(); 13 for (int i = 0; i < 20; i++) { 14 g.roll(0); 15 } ... 17 } 18 19 public void testAllOnes() { 14 - Game g = new Game(); 20 for (int i = 0; i < 20; i++) { 21 g.roll(1); 22 } โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to overwrite Test.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Refactored g to a private field, initialized in setUp() before each test. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > run the tests -
> Add to Game an integer variable named score and
initialised to zero โบ Read(src/main/java/Game.java) โฟ Read 10 lines โบ Update(src/main/java/Game.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/main/java/Game.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 1 public class Game { 2 3 + private int score = 0; 4 + 5 public void roll(int pins) { 6 } 7 โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Game.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Added private int score = 0; to Game. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > -
> Modify roll so that rather than doing nothing it
increments variable score by the value of its pins argument โบ Update(src/main/java/Game.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/main/java/Game.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 3 private int score = 0; 4 5 public void roll(int pins) { 6 + score += pins; 7 } 8 9 public int score() { โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Game.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Done โ roll now adds pins to score. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > -
> Modify score method so that rather than always returning
zero it returns variable score โบ Update(src/main/java/Game.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/main/java/Game.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 7 } 8 9 public int score() { 10 - return 0; 10 + return score; 11 } 12 } โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Game.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Done โ score() now returns the score variable. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > run the tests > run the tests โบ Bash(mkdir -p build/classes build/test-classes && javac -d build/classes src/main/java/Game.java && javac -cp lib/junit-3.8.1.jar:build/classes -d build/test-classesโฆ) โฟ .. Time: 0.001 OK (2 tests) โบ 2 tests ran, all passing. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > add a test for a spare -
The Second test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ private Game g; protected void setUp() throws Exception { g = new Game(); } public void testGutterGame() throws Exception { int n = 20; int pins = 0; for (int i = 0; i < n; i++) { g.roll(pins); } assertEquals(0, g.score()); } public void testAllOnes() throws Exception { for (int i = 0; i < 20; i++) g.roll(1); assertEquals(20, g.score()); } } public class Game { private int score = 0; public void roll(int pins) { score += pins; } public int score() { return score; } } - roll loop is duplicated 25 -
24 25 Refactor the line in testGutterGame that invokes the
roll method: extract 20, the max value of the range, into integer val n, and extract 0, the parameter passed to roll, into integer val pins run the tests -
> Refactor the line in testGutterGame that invokes the roll
method: extract 20, the max value of the range, into integer val n, and extract 0, the parameter passed to roll, into integer val pins โบ Update(src/test/java/Test.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/test/java/Test.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 10 } 11 12 public void testGutterGame() { 13 - for (int i = 0; i < 20; i++) { 14 - g.roll(0); 13 + int n = 20; 14 + int pins = 0; 15 + for (int i = 0; i < n; i++) { 16 + g.roll(pins); 17 } 18 assertEquals(0, g.score()); 19 } โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Test.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Extracted n = 20 and pins = 0 in testGutterGame. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > > run the tests โบ Bash(mkdir -p build/classes build/test-classes && javac -d build/classes src/main/java/Game.java && javac -cp lib/junit-3.8.1.jar:build/classes -d build/test-classesโฆ) โฟ .. Time: 0.001 OK (2 tests) โบ 2 tests ran, all passing. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > -
The Second test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ private Game g; protected void setUp() throws Exception { g = new Game(); } public void testGutterGame() throws Exception { int n = 20; int pins = 0; rollMany(n, pins); assertEquals(0, g.score()); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testAllOnes() throws Exception { for (int i = 0; i < 20; i++) g.roll(1); assertEquals(20, g.score()); } } public class Game { private int score = 0; public void roll(int pins) { score += pins; } public int score() { return score; } } - roll loop is duplicated 26 -
The Second test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ private Game g; protected void setUp() throws Exception { g = new Game(); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testAllOnes() throws Exception { for (int i = 0; i < 20; i++) g.roll(1); assertEquals(20, g.score()); } } public class Game { private int score = 0; public void roll(int pins) { score += pins; } public int score() { return score; } } - roll loop is duplicated 27 -
Extract into method rollMany the loop that invokes method roll
in testGutterGame run the tests 26 27 25 26 -
> Extract into method rollMany the loop that invokes method
roll in testGutterGame โบ Update(src/test/java/Test.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/test/java/Test.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 9 g = new Game(); 10 } 11 12 - public void testGutterGame() { 13 - int n = 20; 14 - int pins = 0; 12 + private void rollMany(int n, int pins) { 13 for (int i = 0; i < n; i++) { 14 g.roll(pins); 15 } 16 + } 17 + 18 + public void testGutterGame() { 19 + rollMany(20, 0); 20 assertEquals(0, g.score()); 21 } 22 โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Test.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Extracted rollMany from testGutterGame. The loop in testAllOnes is unchanged. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > now use rollMany in testAllOnes too -
The Second test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ private Game g; protected void setUp() throws Exception { g = new Game(); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } } public class Game { private int score = 0; public void roll(int pins) { score += pins; } public int score() { return score; } } - roll loop is duplicated 28 -
The Second test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } } public class Game { private int score = 0; public void roll(int pins) { score += pins; } public int score() { return score; } } 29 -
28 29 27 28 now use rollMany in testAllOnes too
run the tests -
> now use rollMany in testAllOnes too โบ Update(src/test/java/Test.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Edit file src/test/java/Test.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 21 } 22 23 public void testAllOnes() { 24 - for (int i = 0; i < 20; i++) { 25 - g.roll(1); 26 - } 24 + rollMany(20, 1); 25 assertEquals(20, g.score()); 26 } 27 } โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Test.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Done โ testAllOnes now uses rollMany(20, 1). โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > run the tests > run the tests โบ Bash(mkdir -p build/classes build/test-classes && javac -d build/classes src/main/java/Game.java && javac -cp lib/junit-3.8.1.jar:build/classes -d build/test-classesโฆ) โฟ .. Time: 0.001 OK (2 tests) โบ 2 tests ran, all passing. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > -
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { g.roll(5); g.roll(5); // spare g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } } public class Game { private int score = 0; public void roll(int pins) { score += pins; } public int score() { return score; } } - ugly comment in test. 30 -
29 30 Write new test testOneSpare which (1) invokes roll
twice with pins=5, and once with pins=3 (2) invokes rollMany with n=17 and pins=0 (3) asserts that the result of invoking score is 16 Append comment "// spare" to the second line in testOneSpare that invokes roll with pins=5 -
> Write new test testOneSpare which (1) invokes roll twice
with pins=5, and once with pins= 3 (2) invokes rollMany with n=17 and pins=0 (3) asserts that the result of invoking score is 16 โบ Update(src/test/java/Test.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/test/java/Test.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 20 assertEquals(0, g.score()); 21 } 22 23 + public void testOneSpare() { 24 + g.roll(5); 25 + g.roll(5); 26 + g.roll(3); 27 + rollMany(17, 0); 28 + assertEquals(16, g.score()); 29 + } 30 + 31 public void testAllOnes() { 32 rollMany(20, 1); 33 assertEquals(20, g.score()); โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Test.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Added testOneSpare. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > run the tests -
> Append comment "// spare" to the second line in
testOneSpare that invokes roll with pins=5 โบ Update(src/test/java/Test.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/test/java/Test.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 22 23 public void testOneSpare() { 24 g.roll(5); 25 - g.roll(5); 25 + g.roll(5); // spare 26 g.roll(3); 27 rollMany(17, 0); 28 assertEquals(16, g.score()); โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Test.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Done. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > -
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { g.roll(5); g.roll(5); // spare g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } } public class Game { private int score = 0; public void roll(int pins) { score += pins; } public int score() { return score; } } - ugly comment in test. expected:<16> but was:<13> 31 -
> run the tests โบ Bash(mkdir -p build/classes build/test-classes &&
javac -d build/classes src/main/java/Game.java && javac -cp lib/junit-3.8.1.jar:build/classes -d build/test-classesโฆ) โฟ Error: Exit code 1 ..F. Time: 0.002 There was 1 failure: 1) testOneSpare(Test)junit.framework.AssertionFailedError: expected:<16> but was:<13> at Test.testOneSpare(Test.java:28) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) โฆ +2 lines (ctrl+o to see all) โบ testOneSpare fails โ expected 16 but got 13. The spare bonus (adding the next roll's pins) isn't implemented yet. The other 2 tests pass. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > -
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { g.roll(5); g.roll(5); // spare g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } } public class Game { private int score = 0; public void roll(int pins) { score += pins; } public int score() { return score; } } - ugly comment in test. tempted to use flag to remember previous roll. So design must be wrong. 32 -
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { g.roll(5); g.roll(5); // spare g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } } public class Game { private int score = 0; public void roll(int pins) { score += pins; } public int score() { return score; } } - ugly comment in test. roll() calculates score, but name does not imply that. score() does not calculate score, but name implies that it does. Design is wrong. Responsibilities are misplaced. 33 -
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } // public void testOneSpare() throws Exception { // g.roll(5); // g.roll(5); // spare // g.roll(3); // rollMany(17,0); // assertEquals(16,g.score()); // } } public class Game { private int score = 0; public void roll(int pins) { score += pins; } public int score() { return score; } } - ugly comment in test. 34 -
> comment out testOneSpare โบ Update(src/test/java/Test.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/test/java/Test.java
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 20 assertEquals(0, g.score()); 21 } 22 23 - public void testOneSpare() { 24 - g.roll(5); 25 - g.roll(5); // spare 26 - g.roll(3); 27 - rollMany(17, 0); 28 - assertEquals(16, g.score()); 29 - } 23 + // public void testOneSpare() { 24 + // g.roll(5); 25 + // g.roll(5); // spare 26 + // g.roll(3); 27 + // rollMany(17, 0); 28 + // assertEquals(16, g.score()); 29 + // } 30 31 public void testAllOnes() { 32 rollMany(20, 1); โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Test.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Done โ testOneSpare is commented out. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > show the current state of Game -
> run the tests โบ Bash(mkdir -p build/classes build/test-classes &&
javac -d build/classes src/main/java/Game.java && javac -cp lib/junit-3.8.1.jar:build/classes -d build/test-classesโฆ) โฟ .. Time: 0.001 OK (2 tests) โบ 2 tests ran, all passing. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > -
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } // public void testOneSpare() throws Exception { // g.roll(5); // g.roll(5); // spare // g.roll(3); // rollMany(17,0); // assertEquals(16,g.score()); // } } public class Game { private int score = 0; private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { score += pins; rolls[currentRoll++] = pins; } public int score() { return score; } } - ugly comment in test. 35 -
Add to Game (1) integer array variable rolls initialized with
an array of 21 zeroes (2) integer variable currentRoll initialized with zero run the tests Add to roll a second line that sets rolls element at index currentRoll to pins and increments currentRoll by 1 34 35 -
> Add to Game (1) integer array variable rolls initialized
with an array of 21 zeroes (2) integer variable currentRoll initialized with zero โบ Update(src/main/java/Game.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/main/java/Game.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 1 public class Game { 2 3 private int score = 0; 4 + private int[] rolls = new int[21]; 5 + private int currentRoll = 0; 6 7 public void roll(int pins) { 8 score += pins; โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Game.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Done โ added rolls (array of 21 zeroes) and currentRoll (initialized to 0). โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > -
> Add to roll a second line that sets rolls
element at index currentRoll to pins and increments currentRoll by 1 โบ Update(src/main/java/Game.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/main/java/Game.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 6 7 public void roll(int pins) { 8 score += pins; 9 + rolls[currentRoll++] = pins; 10 } 11 12 public int score() { โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Game.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Done โ added rolls[currentRoll++] = pins; to roll. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > run the tests > run the tests โบ Bash(mkdir -p build/classes build/test-classes && javac -d build/classes src/main/java/Game.java && javac -cp lib/junit-3.8.1.jar:build/classes -d build/test-classesโฆ) โฟ .. Time: 0.002 OK (2 tests) โบ 2 tests ran, all passing. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > -
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } // public void testOneSpare() throws Exception { // g.roll(5); // g.roll(5); // spare // g.roll(3); // rollMany(17,0); // assertEquals(16,g.score()); // } } public class Game { private int score = 0; private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { score += pins; rolls[currentRoll++] = pins; } public int score() { int score = 0; for (int i = 0; i < rolls.length; i++) score += rolls[i]; return score; } } - ugly comment in test. 36 -
run the tests Modify score method so that (1) it
defines integer variable score initialised to zero (2) loops through rolls elements adding them to score 35 36 -
> Modify score method so that (1) it defines integer
variable score initialised to zero (2) loops through rolls elements adding them to score โบ Update(src/main/java/Game.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/main/java/Game.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 10 } 11 12 public int score() { 13 + int score = 0; 14 + for (int i = 0; i < rolls.length; i++) { 15 + score += rolls[i]; 16 + } 17 return score; 18 } 19 } โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Game.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Done โ score() now sums the rolls array using a local score variable. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > run the tests > run the tests โบ Bash(mkdir -p build/classes build/test-classes && javac -d build/classes src/main/java/Game.java && javac -cp lib/junit-3.8.1.jar:build/classes -d build/test-classesโฆ) โฟ .. Time: 0.002 OK (2 tests) โบ 2 tests ran, all passing. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > -
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } // public void testOneSpare() throws Exception { // g.roll(5); // g.roll(5); // spare // g.roll(3); // rollMany(17,0); // assertEquals(16,g.score()); // } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; for (int i = 0; i < rolls.length; i++) score += rolls[i]; return score; } } - ugly comment in test. 37 -
> remove variable score from Game โบ Update(src/main/java/Game.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit
file src/main/java/Game.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 1 public class Game { 2 3 - private int score = 0; 3 private int[] rolls = new int[21]; 4 private int currentRoll = 0; 5 โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Game.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Update(src/main/java/Game.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/main/java/Game.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 4 private int currentRoll = 0; 5 6 public void roll(int pins) { 7 - score += pins; 7 rolls[currentRoll++] = pins; 8 } 9 โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Game.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Removed the score field and the score += pins; line in roll. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > run the tests -
> run the tests โบ Bash(mkdir -p build/classes build/test-classes &&
javac -d build/classes src/main/java/Game.java && javac -cp lib/junit-3.8.1.jar:build/classes -d build/test-classesโฆ) โฟ .. Time: 0.001 OK (2 tests) โบ 2 tests ran, all passing. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > uncomment testOneSpare -
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { g.roll(5); g.roll(5); // spare g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; for (int i = 0; i < rolls.length; i++) score += rolls[i]; return score; } } - ugly comment in test. expected:<16> but was:<13> 38 -
> uncomment testOneSpare โบ Update(src/test/java/Test.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/test/java/Test.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
20 assertEquals(0, g.score()); 21 } 22 23 - // public void testOneSpare() { 24 - // g.roll(5); 25 - // g.roll(5); // spare 26 - // g.roll(3); 27 - // rollMany(17, 0); 28 - // assertEquals(16, g.score()); 29 - // } 23 + public void testOneSpare() { 24 + g.roll(5); 25 + g.roll(5); // spare 26 + g.roll(3); 27 + rollMany(17, 0); 28 + assertEquals(16, g.score()); 29 + } 30 31 public void testAllOnes() { 32 rollMany(20, 1); โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Test.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Done โ testOneSpare is uncommented. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > run the tests -
> run the tests โบ Bash(mkdir -p build/classes build/test-classes &&
javac -d build/classes src/main/java/Game.java && javac -cp lib/junit-3.8.1.jar:build/classes -d build/test-classesโฆ) โฟ Error: Exit code 1 ..F. Time: 0.003 There was 1 failure: 1) testOneSpare(Test)junit.framework.AssertionFailedError: expected:<16> but was:<13> at Test.testOneSpare(Test.java:28) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) โฆ +2 lines (ctrl+o to see all) โบ testOneSpare fails โ expected 16 but got 13. The spare bonus logic still needs to be implemented in score(). โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > -
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { g.roll(5); g.roll(5); // spare g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; for (int i = 0; i < rolls.length; i++) { if (rolls[i] + rolls[i+1] == 10) // spare score += ... score += rolls[i]; } return score; } } - ugly comment in test. This isnโt going to work because i might not refer to the first ball of the frame. Design is still wrong. Need to walk through array two balls (one frame) at a time. 39 -
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } // public void testOneSpare() throws Exception { // g.roll(5); // g.roll(5); // spare // g.roll(3); // rollMany(17,0); // assertEquals(16,g.score()); // } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; for (int i = 0; i < rolls.length; i++) score += rolls[i]; return score; } } - ugly comment in test. 40 -
> comment out testOneSpare โบ Update(src/test/java/Test.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/test/java/Test.java
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 20 assertEquals(0, g.score()); 21 } 22 23 - public void testOneSpare() { 24 - g.roll(5); 25 - g.roll(5); // spare 26 - g.roll(3); 27 - rollMany(17, 0); 28 - assertEquals(16, g.score()); 29 - } 23 + // public void testOneSpare() { 24 + // g.roll(5); 25 + // g.roll(5); // spare 26 + // g.roll(3); 27 + // rollMany(17, 0); 28 + // assertEquals(16, g.score()); 29 + // } 30 31 public void testAllOnes() { 32 rollMany(20, 1); โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Test.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Done โ testOneSpare is commented out. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > -
> run the tests โบ Bash(mkdir -p build/classes build/test-classes &&
javac -d build/classes src/main/java/Game.java && javac -cp lib/junit-3.8.1.jar:build/classes -d build/test-classesโฆ) โฟ .. Time: 0.001 OK (2 tests) โบ 2 tests ran, all passing. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > -
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } // public void testOneSpare() throws Exception { // g.roll(5); // g.roll(5); // spare // g.roll(3); // rollMany(17,0); // assertEquals(16,g.score()); // } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; int i = 0; for (int frame = 0; frame < 10; frame++) { score += rolls[i] + rolls[i+1]; i += 2; } return score; } } - ugly comment in test. 41 -
40 41 > In the score method: (1) rename i
to frame, except in the loop's body (2) change the exclusive upper bound of frame from rolls.length to 10 (3) before the loop, define integer variable i initialised to zero (4) change the loop's body from score += rolls[i]; to score += rolls[i] + rolls[i+1]; i+=2; -
> In the score method: (1) rename i to frame,
except in the loop's body (2) change the exclusive upper bound of frame from rolls.length to 10 (3) before the loop, define integer variable i initialised to zero (4) change the loop's body from score += rolls[i]; to score += rolls[i] + rolls[i+1]; i+=2; โบ Update(src/main/java/Game.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/main/java/Game.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 9 10 public int score() { 11 int score = 0; 12 - for (int i = 0; i < rolls.length; i++) { 13 - score += rolls[i]; 12 + int i = 0; 13 + for (int frame = 0; frame < 10; frame++) { 14 + score += rolls[i] + rolls[i + 1]; 15 + i += 2; 16 } 17 return score; 18 } โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Game.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Done. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > run the tests -
> run the tests โบ Bash(mkdir -p build/classes build/test-classes &&
javac -d build/classes src/main/java/Game.java && javac -cp lib/junit-3.8.1.jar:build/classes -d build/test-classesโฆ) โฟ .. Time: 0.001 OK (2 tests) โบ 2 tests ran, all passing. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > -
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { g.roll(5); g.roll(5); // spare g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; int i = 0; for (int frame = 0; frame < 10; frame++) { score += rolls[i] + rolls[i+1]; i += 2; } return score; } } - ugly comment in test. expected:<16> but was:<13> 42 -
> uncomment testOneSpare โบ Update(src/test/java/Test.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/test/java/Test.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
20 assertEquals(0, g.score()); 21 } 22 23 - // public void testOneSpare() { 24 - // g.roll(5); 25 - // g.roll(5); // spare 26 - // g.roll(3); 27 - // rollMany(17, 0); 28 - // assertEquals(16, g.score()); 29 - // } 23 + public void testOneSpare() { 24 + g.roll(5); 25 + g.roll(5); // spare 26 + g.roll(3); 27 + rollMany(17, 0); 28 + assertEquals(16, g.score()); 29 + } 30 31 public void testAllOnes() { 32 rollMany(20, 1); โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Test.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Done โ testOneSpare is uncommented. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > run the tests -
> run the tests โบ Bash(mkdir -p build/classes build/test-classes &&
javac -d build/classes src/main/java/Game.java && javac -cp lib/junit-3.8.1.jar:build/classes -d build/test-classesโฆ) โฟ Error: Exit code 1 ..F. Time: 0.003 There was 1 failure: 1) testOneSpare(Test)junit.framework.AssertionFailedError: expected:<16> but was:<13> at Test.testOneSpare(Test.java:28) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) โฆ +2 lines (ctrl+o to see all) โบ testOneSpare still fails โ expected 16 but got 13. The spare bonus (adding the next roll after the frame) isn't handled yet in score(). โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > -
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { g.roll(5); g.roll(5); // spare g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; int i = 0; for (int frame = 0; frame < 10; frame++) { if (rolls[i] + rolls[i + 1] == 10) // spare { score += 10 + rolls[i + 2]; i += 2; } else { score += rolls[i] + rolls[i + 1]; i += 2; } } return score; } } - ugly comment in test. 43 -
42 43 In the score method, change the body of
the loop from score += rolls[i] + rolls[i + 1]; i += 2; to if (rolls[i] + rolls[i + 1] == 10) // spare { score += 10 + rolls[i + 2]; i += 2; } else { score += rolls[i] + rolls[i + 1]; i += 2; } run the tests -
> In the score method, change the body of the
loop from score += rolls[i] + rolls[i + 1]; i += 2; to if (rolls[i] + rolls[i + 1] == 10) // spare { score += 10 + rolls[i + 2]; i += 2; } else { score += rolls[i] + rolls[i + 1]; i += 2; } โบ Update(src/main/java/Game.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/main/java/Game.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 11 int score = 0; 12 int i = 0; 13 for (int frame = 0; frame < 10; frame++) { 14 - score += rolls[i] + rolls[i+1]; 15 - i += 2; 14 + if (rolls[i] + rolls[i + 1] == 10) // spare 15 + { 16 + score += 10 + rolls[i + 2]; 17 + i += 2; 18 + } else { 19 + score += rolls[i] + rolls[i + 1]; 20 + i += 2; 21 + } 22 } 23 return score; 24 } โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Game.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Done โ added spare handling in the score() loop. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > run the tests -
> run the tests โบ Bash(mkdir -p build/classes build/test-classes &&
javac -d build/classes src/main/java/Game.java && javac -cp lib/junit-3.8.1.jar:build/classes -d build/test-classesโฆ) โฟ ... Time: 0.001 OK (3 tests) โบ 3 tests ran, all passing. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > Write new test testOneStrike -
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { g.roll(5); g.roll(5); // spare g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; int i = 0; for (int frame = 0; frame < 10; frame++) { if (rolls[i] + rolls[i + 1] == 10) // spare { score += 10 + rolls[i + 2]; i += 2; } else { score += rolls[i] + rolls[i + 1]; i += 2; } } return score; } } - ugly comment in test. - ugly comment in conditional. - i is a bad name for this variable 44 -
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { g.roll(5); g.roll(5); // spare g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; int frameIndex = 0; for (int frame = 0; frame < 10; frame++) { if (rolls[frameIndex] + rolls[frameIndex + 1] == 10) // spare { score += 10 + rolls[frameIndex + 2]; frameIndex += 2; } else { score += rolls[frameIndex] + rolls[frameIndex + 1]; frameIndex += 2; } } return score; } } - ugly comment in test. - ugly comment in conditional. 45 -
> rename i to frameIndex in score method โบ Read(src/main/java/Game.java)
โฟ Read 26 lines โบ Update(src/main/java/Game.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/main/java/Game.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 9 10 public int score() { 11 int score = 0; 12 - int i = 0; 12 + int frameIndex = 0; 13 for (int frame = 0; frame < 10; frame++) { 14 - if (rolls[i] + rolls[i + 1] == 10) // spare 14 + if (rolls[frameIndex] + rolls[frameIndex + 1] == 10) // spare 15 { 16 - score += 10 + rolls[i + 2]; 17 - i += 2; 16 + score += 10 + rolls[frameIndex + 2]; 17 + frameIndex += 2; 18 } else { 19 - score += rolls[i] + rolls[i + 1]; 20 - i += 2; 19 + score += rolls[frameIndex] + rolls[frameIndex + 1]; 20 + frameIndex += 2; 21 } 22 } 23 return score; โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Game.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Done โ renamed i to frameIndex throughout the score() method. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > run the tests -
> run the tests โบ Bash(mkdir -p build/classes build/test-classes &&
javac -d build/classes src/main/java/Game.java && javac -cp lib/junit-3.8.1.jar:build/classes -d build/test-classesโฆ) โฟ ... Time: 0.001 OK (3 tests) โบ 3 tests ran, all passing. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > Add a test for a strike -
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { g.roll(5); g.roll(5); // spare g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; int frameIndex = 0; for (int frame = 0; frame < 10; frame++) { if (isSpare(frameIndex)) { score += 10 + rolls[frameIndex + 2]; frameIndex += 2; } else { score += rolls[frameIndex] + rolls[frameIndex + 1]; frameIndex += 2; } } return score; } private boolean isSpare(int frameIndex) { return rolls[frameIndex] + rolls[frameIndex + 1] == 10; } } - ugly comment in test. 46 -
perform an 'extract method' refactoring on the boolean expression that
is tested in the conditional in the score() method, calling the new method 'isSpare' run the tests 45 46 -
> perform an 'extract method' refactoring on the boolean expression
that is tested in the conditional in the score() method, calling the new method 'isSpare' โบ Update(src/main/java/Game.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/main/java/Game.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 11 int score = 0; 12 int frameIndex = 0; 13 for (int frame = 0; frame < 10; frame++) { 14 - if (rolls[frameIndex] + rolls[frameIndex + 1] == 10) // spare 14 + if (isSpare(frameIndex)) 15 { 16 score += 10 + rolls[frameIndex + 2]; 17 frameIndex += 2; โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Game.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Update(src/main/java/Game.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/main/java/Game.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 7 rolls[currentRoll++] = pins; 8 } 9 10 + private boolean isSpare(int frameIndex) { 11 + return rolls[frameIndex] + rolls[frameIndex + 1] == 10; 12 + } 13 + 14 public int score() { 15 int score = 0; 16 int frameIndex = 0; โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Game.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Done โ extracted isSpare method and replaced the boolean expression with a call to it. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > run the tests -
โบ Done โ extracted isSpare method and replaced the boolean
expression with a call to it. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > run the tests > run the tests โบ Bash(mkdir -p build/classes build/test-classes && javac -d build/classes src/main/java/Game.java && javac -cp lib/junit-3.8.1.jar:build/classes -d build/test-classesโฆ) โฟ ... Time: 0.001 OK (3 tests) โบ 3 tests ran, all passing. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > Add a test for a strike -
The Third test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ private Game g; protected void setUp() throws Exception { g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) g.roll(pins); } public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { rollSpare(); g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } private void rollSpare() { g.roll(5); g.roll(5); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; int frameIndex = 0; for (int frame = 0; frame < 10; frame++) { if (isSpare(frameIndex)) { score += 10 + rolls[frameIndex + 2]; frameIndex += 2; } else { score += rolls[frameIndex] + rolls[frameIndex + 1]; frameIndex += 2; } } return score; } private boolean isSpare(int frameIndex) { return rolls[frameIndex] + rolls[frameIndex + 1] == 10; } } - 47 -
extract from testOneSpare a method called rollSpare that rolls two
fives run the tests 46 47 -
โบ Done โ extracted rollSpare() and replaced the two g.roll(5)
calls in testOneSpare with it. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > run the tests > extract from testOneSpare a method called rollSpare that rolls two fives โบ Read(src/test/java/Test.java) โฟ Read 36 lines โบ Update(src/test/java/Test.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/test/java/Test.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 20 assertEquals(0, g.score()); 21 } 22 23 - public void testOneSpare() { 23 + private void rollSpare() { 24 g.roll(5); 25 - g.roll(5); // spare 25 + g.roll(5); 26 + } 27 + 28 + public void testOneSpare() { 29 + rollSpare(); 30 g.roll(3); 31 rollMany(17, 0); 32 assertEquals(16, g.score()); โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Test.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel -
โบ Done โ extracted isSpare method and replaced the boolean
expression with a call to it. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > run the tests > run the tests โบ Bash(mkdir -p build/classes build/test-classes && javac -d build/classes src/main/java/Game.java && javac -cp lib/junit-3.8.1.jar:build/classes -d build/test-classesโฆ) โฟ ... Time: 0.001 OK (3 tests) โบ 3 tests ran, all passing. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > Add a test for a strike -
The Fourth test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ ... public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { rollSpare(); g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } public void testOneStrike() throws Exception { g.roll(10); // strike g.roll(3); g.roll(4); rollMany(16, 0); assertEquals(24, g.score()); } private void rollSpare() { g.roll(5); g.roll(5); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; int frameIndex = 0; for (int frame = 0; frame < 10; frame++) { if (isSpare(frameIndex)) { score += 10 + rolls[frameIndex + 2]; frameIndex += 2; } else { score += rolls[frameIndex] + rolls[frameIndex + 1]; frameIndex += 2; } } return score; } private boolean isSpare(int frameIndex) { return rolls[frameIndex] + rolls[frameIndex + 1] == 10; } } - ugly comment in testOneStrike. expected:<24> but was:<17> 48 -
Add the following test public void testOneStrike() throws Exception {
g.roll(10); // strike g.roll(3); g.roll(4); rollMany(16, 0); assertEquals(24, g.score()); } run the tests 47 48 -
> Add the following test public void testOneStrike() throws Exception
{ g.roll(10); // strike g.roll(3); g.roll(4); rollMany(16, 0); assertEquals(24, g.score()); } โบ Update(src/test/java/Test.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/test/java/Test.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 32 assertEquals(16, g.score()); 33 } 34 35 + public void testOneStrike() throws Exception { 36 + g.roll(10); // strike 37 + g.roll(3); 38 + g.roll(4); 39 + rollMany(16, 0); 40 + assertEquals(24, g.score()); 41 + } 42 + 43 public void testAllOnes() { 44 rollMany(20, 1); 45 assertEquals(20, g.score()); โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Test.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Added testOneStrike. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > run the tests -
> run the tests โบ Bash(mkdir -p build/classes build/test-classes &&
javac -d build/classes src/main/java/Game.java && javac -cp lib/junit-3.8.1.jar:build/classes -d build/test-classesโฆ) โฟ Error: Exit code 1 ..F.. Time: 0.003 There was 1 failure: 1) testOneStrike(Test)junit.framework.AssertionFailedError: expected:<24> but was:<17> at Test.testOneStrike(Test.java:40) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) โฆ +2 lines (ctrl+o to see all) โบ testOneStrike fails โ expected 24 but got 17. The strike bonus logic isn't implemented yet in score(). โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > add strike handling to the score method -
The Fourth test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ ... public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { rollSpare(); g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } public void testOneStrike() throws Exception { g.roll(10); // strike g.roll(3); g.roll(4); rollMany(16, 0); assertEquals(24, g.score()); } private void rollSpare() { g.roll(5); g.roll(5); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; int frameIndex = 0; for (int frame = 0; frame < 10; frame++) { if (rolls[frameIndex] == 10) // strike { score += 10 + rolls[frameIndex+1] + rolls[frameIndex+2]; frameIndex++; } else if (isSpare(frameIndex)) { score += 10 + rolls[frameIndex + 2]; frameIndex += 2; } else { score += rolls[frameIndex] + rolls[frameIndex + 1]; frameIndex += 2; } } return score; } private boolean isSpare(int frameIndex) { return rolls[frameIndex] + rolls[frameIndex + 1] == 10; } } - ugly comment in testOneStrike. - ugly comment in conditional. - ugly expressions. 49 -
prefix the conditional in score() with the following: if (rolls[frameIndex]
== 10) // strike { score += 10 + rolls[frameIndex+1] + rolls[frameIndex+2]; frameIndex++; } else run the tests 48 49 -
> prefix the conditional in score() with the following: if
(rolls[frameIndex] == 10) // strike { score += 10 + rolls[frameIndex+1] + rolls[frameIndex+2]; frameIndex++; } else โบ Update(src/main/java/Game.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/main/java/Game.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 15 int score = 0; 16 int frameIndex = 0; 17 for (int frame = 0; frame < 10; frame++) { 18 - if (isSpare(frameIndex)) 18 + if (rolls[frameIndex] == 10) // strike 19 { 20 + score += 10 + 21 + rolls[frameIndex+1] + 22 + rolls[frameIndex+2]; 23 + frameIndex++; 24 + } 25 + else if (isSpare(frameIndex)) 26 + { 27 score += 10 + rolls[frameIndex + 2]; 28 frameIndex += 2; 29 } else { โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Game.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Done โ added strike handling before the spare conditional. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > run the tests -
> run the tests โบ Bash(mkdir -p build/classes build/test-classes &&
javac -d build/classes src/main/java/Game.java && javac -cp lib/junit-3.8.1.jar:build/classes -d build/test-classesโฆ) โฟ .... Time: 0.002 OK (4 tests) โบ 4 tests ran, all passing. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > -
The Fourth test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ ... public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { rollSpare(); g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } public void testOneStrike() throws Exception { g.roll(10); // strike g.roll(3); g.roll(4); rollMany(16, 0); assertEquals(24, g.score()); } private void rollSpare() { g.roll(5); g.roll(5); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; int frameIndex = 0; for (int frame = 0; frame < 10; frame++) { if (rolls[frameIndex] == 10) // strike { score += 10 + strikeBonus(frameIndex); frameIndex++; } else if (isSpare(frameIndex)) { score += 10 + spareBonus(frameIndex); frameIndex += 2; } else { score += sumOfBallsInFrame(frameIndex); frameIndex += 2; } } return score; } private int sumOfBallsInFrame(int frameIndex) { return rolls[frameIndex]+rolls[frameIndex+1]; } private int spareBonus(int frameIndex) { return rolls[frameIndex + 2]; } private int strikeBonus(int frameIndex) { return rolls[frameIndex+1]+rolls[frameIndex+2]; } private boolean isSpare(int frameIndex) { return rolls[frameIndex]+rolls[frameIndex+1] == 10; } } - ugly comment in testOneStrike. - ugly comment in conditional. 50 -
extract the following functions from the body of method score():
private int sumOfBallsInFrame(int frameIndex) { return rolls[frameIndex]+rolls[frameIndex+1]; } private int spareBonus(int frameIndex) { return rolls[frameIndex + 2]; } private int strikeBonus(int frameIndex) { return rolls[frameIndex+1]+rolls[frameIndex+2]; } run the tests 49 50 -
> extract the following functions from the body of method
score(): private int sumOfBallsInFrame(int frameIndex) { return rolls[frameIndex]+rolls[frameIndex+1]; } private int spareBonus(int frameIndex) { return rolls[frameIndex + 2]; } private int strikeBonus(int frameIndex) { return rolls[frameIndex+1]+rolls[frameIndex+2]; } โบ Read(src/main/java/Game.java) โฟ Read 37 lines โบ Update(src/main/java/Game.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/main/java/Game.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 11 return rolls[frameIndex] + rolls[frameIndex + 1] == 10; 12 } 13 14 + private int sumOfBallsInFrame(int frameIndex) { 15 + return rolls[frameIndex]+rolls[frameIndex+1]; 16 + } 17 + 18 + private int spareBonus(int frameIndex) { 19 + return rolls[frameIndex + 2]; 20 + } 21 + 22 + private int strikeBonus(int frameIndex) { 23 + return rolls[frameIndex+1]+rolls[frameIndex+2]; 24 + } 25 + 26 public int score() { 27 int score = 0; 28 int frameIndex = 0; โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Game.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel -
โบ Update(src/main/java/Game.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/main/java/Game.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 29 for (int
frame = 0; frame < 10; frame++) { 30 if (rolls[frameIndex] == 10) // strike 31 { 32 - score += 10 + 33 - rolls[frameIndex+1] + 34 - rolls[frameIndex+2]; 32 + score += 10 + strikeBonus(frameIndex); 33 frameIndex++; 34 } 35 else if (isSpare(frameIndex)) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Game.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Update(src/main/java/Game.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/main/java/Game.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 34 } 35 else if (isSpare(frameIndex)) 36 { 37 - score += 10 + rolls[frameIndex + 2]; 37 + score += 10 + spareBonus(frameIndex); 38 frameIndex += 2; 39 } else { 40 score += rolls[frameIndex] + rolls[frameIndex + 1]; โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Game.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel -
โบ Update(src/main/java/Game.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/main/java/Game.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 37 score +=
10 + spareBonus(frameIndex); 38 frameIndex += 2; 39 } else { 40 - score += rolls[frameIndex] + rolls[frameIndex + 1]; 40 + score += sumOfBallsInFrame(frameIndex); 41 frameIndex += 2; 42 } 43 } โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Game.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Done โ extracted sumOfBallsInFrame, spareBonus, and strikeBonus methods and replaced the inline expressions in score(). โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > run the tests > run the tests โบ Bash(mkdir -p build/classes build/test-classes && javac -d build/classes src/main/java/Game.java && javac -cp lib/junit-3.8.1.jar:build/classes -d build/test-classesโฆ) โฟ .... Time: 0.002 OK (4 tests) โบ 4 tests ran, all passing. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > -
The Fourth test. import junit.framework.TestCase; public class BowlingGameTest extends TestCase
{ ... public void testGutterGame() throws Exception { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { rollSpare(); g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } public void testOneStrike() throws Exception { g.roll(10); // strike g.roll(3); g.roll(4); rollMany(16, 0); assertEquals(24, g.score()); } private void rollSpare() { g.roll(5); g.roll(5); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; int frameIndex = 0; for (int frame = 0; frame < 10; frame++) { if (isStrike(frameIndex)) { score += 10 + strikeBonus(frameIndex); frameIndex++; } else if (isSpare(frameIndex)) { score += 10 + spareBonus(frameIndex); frameIndex += 2; } else { score += sumOfBallsInFrame(frameIndex); frameIndex += 2; } } return score; } private boolean isStrike(int frameIndex) { return rolls[frameIndex] == 10; } private int sumOfBallsInFrame(int frameIndex) { return rolls[frameIndex] + rolls[frameIndex+1]; } private int spareBonus(int frameIndex) { return rolls[frameIndex+2]; } private int strikeBonus(int frameIndex) { return rolls[frameIndex+1] + rolls[frameIndex+2]; } private boolean isSpare(int frameIndex) { return rolls[frameIndex]+rolls[frameIndex+1] == 10; } } - ugly comment in testOneStrike. 51 -
> extract the following into method isStrike: rolls[frameIndex] == 10
โบ Update(src/main/java/Game.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/main/java/Game.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 7 rolls[currentRoll++] = pins; 8 } 9 10 + private boolean isStrike(int frameIndex) { 11 + return rolls[frameIndex] == 10; 12 + } 13 + 14 private boolean isSpare(int frameIndex) { 15 return rolls[frameIndex] + rolls[frameIndex + 1] == 10; 16 } โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Game.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Update(src/main/java/Game.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/main/java/Game.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 31 int score = 0; 32 int frameIndex = 0; 33 for (int frame = 0; frame < 10; frame++) { 34 - if (rolls[frameIndex] == 10) // strike 34 + if (isStrike(frameIndex)) 35 { 36 score += 10 + strikeBonus(frameIndex); 37 frameIndex++; โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Game.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Done โ extracted isStrike method and replaced the inline expression. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > run the tests -
> run the tests โบ Bash(mkdir -p build/classes build/test-classes &&
javac -d build/classes src/main/java/Game.java && javac -cp lib/junit-3.8.1.jar:build/classes -d build/test-classesโฆ) โฟ .... Time: 0.002 OK (4 tests) โบ 4 tests ran, all passing. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > -
The Fourth test. ... public void testGutterGame() throws Exception {
rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { rollSpare(); g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } public void testOneStrike() throws Exception { rollStrike(); g.roll(3); g.roll(4); rollMany(16, 0); assertEquals(24, g.score()); } private void rollStrike() { g.roll(10); } private void rollSpare() { g.roll(5); g.roll(5); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; int frameIndex = 0; for (int frame = 0; frame < 10; frame++) { if (isStrike(frameIndex)) { score += 10 + strikeBonus(frameIndex); frameIndex++; } else if (isSpare(frameIndex)) { score += 10 + spareBonus(frameIndex); frameIndex += 2; } else { score += sumOfBallsInFrame(frameIndex); frameIndex += 2; } } return score; } private boolean isStrike(int frameIndex) { return rolls[frameIndex] == 10; } private int sumOfBallsInFrame(int frameIndex) { return rolls[frameIndex] + rolls[frameIndex+1]; } private int spareBonus(int frameIndex) { return rolls[frameIndex+2]; } private int strikeBonus(int frameIndex) { return rolls[frameIndex+1] + rolls[frameIndex+2]; } private boolean isSpare(int frameIndex) { return rolls[frameIndex]+rolls[frameIndex+1] == 10; } } 52 -
> extract g.roll(10) into method rollStrike โบ Update(src/test/java/Test.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit
file src/test/java/Test.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 33 } 34 35 public void testOneStrike() throws Exception { 36 - g.roll(10); // strike 36 + rollStrike(); 37 g.roll(3); 38 g.roll(4); 39 rollMany(16, 0); โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Test.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Update(src/test/java/Test.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/test/java/Test.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 25 g.roll(5); 26 } 27 28 + private void rollStrike() { 29 + g.roll(10); 30 + } 31 + 32 public void testOneSpare() { 33 rollSpare(); 34 g.roll(3); โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Test.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Done โ extracted rollStrike method. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > run the tests -
> run the tests โบ Bash(mkdir -p build/classes build/test-classes &&
javac -d build/classes src/main/java/Game.java && javac -cp lib/junit-3.8.1.jar:build/classes -d build/test-classesโฆ) โฟ .... Time: 0.002 OK (4 tests) โบ 4 tests ran, all passing. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > -
The Fifth test. ... public void testGutterGame() throws Exception {
rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() throws Exception { rollMany(20,1); assertEquals(20, g.score()); } public void testOneSpare() throws Exception { rollSpare(); g.roll(3); rollMany(17,0); assertEquals(16,g.score()); } public void testOneStrike() throws Exception { rollStrike(); g.roll(3); g.roll(4); rollMany(16, 0); assertEquals(24, g.score()); } public void testPerfectGame() throws Exception { rollMany(12,10); assertEquals(300, g.score()); } private void rollStrike() { g.roll(10); } private void rollSpare() { g.roll(5); g.roll(5); } } public class Game { private int rolls[] = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; int frameIndex = 0; for (int frame = 0; frame < 10; frame++) { if (isStrike(frameIndex)) { score += 10 + strikeBonus(frameIndex); frameIndex++; } else if (isSpare(frameIndex)) { score += 10 + spareBonus(frameIndex); frameIndex += 2; } else { score += sumOfBallsInFrame(frameIndex); frameIndex += 2; } } return score; } private boolean isStrike(int frameIndex) { return rolls[frameIndex] == 10; } private int sumOfBallsInFrame(int frameIndex) { return rolls[frameIndex] + rolls[frameIndex+1]; } private int spareBonus(int frameIndex) { return rolls[frameIndex+2]; } private int strikeBonus(int frameIndex) { return rolls[frameIndex+1] + rolls[frameIndex+2]; } private boolean isSpare(int frameIndex) { return rolls[frameIndex]+rolls[frameIndex+1] == 10; } } 53 -
Add this final test: public void testPerfectGame() throws Exception {
rollMany(12,10); assertEquals(300, g.score()); } run the tests 52 53 -
> Add this final test: public void testPerfectGame() throws Exception
{ rollMany(12,10); assertEquals(300, g.score()); } โบ Update(src/test/java/Test.java) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Edit file src/test/java/Test.java โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 44 assertEquals(24, g.score()); 45 } 46 47 + public void testPerfectGame() throws Exception { 48 + rollMany(12,10); 49 + assertEquals(300, g.score()); 50 + } 51 + 52 public void testAllOnes() { 53 rollMany(20, 1); 54 assertEquals(20, g.score()); โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Do you want to make this edit to Test.java? (Tab to add further instructions) โฏ 1. Yes 2. Yes, allow all edits during this session (shift+tab) 3. No Esc to cancel โบ Added testPerfectGame. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > run the tests -
> run the tests โบ Bash(mkdir -p build/classes build/test-classes &&
javac -d build/classes src/main/java/Game.java && javac -cp lib/junit-3.8.1.jar:build/classes -d build/test- classesโฆ) โฟ ..... Time: 0.001 OK (5 tests) โบ 5 tests ran, all passing. โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > show me Game.java -
In the next slide we tell Claude to show the
final code. The subsequent slide is the final one, and shows the same code, but with methods grouped according to their access modifiers (private/public), and with a splash of colour added to aid comprehension. -
> show me Game.java โบ Read(src/main/java/Game.java) โฟ Read 51 lines
โบ public class Game { private int[] rolls = new int[21]; private int currentRoll = 0; public void roll(int pins) { rolls[currentRoll++] = pins; } private boolean isStrike(int frameIndex) { return rolls[frameIndex] == 10; } private boolean isSpare(int frameIndex) { return rolls[frameIndex] + rolls[frameIndex + 1] == 10; } private int sumOfBallsInFrame(int frameIndex) { return rolls[frameIndex]+rolls[frameIndex+1]; } private int spareBonus(int frameIndex) { return rolls[frameIndex + 2]; } private int strikeBonus(int frameIndex) { return rolls[frameIndex+1]+rolls[frameIndex+2]; } public int score() { int score = 0; int frameIndex = 0; for (int frame = 0; frame < 10; frame++) { if (isStrike(frameIndex)) { score += 10 + strikeBonus(frameIndex); frameIndex++; } else if (isSpare(frameIndex)) { score += 10 + spareBonus(frameIndex); frameIndex += 2; } else { score += sumOfBallsInFrame(frameIndex); frameIndex += 2; } } return score; } } โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ > show me Test.java โบ Read(src/test/java/Test.java) โฟ Read 57 lines โบ import junit.framework.TestCase; public class Test extends TestCase { private Game g; protected void setUp() throws Exception { super.setUp(); g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) { g.roll(pins); } } public void testGutterGame() { rollMany(20, 0); assertEquals(0, g.score()); } private void rollSpare() { g.roll(5); g.roll(5); } private void rollStrike() { g.roll(10); } public void testOneSpare() { rollSpare(); g.roll(3); rollMany(17, 0); assertEquals(16, g.score()); } public void testOneStrike() throws Exception { rollStrike(); g.roll(3); g.roll(4); rollMany(16, 0); assertEquals(24, g.score()); } public void testPerfectGame() throws Exception { rollMany(12,10); assertEquals(300, g.score()); } public void testAllOnes() { rollMany(20, 1); assertEquals(20, g.score()); } } โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ -
public class Game { private int[] rolls = new int[21];
private int currentRoll = 0; private boolean isStrike(int frameIndex) { return rolls[frameIndex] == 10; } private boolean isSpare(int frameIndex) { return rolls[frameIndex] + rolls[frameIndex + 1] == 10; } private int sumOfBallsInFrame(int frameIndex) { return rolls[frameIndex]+rolls[frameIndex+1]; } private int spareBonus(int frameIndex) { return rolls[frameIndex + 2]; } private int strikeBonus(int frameIndex) { return rolls[frameIndex+1]+rolls[frameIndex+2]; } public void roll(int pins) { rolls[currentRoll++] = pins; } public int score() { int score = 0; int frameIndex = 0; for (int frame = 0; frame < 10; frame++) { if (isStrike(frameIndex)) { score += 10 + strikeBonus(frameIndex); frameIndex++; } else if (isSpare(frameIndex)) { score += 10 + spareBonus(frameIndex); frameIndex += 2; } else { score += sumOfBallsInFrame(frameIndex); frameIndex += 2; } } return score; } } import junit.framework.TestCase; public class Test extends TestCase { private Game g; protected void setUp() throws Exception { super.setUp(); g = new Game(); } private void rollMany(int n, int pins) { for (int i = 0; i < n; i++) { g.roll(pins); } } private void rollSpare() { g.roll(5); g.roll(5); } private void rollStrike() { g.roll(10); } public void testGutterGame() { rollMany(20, 0); assertEquals(0, g.score()); } public void testAllOnes() { rollMany(20, 1); assertEquals(20, g.score()); } public void testOneSpare() { rollSpare(); g.roll(3); rollMany(17, 0); assertEquals(16, g.score()); } public void testOneStrike() throws Exception { rollStrike(); g.roll(3); g.roll(4); rollMany(16, 0); assertEquals(24, g.score()); } public void testPerfectGame() throws Exception { rollMany(12,10); assertEquals(300, g.score()); } }