From 563a7fac0669253332dff900da78e4f46e2a498e Mon Sep 17 00:00:00 2001 From: Emily Bache Date: Mon, 8 Apr 2013 10:39:16 +0200 Subject: [PATCH] --- Tennis/README.md | 2 +- Tennis/java/TennisGame.java | 6 ++ .../TennisGame.java => TennisGame1.java} | 5 +- .../TennisGame.java => TennisGame2.java} | 5 +- .../TennisGame.java => TennisGame3.java} | 5 +- Tennis/java/{defactored1 => }/TennisTest.java | 33 ++++--- Tennis/java/defactored2/TennisTest.java | 97 ------------------- Tennis/java/defactored3/TennisTest.java | 97 ------------------- Tennis/java/factored1/TennisGame.java | 63 ------------ Tennis/java/factored1/TennisTest.java | 96 ------------------ 10 files changed, 30 insertions(+), 379 deletions(-) create mode 100644 Tennis/java/TennisGame.java rename Tennis/java/{defactored1/TennisGame.java => TennisGame1.java} (94%) rename Tennis/java/{defactored2/TennisGame.java => TennisGame2.java} (96%) rename Tennis/java/{defactored3/TennisGame.java => TennisGame3.java} (88%) rename Tennis/java/{defactored1 => }/TennisTest.java (80%) delete mode 100644 Tennis/java/defactored2/TennisTest.java delete mode 100644 Tennis/java/defactored3/TennisTest.java delete mode 100644 Tennis/java/factored1/TennisGame.java delete mode 100644 Tennis/java/factored1/TennisTest.java diff --git a/Tennis/README.md b/Tennis/README.md index 31a18a00..7e3c8db0 100644 --- a/Tennis/README.md +++ b/Tennis/README.md @@ -23,6 +23,6 @@ As an alternative to downloading the code, click one of the links below to creat - [Python](http://cyber-dojo.com/forker/fork/FFEB8EE18C?avatar=cheetah&tag=3) - [Ruby](http://cyber-dojo.com/forker/fork/9197D6B12C?avatar=cheetah&tag=3) -- [Java](http://cyber-dojo.com/forker/fork/B22DCD17C3?avatar=buffalo&tag=11) +- [Java](http://cyber-dojo.com/forker/fork/426FA07B60?avatar=raccoon&tag=3) - [C++](http://cyber-dojo.com/forker/fork/CD6FC41518?avatar=deer&tag=45) - [C#](http://cyber-dojo.com/forker/fork/672E047F5D?avatar=buffalo&tag=8) \ No newline at end of file diff --git a/Tennis/java/TennisGame.java b/Tennis/java/TennisGame.java new file mode 100644 index 00000000..2bc85203 --- /dev/null +++ b/Tennis/java/TennisGame.java @@ -0,0 +1,6 @@ + +public interface TennisGame { + + void wonPoint(String playerName); + String getScore(); +} \ No newline at end of file diff --git a/Tennis/java/defactored1/TennisGame.java b/Tennis/java/TennisGame1.java similarity index 94% rename from Tennis/java/defactored1/TennisGame.java rename to Tennis/java/TennisGame1.java index cadb6fb5..c92d5fab 100644 --- a/Tennis/java/defactored1/TennisGame.java +++ b/Tennis/java/TennisGame1.java @@ -1,13 +1,12 @@ -package defactored1; -public class TennisGame { +public class TennisGame1 implements TennisGame { private int m_score1 = 0; private int m_score2 = 0; private String player1Name; private String player2Name; - public TennisGame(String player1Name, String player2Name) { + public TennisGame1(String player1Name, String player2Name) { this.player1Name = player1Name; this.player2Name = player2Name; } diff --git a/Tennis/java/defactored2/TennisGame.java b/Tennis/java/TennisGame2.java similarity index 96% rename from Tennis/java/defactored2/TennisGame.java rename to Tennis/java/TennisGame2.java index a7b913a4..461d57df 100644 --- a/Tennis/java/defactored2/TennisGame.java +++ b/Tennis/java/TennisGame2.java @@ -1,6 +1,5 @@ -package defactored2; -public class TennisGame +public class TennisGame2 implements TennisGame { public int P1point = 0; public int P2point = 0; @@ -10,7 +9,7 @@ public class TennisGame private String player1Name; private String player2Name; - public TennisGame(String player1Name, String player2Name) { + public TennisGame2(String player1Name, String player2Name) { this.player1Name = player1Name; this.player2Name = player2Name; } diff --git a/Tennis/java/defactored3/TennisGame.java b/Tennis/java/TennisGame3.java similarity index 88% rename from Tennis/java/defactored3/TennisGame.java rename to Tennis/java/TennisGame3.java index 6c121586..f222b460 100644 --- a/Tennis/java/defactored3/TennisGame.java +++ b/Tennis/java/TennisGame3.java @@ -1,13 +1,12 @@ -package defactored3; -public class TennisGame { +public class TennisGame3 implements TennisGame { private int p2; private int p1; private String p1N; private String p2N; - public TennisGame(String p1N, String p2N) { + public TennisGame3(String p1N, String p2N) { this.p1N = p1N; this.p2N = p2N; } diff --git a/Tennis/java/defactored1/TennisTest.java b/Tennis/java/TennisTest.java similarity index 80% rename from Tennis/java/defactored1/TennisTest.java rename to Tennis/java/TennisTest.java index a90fd303..c03990ca 100644 --- a/Tennis/java/defactored1/TennisTest.java +++ b/Tennis/java/TennisTest.java @@ -1,5 +1,3 @@ -package defactored1; - import static org.junit.Assert.*; import java.util.Arrays; @@ -68,9 +66,7 @@ public class TennisTest { }); } - @Test - public void checkAllScores() { - TennisGame game = new TennisGame("player1", "player2"); + public void checkAllScores(TennisGame game) { int highestScore = Math.max(this.player1Score, this.player2Score); for (int i = 0; i < highestScore; i++) { if (i < this.player1Score) @@ -80,18 +76,23 @@ public class TennisTest { } assertEquals(this.expectedScore, game.getScore()); } - + @Test - public void realisticGame() { - TennisGame game = new TennisGame("player1", "player2"); - String[] points = {"player1", "player1", "player2", "player2", "player1", "player1"}; - String[] expected_scores = {"Fifteen-Love", "Thirty-Love", "Thirty-Fifteen", "Thirty-All", "Forty-Thirty", "Win for player1"}; - for (int i = 0; i < 6; i++) { - game.wonPoint(points[i]); - assertEquals(expected_scores[i], game.getScore()); - } - - + public void checkAllScoresTennisGame1() { + TennisGame1 game = new TennisGame1("player1", "player2"); + checkAllScores(game); + } + + @Test + public void checkAllScoresTennisGame2() { + TennisGame2 game = new TennisGame2("player1", "player2"); + checkAllScores(game); + } + + @Test + public void checkAllScoresTennisGame3() { + TennisGame3 game = new TennisGame3("player1", "player2"); + checkAllScores(game); } } diff --git a/Tennis/java/defactored2/TennisTest.java b/Tennis/java/defactored2/TennisTest.java deleted file mode 100644 index bbdd4a5e..00000000 --- a/Tennis/java/defactored2/TennisTest.java +++ /dev/null @@ -1,97 +0,0 @@ -package defactored2; - -import static org.junit.Assert.*; - -import java.util.Arrays; -import java.util.Collection; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; - -@RunWith(Parameterized.class) -public class TennisTest { - - private int player1Score; - private int player2Score; - private String expectedScore; - - public TennisTest(int player1Score, int player2Score, String expectedScore) { - this.player1Score = player1Score; - this.player2Score = player2Score; - this.expectedScore = expectedScore; - } - - @Parameters - public static Collection getAllScores() { - return Arrays.asList(new Object[][] { - { 0, 0, "Love-All" }, - { 1, 1, "Fifteen-All" }, - { 2, 2, "Thirty-All"}, - { 3, 3, "Forty-All"}, - { 4, 4, "Deuce"}, - - { 1, 0, "Fifteen-Love"}, - { 0, 1, "Love-Fifteen"}, - { 2, 0, "Thirty-Love"}, - { 0, 2, "Love-Thirty"}, - { 3, 0, "Forty-Love"}, - { 0, 3, "Love-Forty"}, - { 4, 0, "Win for player1"}, - { 0, 4, "Win for player2"}, - - { 2, 1, "Thirty-Fifteen"}, - { 1, 2, "Fifteen-Thirty"}, - { 3, 1, "Forty-Fifteen"}, - { 1, 3, "Fifteen-Forty"}, - { 4, 1, "Win for player1"}, - { 1, 4, "Win for player2"}, - - { 3, 2, "Forty-Thirty"}, - { 2, 3, "Thirty-Forty"}, - { 4, 2, "Win for player1"}, - { 2, 4, "Win for player2"}, - - { 4, 3, "Advantage player1"}, - { 3, 4, "Advantage player2"}, - { 5, 4, "Advantage player1"}, - { 4, 5, "Advantage player2"}, - { 15, 14, "Advantage player1"}, - { 14, 15, "Advantage player2"}, - - { 6, 4, "Win for player1"}, - { 4, 6, "Win for player2"}, - { 16, 14, "Win for player1"}, - { 14, 16, "Win for player2"}, - }); - } - - @Test - public void checkAllScores() { - TennisGame game = new TennisGame("player1", "player2"); - int highestScore = Math.max(this.player1Score, this.player2Score); - for (int i = 0; i < highestScore; i++) { - if (i < this.player1Score) - game.wonPoint("player1"); - if (i < this.player2Score) - game.wonPoint("player2"); - } - assertEquals(this.expectedScore, game.getScore()); - } - - @Test - public void realisticGame() { - TennisGame game = new TennisGame("player1", "player2"); - String[] points = {"player1", "player1", "player2", "player2", "player1", "player1"}; - String[] expected_scores = {"Fifteen-Love", "Thirty-Love", "Thirty-Fifteen", "Thirty-All", "Forty-Thirty", "Win for player1"}; - for (int i = 0; i < 6; i++) { - game.wonPoint(points[i]); - assertEquals(expected_scores[i], game.getScore()); - } - - - } - -} diff --git a/Tennis/java/defactored3/TennisTest.java b/Tennis/java/defactored3/TennisTest.java deleted file mode 100644 index c6fe5abd..00000000 --- a/Tennis/java/defactored3/TennisTest.java +++ /dev/null @@ -1,97 +0,0 @@ -package defactored3; - -import static org.junit.Assert.*; - -import java.util.Arrays; -import java.util.Collection; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; - -@RunWith(Parameterized.class) -public class TennisTest { - - private int player1Score; - private int player2Score; - private String expectedScore; - - public TennisTest(int player1Score, int player2Score, String expectedScore) { - this.player1Score = player1Score; - this.player2Score = player2Score; - this.expectedScore = expectedScore; - } - - @Parameters - public static Collection getAllScores() { - return Arrays.asList(new Object[][] { - { 0, 0, "Love-All" }, - { 1, 1, "Fifteen-All" }, - { 2, 2, "Thirty-All"}, - { 3, 3, "Forty-All"}, - { 4, 4, "Deuce"}, - - { 1, 0, "Fifteen-Love"}, - { 0, 1, "Love-Fifteen"}, - { 2, 0, "Thirty-Love"}, - { 0, 2, "Love-Thirty"}, - { 3, 0, "Forty-Love"}, - { 0, 3, "Love-Forty"}, - { 4, 0, "Win for player1"}, - { 0, 4, "Win for player2"}, - - { 2, 1, "Thirty-Fifteen"}, - { 1, 2, "Fifteen-Thirty"}, - { 3, 1, "Forty-Fifteen"}, - { 1, 3, "Fifteen-Forty"}, - { 4, 1, "Win for player1"}, - { 1, 4, "Win for player2"}, - - { 3, 2, "Forty-Thirty"}, - { 2, 3, "Thirty-Forty"}, - { 4, 2, "Win for player1"}, - { 2, 4, "Win for player2"}, - - { 4, 3, "Advantage player1"}, - { 3, 4, "Advantage player2"}, - { 5, 4, "Advantage player1"}, - { 4, 5, "Advantage player2"}, - { 15, 14, "Advantage player1"}, - { 14, 15, "Advantage player2"}, - - { 6, 4, "Win for player1"}, - { 4, 6, "Win for player2"}, - { 16, 14, "Win for player1"}, - { 14, 16, "Win for player2"}, - }); - } - - @Test - public void checkAllScores() { - TennisGame game = new TennisGame("player1", "player2"); - int highestScore = Math.max(this.player1Score, this.player2Score); - for (int i = 0; i < highestScore; i++) { - if (i < this.player1Score) - game.wonPoint("player1"); - if (i < this.player2Score) - game.wonPoint("player2"); - } - assertEquals(this.expectedScore, game.getScore()); - } - - @Test - public void realisticGame() { - TennisGame game = new TennisGame("player1", "player2"); - String[] points = {"player1", "player1", "player2", "player2", "player1", "player1"}; - String[] expected_scores = {"Fifteen-Love", "Thirty-Love", "Thirty-Fifteen", "Thirty-All", "Forty-Thirty", "Win for player1"}; - for (int i = 0; i < 6; i++) { - game.wonPoint(points[i]); - assertEquals(expected_scores[i], game.getScore()); - } - - - } - -} diff --git a/Tennis/java/factored1/TennisGame.java b/Tennis/java/factored1/TennisGame.java deleted file mode 100644 index 0ff71289..00000000 --- a/Tennis/java/factored1/TennisGame.java +++ /dev/null @@ -1,63 +0,0 @@ -package factored1; - -public class TennisGame { - - public static final String[] POINTS = new String[]{"Love", "Fifteen", "Thirty", "Forty"}; - - private int player2Score; - private int player1Score; - - private String player1Name; - private String player2Name; - - public TennisGame(String player1Name, String player2Name) { - this.player1Name = player1Name; - this.player2Name = player2Name; - } - - public String getScore() { - if (someoneHasWon()) - return "Win for " + winningPlayerName(); - - if (isEndgame()) { - if (pointsAreEven()) - return "Deuce"; - else - return "Advantage " + winningPlayerName(); - } else { - if (pointsAreEven()) - return POINTS[player1Score] + "-All"; - else { - return POINTS[player1Score] + "-" + POINTS[player2Score]; - } - } - } - - public boolean someoneHasWon() { - return isEndgame() && (player1Score-player2Score >= 2 || player2Score-player1Score >= 2); - } - - private boolean pointsAreEven() { - return player1Score == player2Score; - } - - private boolean isEndgame() { - return player1Score > 3 || player2Score > 3; - } - - public String winningPlayerName() { - if (player1Score > player2Score) - return player1Name; - else - return player2Name; - } - - public void wonPoint(String playerName) { - if (playerName == player1Name) - this.player1Score += 1; - else - this.player2Score += 1; - - } - -} diff --git a/Tennis/java/factored1/TennisTest.java b/Tennis/java/factored1/TennisTest.java deleted file mode 100644 index 2300a761..00000000 --- a/Tennis/java/factored1/TennisTest.java +++ /dev/null @@ -1,96 +0,0 @@ -package factored1; - -import static org.junit.Assert.*; - -import java.util.Arrays; -import java.util.Collection; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; - -@RunWith(Parameterized.class) -public class TennisTest { - - private int player1Score; - private int player2Score; - private String expectedScore; - - public TennisTest(int player1Score, int player2Score, String expectedScore) { - this.player1Score = player1Score; - this.player2Score = player2Score; - this.expectedScore = expectedScore; - } - - @Parameters - public static Collection getAllScores() { - return Arrays.asList(new Object[][] { - { 0, 0, "Love-All" }, - { 1, 1, "Fifteen-All" }, - { 2, 2, "Thirty-All"}, - { 3, 3, "Forty-All"}, - { 4, 4, "Deuce"}, - - { 1, 0, "Fifteen-Love"}, - { 0, 1, "Love-Fifteen"}, - { 2, 0, "Thirty-Love"}, - { 0, 2, "Love-Thirty"}, - { 3, 0, "Forty-Love"}, - { 0, 3, "Love-Forty"}, - { 4, 0, "Win for player1"}, - { 0, 4, "Win for player2"}, - - { 2, 1, "Thirty-Fifteen"}, - { 1, 2, "Fifteen-Thirty"}, - { 3, 1, "Forty-Fifteen"}, - { 1, 3, "Fifteen-Forty"}, - { 4, 1, "Win for player1"}, - { 1, 4, "Win for player2"}, - - { 3, 2, "Forty-Thirty"}, - { 2, 3, "Thirty-Forty"}, - { 4, 2, "Win for player1"}, - { 2, 4, "Win for player2"}, - - { 4, 3, "Advantage player1"}, - { 3, 4, "Advantage player2"}, - { 5, 4, "Advantage player1"}, - { 4, 5, "Advantage player2"}, - { 15, 14, "Advantage player1"}, - { 14, 15, "Advantage player2"}, - - { 6, 4, "Win for player1"}, - { 4, 6, "Win for player2"}, - { 16, 14, "Win for player1"}, - { 14, 16, "Win for player2"}, - }); - } - - @Test - public void checkAllScores() { - TennisGame game = new TennisGame("player1", "player2"); - int highestScore = Math.max(this.player1Score, this.player2Score); - for (int i = 0; i < highestScore; i++) { - if (i < this.player1Score) - game.wonPoint("player1"); - if (i < this.player2Score) - game.wonPoint("player2"); - } - assertEquals(this.expectedScore, game.getScore()); - } - - @Test - public void realisticGame() { - TennisGame game = new TennisGame("player1", "player2"); - String[] points = {"player1", "player1", "player2", "player2", "player1", "player1"}; - String[] expected_scores = {"Fifteen-Love", "Thirty-Love", "Thirty-Fifteen", "Thirty-All", "Forty-Thirty", "Win for player1"}; - for (int i = 0; i < 6; i++) { - game.wonPoint(points[i]); - assertEquals(expected_scores[i], game.getScore()); - } - - - } - -}