• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Bowling league: first story (code)

 
Sheriff
Posts: 13696
227
Android Mac IntelliJ IDE Eclipse IDE Spring Debian Ubuntu Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Task: Score a Single Bowling Game
Version: 0.0.x
Discussion for the code being progressed in this thread is here
[This message has been edited by JUNILU LACAR (edited March 25, 2001).]
 
Junilu Lacar
Sheriff
Posts: 13696
227
Android Mac IntelliJ IDE Eclipse IDE Spring Debian Ubuntu Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Task: Score a Single Bowling Game
Version: 0.0.0000



Discussion for the code being progressed in this thread is here

[This message has been edited by JUNILU LACAR (edited March 25, 2001).]
 
Junilu Lacar
Sheriff
Posts: 13696
227
Android Mac IntelliJ IDE Eclipse IDE Spring Debian Ubuntu Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Task: Score a Single Bowling Game
Version: 0.0.0001
See: "First TestCase" in the discussion thread




[This message has been edited by JUNILU LACAR (edited March 25, 2001).]
 
Junilu Lacar
Sheriff
Posts: 13696
227
Android Mac IntelliJ IDE Eclipse IDE Spring Debian Ubuntu Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Task: Score a Single Bowling Game
Version: 0.0.0002
See: "Second and Third testcase" in the discussion thread

<pre>
//TestGame.java
import junit.framework.*;
public class TestGame extends TestCase {
public TestGame(String name) {
super(name);
}
private Game g;
public void setUp() {
g = new Game();
}
public void testTwoThrowsNoMark() {
g.add(5);
g.add(4);
assertEquals(9, g.getScore());
}
public void testFourThrowsNoMark() {
g.add(5);
g.add(4);
g.add(7);
g.add(2);
assertEquals(18, g.getScore());
}
}
</pre>
<pre>
//Game.java
public class Game {
public void add(int pinFall) {
_score += pinFall;
}
private int _score;
public int getScore() {
return _score;
}
}
</pre>

[This message has been edited by JUNILU LACAR (edited March 28, 2001).]
 
Junilu Lacar
Sheriff
Posts: 13696
227
Android Mac IntelliJ IDE Eclipse IDE Spring Debian Ubuntu Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Task: Score a Single Bowling Game
Version: 0.0.0003
See: "Second and Third testcase" in the discussion thread

<pre>
//TestGame.java
import junit.framework.*;
public class TestGame extends TestCase {
public TestGame(String name) {
super(name);
}
private Game g;
public void setUp() {
g = new Game();
}
public void testTwoThrowsNoMark() {
g.add(5);
g.add(4);
assertEquals(9, g.getScore());
}
public void testFourThrowsNoMark() {
g.add(5);
g.add(4);
g.add(7);
g.add(2);
assertEquals(18, g.getScore());
}
public void testTwoStrikes() {
g.add(10);
g.add(10);
assertEquals(30, g.getScore());
}

}
</pre>
<pre>
//Game.java
public class Game {
public void add(int pinFall) {
_score += pinFall;
}
private int _score;
public int getScore() {
return _score;
}
}
</pre>

[This message has been edited by JUNILU LACAR (edited March 28, 2001).]
 
Junilu Lacar
Sheriff
Posts: 13696
227
Android Mac IntelliJ IDE Eclipse IDE Spring Debian Ubuntu Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

TestFrame.java (version 0.0.0001)
See "Enter the Frame" in discussion
The labeling convention I've used so far seems a bit unwieldy so I'm going to try to simplify a bit starting with this post


[This message has been edited by JUNILU LACAR (edited March 28, 2001).]
 
Junilu Lacar
Sheriff
Posts: 13696
227
Android Mac IntelliJ IDE Eclipse IDE Spring Debian Ubuntu Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Frame.java (version 0.0.0000)
See "Enter the Frame" in discussion
<pre>
//Frame.java
public class Frame {
public void add(int pinFall) {}
public int getPinFalls() {
return 0;
}
}
</pre>
Another change in convention: I think I'll start skeleton classes with version 0.0.0000 while classes that actually have some meat from inception start from 0.0.0001. In the real world, you probably want to stay consistent, especially in a team environment. Since I'm still talking to myself here, I'll just make things up as I go (at least for now)

[This message has been edited by JUNILU LACAR (edited March 28, 2001).]
 
Junilu Lacar
Sheriff
Posts: 13696
227
Android Mac IntelliJ IDE Eclipse IDE Spring Debian Ubuntu Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Frame.java (version 0.0.0001)
See "Enter the Frame" in discussion
<pre>
//Frame.java
public class Frame {
public void add(int pinFall) {

if (ballCount < 2)
pinFalls[ballCount++] = pinFall;

}

private int ballCount = 0;
private int[] pinFalls = new int[2];

public int getPinFalls() {

return pinFalls[0] + pinFalls[1];

}
}
</pre>
New code is shown in blue

[This message has been edited by JUNILU LACAR (edited March 28, 2001).]
 
Junilu Lacar
Sheriff
Posts: 13696
227
Android Mac IntelliJ IDE Eclipse IDE Spring Debian Ubuntu Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Game.java (version: 0.0.0004)
See "Back to the Game" in the discussion thread
<pre>
//Game.java
public class Game {
public void add(int pinFall) {
_currentFrame.add(pinFall);
}
private int _score;
private Frame[] _frames = new Frame[10];
private Frame _currentFrame;
public Game() {
_currentFrame = _frames[0];
}

public int getScore() {
return _score;
}
}
</pre>
JUnit results for TestGame: 3 Errors, 0 Failures


<pre>
//Game.java
public class Game {
public void add(int pinFall) {
_currentFrame.add(pinFall);
}
private int _score;
private Frame[] _frames = new Frame[10];
private Frame _currentFrame;
public Game() {
for (int i = 0; i < 10; i++) {
_frames[i] = new Frame();
}

_currentFrame = _frames[0];
}
public int getScore() {
return _score;
}
}
</pre>JUnit results for TestGame: 0 Errors, 3 Failures


<pre>
//Game.java
public class Game {
public void add(int pinFall) {
_currentFrame.add(pinFall);
}
private int _score;
private Frame[] _frames = new Frame[10];
private Frame _currentFrame;
public Game() {
for (int i = 0; i < 10; i++) {
_frames[i] = new Frame();
}
_currentFrame = _frames[0];
}
public int getScore() {
_score = 0;
for (int i = 0; i < 10; i++) {
_score += _frames[i].getPinFalls();
}

return _score;
}
}
</pre>JUnit results for TestGame: 0 Errors, 2 Failures


<pre>
//Game.java
public class Game {
public void add(int pinFall) {
_currentFrame.add(pinFall);
advanceCurrentFrame();
}
private int _score;
private Frame[] _frames = new Frame[10];
private Frame _currentFrame;
public Game() {
for (int i = 0; i < 10; i++) {
_frames[i] = new Frame();
}
_currentFrame = _frames[0];
}
public int getScore() {
int _score = 0;
for (int i = 0; i < 10; i++) {
_score += _frames[i].getPinFalls();
}
return _score;
}
private void advanceCurrentFrame() {
if (_currentFrame.isDone()) {
int i;
for (i = 0; _frames[i] != _currentFrame; i++)
;
if (++i < 10)
_currentFrame = _frames[i];
}
}

}
</pre>JUnit results for TestGame: 0 Errors, 1 Failure



[This message has been edited by JUNILU LACAR (edited March 28, 2001).]
 
Junilu Lacar
Sheriff
Posts: 13696
227
Android Mac IntelliJ IDE Eclipse IDE Spring Debian Ubuntu Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
TestFrame.java (version 0.0.0002)
See "Deja vu all over again" in discussion

<pre>
//TestFrame.java
import junit.framework.*;
public class TestFrame extends TestCase{
public TestFrame(String name) {
super(name);
}
private Frame f;
public void setUp() {
f = new Frame();
}
public void testFirstBallNoMark() {
f.add(5);
assertEquals(5, f.getPinFalls());
}

public void testSecondBallNoMark() {
f.add(5);
f.add(4);
assertEquals(9, f.getPinFalls());
}
public void testIsDoneNoMark() {
f.add(5);
assert(!f.isDone());
f.add(4);
assert(f.isDone());
}

}
</pre>

[This message has been edited by JUNILU LACAR (edited March 28, 2001).]
 
Junilu Lacar
Sheriff
Posts: 13696
227
Android Mac IntelliJ IDE Eclipse IDE Spring Debian Ubuntu Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frame.java (version 0.0.0002)
See "Deja vu all over again" in discussion
<pre>
//Frame.java
public class Frame {
public void add(int pinFall) {
if (ballCount < 2)
pinFalls[ballCount++] = pinFall;
}
private int ballCount = 0;
private int[] pinFalls = new int[2];
public int getPinFalls() {
return pinFalls[0] + pinFalls[1];
}

public boolean isDone() {
return ballCount == 2;
}

}
</pre>

[This message has been edited by JUNILU LACAR (edited March 28, 2001).]
 
Paper jam tastes about as you would expect. Try some on this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!