From b200561e73bdfcab70d39f1fe19ef69edde6d8ad Mon Sep 17 00:00:00 2001 From: chiarazarrella Date: Thu, 10 Apr 2025 10:30:10 +0200 Subject: [PATCH 1/2] solution to wizards-and-warriors-2-analyzer --- src/main/java/analyzer/AnalyzerRoot.java | 4 +- .../UseMethodOverloading.java | 20 +++++ .../WizardsAndWarriors2Analyzer.java | 85 +++++++++++++++++++ .../analyzer/AnalyzerIntegrationTest.java | 16 ++++ ...andwarriors2.ExemplarSolution.approved.txt | 11 +++ ...iors2.NotUseMethodOverloading.approved.txt | 14 +++ ...PartialUseOfMethodOverloading.approved.txt | 14 +++ ...sandwarriors2.UseStringFormat.approved.txt | 14 +++ ...matAndNotUseMethodOverloading.approved.txt | 19 +++++ .../ExemplarSolution.java | 25 ++++++ .../NotUseMethodOverloading.java | 29 +++++++ .../PartialUseOfMethodOverloading.java | 25 ++++++ .../UseStringFormat.java | 28 ++++++ ...tringFormatAndNotUseMethodOverloading.java | 35 ++++++++ .../exemplar-solution/.meta/config.json | 32 +++++++ .../exemplar-solution/expected_analysis.json | 11 +++ .../exemplar-solution/expected_tags.json | 3 + .../src/main/java/GameMaster.java | 25 ++++++ .../.meta/config.json | 32 +++++++ .../expected_analysis.json | 14 +++ .../expected_tags.json | 3 + .../src/main/java/GameMaster.java | 29 +++++++ .../use-string-format/.meta/config.json | 32 +++++++ .../use-string-format/expected_analysis.json | 14 +++ .../use-string-format/expected_tags.json | 3 + .../src/main/java/GameMaster.java | 28 ++++++ 26 files changed, 563 insertions(+), 2 deletions(-) create mode 100644 src/main/java/analyzer/exercises/wizardsandwarriors2/UseMethodOverloading.java create mode 100644 src/main/java/analyzer/exercises/wizardsandwarriors2/WizardsAndWarriors2Analyzer.java create mode 100644 src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.ExemplarSolution.approved.txt create mode 100644 src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.NotUseMethodOverloading.approved.txt create mode 100644 src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.PartialUseOfMethodOverloading.approved.txt create mode 100644 src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.UseStringFormat.approved.txt create mode 100644 src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.UseStringFormatAndNotUseMethodOverloading.approved.txt create mode 100644 src/test/resources/scenarios/wizards-and-warriors-2/ExemplarSolution.java create mode 100644 src/test/resources/scenarios/wizards-and-warriors-2/NotUseMethodOverloading.java create mode 100644 src/test/resources/scenarios/wizards-and-warriors-2/PartialUseOfMethodOverloading.java create mode 100644 src/test/resources/scenarios/wizards-and-warriors-2/UseStringFormat.java create mode 100644 src/test/resources/scenarios/wizards-and-warriors-2/UseStringFormatAndNotUseMethodOverloading.java create mode 100644 tests/wizards-and-warriors-2/exemplar-solution/.meta/config.json create mode 100644 tests/wizards-and-warriors-2/exemplar-solution/expected_analysis.json create mode 100644 tests/wizards-and-warriors-2/exemplar-solution/expected_tags.json create mode 100644 tests/wizards-and-warriors-2/exemplar-solution/src/main/java/GameMaster.java create mode 100644 tests/wizards-and-warriors-2/not-use-method-overloading/.meta/config.json create mode 100644 tests/wizards-and-warriors-2/not-use-method-overloading/expected_analysis.json create mode 100644 tests/wizards-and-warriors-2/not-use-method-overloading/expected_tags.json create mode 100644 tests/wizards-and-warriors-2/not-use-method-overloading/src/main/java/GameMaster.java create mode 100644 tests/wizards-and-warriors-2/use-string-format/.meta/config.json create mode 100644 tests/wizards-and-warriors-2/use-string-format/expected_analysis.json create mode 100644 tests/wizards-and-warriors-2/use-string-format/expected_tags.json create mode 100644 tests/wizards-and-warriors-2/use-string-format/src/main/java/GameMaster.java diff --git a/src/main/java/analyzer/AnalyzerRoot.java b/src/main/java/analyzer/AnalyzerRoot.java index 6e249ec3..b7cb7a2f 100644 --- a/src/main/java/analyzer/AnalyzerRoot.java +++ b/src/main/java/analyzer/AnalyzerRoot.java @@ -12,6 +12,7 @@ import analyzer.exercises.secrets.SecretsAnalyzer; import analyzer.exercises.twofer.TwoferAnalyzer; import analyzer.exercises.wizardsandwarriors.WizardsAndWarriorsAnalyzer; +import analyzer.exercises.wizardsandwarriors2.WizardsAndWarriors2Analyzer; import java.util.ArrayList; import java.util.List; @@ -33,7 +34,6 @@ private AnalyzerRoot() { */ public static Output analyze(Solution solution) { var outputBuilder = new OutputBuilder(); - for (Analyzer analyzer : createAnalyzers(solution.getSlug())) { analyzer.analyze(solution, outputBuilder); } @@ -49,7 +49,6 @@ private static List createAnalyzers(String slug) { var analyzers = new ArrayList(); analyzers.add(new GlobalAnalyzer()); - switch (slug) { case "annalyns-infiltration" -> analyzers.add(new AnnalynsInfiltrationAnalyzer()); case "hamming" -> analyzers.add(new HammingAnalyzer()); @@ -61,6 +60,7 @@ private static List createAnalyzers(String slug) { case "secrets" -> analyzers.add(new SecretsAnalyzer()); case "two-fer" -> analyzers.add(new TwoferAnalyzer()); case "wizards-and-warriors" -> analyzers.add(new WizardsAndWarriorsAnalyzer()); + case "wizards-and-warriors-2" -> analyzers.add(new WizardsAndWarriors2Analyzer()); } return List.copyOf(analyzers); diff --git a/src/main/java/analyzer/exercises/wizardsandwarriors2/UseMethodOverloading.java b/src/main/java/analyzer/exercises/wizardsandwarriors2/UseMethodOverloading.java new file mode 100644 index 00000000..7d9aefa1 --- /dev/null +++ b/src/main/java/analyzer/exercises/wizardsandwarriors2/UseMethodOverloading.java @@ -0,0 +1,20 @@ +package analyzer.exercises.wizardsandwarriors2; + +import analyzer.Comment; + +/** + * @author: chiarazarrella + */ +public class UseMethodOverloading extends Comment{ + + @Override + public String getKey() { + return "java.wizards-and-warriors-2.use_method_overloading"; + } + + @Override + public Type getType() { + return Type.ACTIONABLE; + } +} + diff --git a/src/main/java/analyzer/exercises/wizardsandwarriors2/WizardsAndWarriors2Analyzer.java b/src/main/java/analyzer/exercises/wizardsandwarriors2/WizardsAndWarriors2Analyzer.java new file mode 100644 index 00000000..8eaed9e2 --- /dev/null +++ b/src/main/java/analyzer/exercises/wizardsandwarriors2/WizardsAndWarriors2Analyzer.java @@ -0,0 +1,85 @@ +package analyzer.exercises.wizardsandwarriors2; + +import analyzer.Analyzer; +import analyzer.OutputCollector; +import analyzer.Solution; +import analyzer.comments.ExemplarSolution; +import analyzer.comments.PreferStringConcatenation; +import com.github.javaparser.ast.body.MethodDeclaration; +import com.github.javaparser.ast.expr.MethodCallExpr; +import com.github.javaparser.ast.visitor.VoidVisitorAdapter; + +import java.util.List; + +/** + * The {@link WizardsAndWarriors2Analyzer} is the analyzer implementation for the {@code wizards-and-warriors-2} practice exercise. + * + * @see The wizards-and-warriors exercise on the Java track + */ +public class WizardsAndWarriors2Analyzer extends VoidVisitorAdapter implements Analyzer { + + private static final String EXERCISE_NAME = "Wizards and Warriors 2"; + private static final String GAME_MASTER = "GameMaster"; + private static final String DESCRIBE = "describe"; + private static final String FORMAT = "format"; + + @Override + public void analyze(Solution solution, OutputCollector output) { + + for (var compilationUnit : solution.getCompilationUnits()) { + compilationUnit.getClassByName(GAME_MASTER).ifPresent(c -> c.accept(this, output)); + } + + if (output.getComments().isEmpty()) { + output.addComment(new ExemplarSolution(EXERCISE_NAME)); + } + } + + @Override + public void visit(MethodDeclaration node, OutputCollector output) { + + if(!node.getNameAsString().equals(DESCRIBE)) { + return; + } + + if(node.getParameters().size() > 1 && !useOverload(node)) { + + output.addComment(new UseMethodOverloading()); + + } + + if(useStringFormat(node)) { + + output.addComment(new PreferStringConcatenation()); + + } + + + super.visit(node, output); + } + + private static boolean useOverload(MethodDeclaration node) { + + int paramCount = node.getParameters().size(); + + List describeCalls = node.findAll(MethodCallExpr.class).stream() + .filter(m -> m.getNameAsString().equals(DESCRIBE)) + .toList(); + + if (paramCount == 2) { + return describeCalls.size() == 1 || describeCalls.size() == 3; + } + + if (paramCount == 3) { + return describeCalls.size() == 3; + } + + return false; + } + + private static boolean useStringFormat(MethodDeclaration node) { + return node.findAll(MethodCallExpr.class).stream() + .anyMatch(m -> m.getNameAsString().contains(FORMAT)); + } + +} diff --git a/src/test/java/analyzer/AnalyzerIntegrationTest.java b/src/test/java/analyzer/AnalyzerIntegrationTest.java index 26315c72..8b732f84 100644 --- a/src/test/java/analyzer/AnalyzerIntegrationTest.java +++ b/src/test/java/analyzer/AnalyzerIntegrationTest.java @@ -205,4 +205,20 @@ void salarycalculator(String scenario) throws IOException { Approvals.verify(serialize(output.analysis()), Approvals.NAMES.withParameters(scenario)); } + + @ParameterizedTest + @ValueSource(strings = { + "ExemplarSolution", + "NotUseMethodOverloading", + "PartialUseOfMethodOverloading", + "UseStringFormat", + "UseStringFormatAndNotUseMethodOverloading", + }) + void wizardsandwarriors2(String scenario) throws IOException { + var path = Path.of("wizards-and-warriors-2", scenario + ".java"); + var solution = new SolutionFromFiles("wizards-and-warriors-2", SCENARIOS.resolve(path)); + var output = AnalyzerRoot.analyze(solution); + + Approvals.verify(serialize(output.analysis()), Approvals.NAMES.withParameters(scenario)); + } } diff --git a/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.ExemplarSolution.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.ExemplarSolution.approved.txt new file mode 100644 index 00000000..a9ee72ff --- /dev/null +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.ExemplarSolution.approved.txt @@ -0,0 +1,11 @@ +{ + "comments": [ + { + "comment": "java.general.exemplar", + "params": { + "exerciseName": "Wizards and Warriors 2" + }, + "type": "celebratory" + } + ] +} \ No newline at end of file diff --git a/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.NotUseMethodOverloading.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.NotUseMethodOverloading.approved.txt new file mode 100644 index 00000000..96757c7d --- /dev/null +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.NotUseMethodOverloading.approved.txt @@ -0,0 +1,14 @@ +{ + "comments": [ + { + "comment": "java.wizards-and-warriors-2.use_method_overloading", + "params": {}, + "type": "actionable" + }, + { + "comment": "java.general.feedback_request", + "params": {}, + "type": "informative" + } + ] +} \ No newline at end of file diff --git a/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.PartialUseOfMethodOverloading.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.PartialUseOfMethodOverloading.approved.txt new file mode 100644 index 00000000..96757c7d --- /dev/null +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.PartialUseOfMethodOverloading.approved.txt @@ -0,0 +1,14 @@ +{ + "comments": [ + { + "comment": "java.wizards-and-warriors-2.use_method_overloading", + "params": {}, + "type": "actionable" + }, + { + "comment": "java.general.feedback_request", + "params": {}, + "type": "informative" + } + ] +} \ No newline at end of file diff --git a/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.UseStringFormat.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.UseStringFormat.approved.txt new file mode 100644 index 00000000..b01ad7f7 --- /dev/null +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.UseStringFormat.approved.txt @@ -0,0 +1,14 @@ +{ + "comments": [ + { + "comment": "java.general.prefer_string_concatenation", + "params": {}, + "type": "informative" + }, + { + "comment": "java.general.feedback_request", + "params": {}, + "type": "informative" + } + ] +} \ No newline at end of file diff --git a/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.UseStringFormatAndNotUseMethodOverloading.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.UseStringFormatAndNotUseMethodOverloading.approved.txt new file mode 100644 index 00000000..25d682cc --- /dev/null +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.UseStringFormatAndNotUseMethodOverloading.approved.txt @@ -0,0 +1,19 @@ +{ + "comments": [ + { + "comment": "java.wizards-and-warriors-2.use_method_overloading", + "params": {}, + "type": "actionable" + }, + { + "comment": "java.general.prefer_string_concatenation", + "params": {}, + "type": "informative" + }, + { + "comment": "java.general.feedback_request", + "params": {}, + "type": "informative" + } + ] +} \ No newline at end of file diff --git a/src/test/resources/scenarios/wizards-and-warriors-2/ExemplarSolution.java b/src/test/resources/scenarios/wizards-and-warriors-2/ExemplarSolution.java new file mode 100644 index 00000000..e5b69038 --- /dev/null +++ b/src/test/resources/scenarios/wizards-and-warriors-2/ExemplarSolution.java @@ -0,0 +1,25 @@ +public class GameMaster { + + public String describe(Character character) { + return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points."; + } + + public String describe(Destination destination) { + return "You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants."; + } + + public String describe(TravelMethod travelMethod) { + if (travelMethod == TravelMethod.WALKING) { + return "You're traveling to your destination by walking."; + } + return "You're traveling to your destination on horseback."; + } + + public String describe(Character character, Destination destination, TravelMethod travelMethod) { + return describe(character) + " " + describe(travelMethod) + " " + describe(destination); + } + + public String describe(Character character, Destination destination) { + return describe(character, destination, TravelMethod.WALKING); + } + } \ No newline at end of file diff --git a/src/test/resources/scenarios/wizards-and-warriors-2/NotUseMethodOverloading.java b/src/test/resources/scenarios/wizards-and-warriors-2/NotUseMethodOverloading.java new file mode 100644 index 00000000..97c00b5b --- /dev/null +++ b/src/test/resources/scenarios/wizards-and-warriors-2/NotUseMethodOverloading.java @@ -0,0 +1,29 @@ +public class GameMaster { + + public String describe(Character character) { + return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points."; + } + + public String describe(Destination destination) { + return "You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants."; + } + + public String describe(TravelMethod travelMethod) { + if (travelMethod == TravelMethod.WALKING) { + return "You're traveling to your destination by walking."; + } + return "You're traveling to your destination on horseback."; + } + + public String describe(Character character, Destination destination, TravelMethod travelMethod) { + return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points. " + + (travelMethod == TravelMethod.WALKING ? "You're traveling to your destination by walking. " : "You're traveling to your destination on horseback. ") + + "You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants."; + } + + public String describe(Character character, Destination destination) { + return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points. " + + "You're traveling to your destination by walking. " + + "You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants."; + } +} \ No newline at end of file diff --git a/src/test/resources/scenarios/wizards-and-warriors-2/PartialUseOfMethodOverloading.java b/src/test/resources/scenarios/wizards-and-warriors-2/PartialUseOfMethodOverloading.java new file mode 100644 index 00000000..6e9efe84 --- /dev/null +++ b/src/test/resources/scenarios/wizards-and-warriors-2/PartialUseOfMethodOverloading.java @@ -0,0 +1,25 @@ +public class GameMaster { + + public String describe(Character character) { + return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points."; + } + + public String describe(Destination destination) { + return "You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants."; + } + + public String describe(TravelMethod travelMethod) { + if (travelMethod == TravelMethod.WALKING) { + return "You're traveling to your destination by walking."; + } + return "You're traveling to your destination on horseback."; + } + + public String describe(Character character, Destination destination, TravelMethod travelMethod) { + return describe(character) + " " + describe(travelMethod) + " " + describe(destination); + } + + public String describe(Character character, Destination destination) { + return describe(character) + " " + describe(destination) + " You're traveling to your destination by walking."; + } +} \ No newline at end of file diff --git a/src/test/resources/scenarios/wizards-and-warriors-2/UseStringFormat.java b/src/test/resources/scenarios/wizards-and-warriors-2/UseStringFormat.java new file mode 100644 index 00000000..e32fd7d4 --- /dev/null +++ b/src/test/resources/scenarios/wizards-and-warriors-2/UseStringFormat.java @@ -0,0 +1,28 @@ +public class GameMaster { + + public String describe(Character character) { + return "You're a level %d %s with %d hit points.".formatted(character.getLevel(), + character.getCharacterClass(), character.getHitPoints()); + } + + public String describe(Destination destination) { + return "You've arrived at %s, which has %d inhabitants.".formatted(destination.getName(), + destination.getInhabitants()); + } + + public String describe(TravelMethod travelMethod) { + if (travelMethod == TravelMethod.WALKING) { + return "You're traveling to your destination by walking."; + } + return "You're traveling to your destination on horseback."; + + } + + public String describe(Character character, Destination destination, TravelMethod travelMethod) { + return "%s %s %s".formatted(describe(character), describe(travelMethod), describe(destination)); + } + + public String describe(Character character, Destination destination) { + return describe(character, destination, TravelMethod.WALKING); + } +} diff --git a/src/test/resources/scenarios/wizards-and-warriors-2/UseStringFormatAndNotUseMethodOverloading.java b/src/test/resources/scenarios/wizards-and-warriors-2/UseStringFormatAndNotUseMethodOverloading.java new file mode 100644 index 00000000..f3fc84e3 --- /dev/null +++ b/src/test/resources/scenarios/wizards-and-warriors-2/UseStringFormatAndNotUseMethodOverloading.java @@ -0,0 +1,35 @@ +public class GameMaster { + + public String describe(Character character) { + return "You're a level %d %s with %d hit points.".formatted(character.getLevel(), + character.getCharacterClass(), character.getHitPoints()); + } + + public String describe(Destination destination) { + return "You've arrived at %s, which has %d inhabitants.".formatted(destination.getName(), + destination.getInhabitants()); + } + + public String describe(TravelMethod travelMethod) { + if (travelMethod == TravelMethod.WALKING) { + return "You're traveling to your destination by walking."; + } + return "You're traveling to your destination on horseback."; + + } + + public String describe(Character character, Destination destination, TravelMethod travelMethod) { + return "You're a level %d %s with %d hit points. ".formatted(character.getLevel(), + character.getCharacterClass(), character.getHitPoints()) + + (travelMethod == TravelMethod.WALKING ? "You're traveling to your destination by walking. " + : "You're traveling to your destination on horseback. ") + + "You've arrived at %s, which has %d inhabitants.".formatted(destination.getName(), + destination.getInhabitants()); + } + + public String describe(Character character, Destination destination) { + return "You're a level %d %s with %d hit points. You're traveling to your destination by walking. You've arrived at %s, which has %d inhabitants." + .formatted(character.getLevel(), character.getCharacterClass(), character.getHitPoints(), + destination.getName(), destination.getInhabitants()); + } +} diff --git a/tests/wizards-and-warriors-2/exemplar-solution/.meta/config.json b/tests/wizards-and-warriors-2/exemplar-solution/.meta/config.json new file mode 100644 index 00000000..decfa680 --- /dev/null +++ b/tests/wizards-and-warriors-2/exemplar-solution/.meta/config.json @@ -0,0 +1,32 @@ +{ + "authors": [ + "sougat818" + ], + "contributors": [ + "jagdish-15", + "sanderploegsma" + ], + "files": { + "solution": [ + "src/main/java/GameMaster.java" + ], + "test": [ + "src/test/java/GameMasterTest.java" + ], + "exemplar": [ + ".meta/src/reference/java/GameMaster.java" + ], + "editor": [ + "src/main/java/Character.java", + "src/main/java/Destination.java", + "src/main/java/TravelMethod.java" + ], + "invalidator": [ + "build.gradle" + ] + }, + "forked_from": [ + "csharp/wizards-and-warriors-2" + ], + "blurb": "Learn about method overloading by extending your favorite RPG." +} \ No newline at end of file diff --git a/tests/wizards-and-warriors-2/exemplar-solution/expected_analysis.json b/tests/wizards-and-warriors-2/exemplar-solution/expected_analysis.json new file mode 100644 index 00000000..a9b48769 --- /dev/null +++ b/tests/wizards-and-warriors-2/exemplar-solution/expected_analysis.json @@ -0,0 +1,11 @@ +{ + "comments": [ + { + "comment": "java.general.exemplar", + "params": { + "exerciseName": "Wizards and Warriors 2" + }, + "type": "celebratory" + } + ] +} \ No newline at end of file diff --git a/tests/wizards-and-warriors-2/exemplar-solution/expected_tags.json b/tests/wizards-and-warriors-2/exemplar-solution/expected_tags.json new file mode 100644 index 00000000..eb25b190 --- /dev/null +++ b/tests/wizards-and-warriors-2/exemplar-solution/expected_tags.json @@ -0,0 +1,3 @@ +{ + "tags": [] +} \ No newline at end of file diff --git a/tests/wizards-and-warriors-2/exemplar-solution/src/main/java/GameMaster.java b/tests/wizards-and-warriors-2/exemplar-solution/src/main/java/GameMaster.java new file mode 100644 index 00000000..8a513f51 --- /dev/null +++ b/tests/wizards-and-warriors-2/exemplar-solution/src/main/java/GameMaster.java @@ -0,0 +1,25 @@ +public class GameMaster { + + public String describe(Character character) { + return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points."; + } + + public String describe(Destination destination) { + return "You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants."; + } + + public String describe(TravelMethod travelMethod) { + if (travelMethod == TravelMethod.WALKING) { + return "You're traveling to your destination by walking."; + } + return "You're traveling to your destination on horseback."; + } + + public String describe(Character character, Destination destination, TravelMethod travelMethod) { + return describe(character) + " " + describe(travelMethod) + " " + describe(destination); + } + + public String describe(Character character, Destination destination) { + return describe(character, destination, TravelMethod.WALKING); + } +} \ No newline at end of file diff --git a/tests/wizards-and-warriors-2/not-use-method-overloading/.meta/config.json b/tests/wizards-and-warriors-2/not-use-method-overloading/.meta/config.json new file mode 100644 index 00000000..decfa680 --- /dev/null +++ b/tests/wizards-and-warriors-2/not-use-method-overloading/.meta/config.json @@ -0,0 +1,32 @@ +{ + "authors": [ + "sougat818" + ], + "contributors": [ + "jagdish-15", + "sanderploegsma" + ], + "files": { + "solution": [ + "src/main/java/GameMaster.java" + ], + "test": [ + "src/test/java/GameMasterTest.java" + ], + "exemplar": [ + ".meta/src/reference/java/GameMaster.java" + ], + "editor": [ + "src/main/java/Character.java", + "src/main/java/Destination.java", + "src/main/java/TravelMethod.java" + ], + "invalidator": [ + "build.gradle" + ] + }, + "forked_from": [ + "csharp/wizards-and-warriors-2" + ], + "blurb": "Learn about method overloading by extending your favorite RPG." +} \ No newline at end of file diff --git a/tests/wizards-and-warriors-2/not-use-method-overloading/expected_analysis.json b/tests/wizards-and-warriors-2/not-use-method-overloading/expected_analysis.json new file mode 100644 index 00000000..a2e52b42 --- /dev/null +++ b/tests/wizards-and-warriors-2/not-use-method-overloading/expected_analysis.json @@ -0,0 +1,14 @@ +{ + "comments": [ + { + "comment": "java.wizards-and-warriors-2.use_method_overloading", + "params": {}, + "type": "actionable" + }, + { + "comment": "java.general.feedback_request", + "params": {}, + "type": "informative" + } + ] +} \ No newline at end of file diff --git a/tests/wizards-and-warriors-2/not-use-method-overloading/expected_tags.json b/tests/wizards-and-warriors-2/not-use-method-overloading/expected_tags.json new file mode 100644 index 00000000..eb25b190 --- /dev/null +++ b/tests/wizards-and-warriors-2/not-use-method-overloading/expected_tags.json @@ -0,0 +1,3 @@ +{ + "tags": [] +} \ No newline at end of file diff --git a/tests/wizards-and-warriors-2/not-use-method-overloading/src/main/java/GameMaster.java b/tests/wizards-and-warriors-2/not-use-method-overloading/src/main/java/GameMaster.java new file mode 100644 index 00000000..97c00b5b --- /dev/null +++ b/tests/wizards-and-warriors-2/not-use-method-overloading/src/main/java/GameMaster.java @@ -0,0 +1,29 @@ +public class GameMaster { + + public String describe(Character character) { + return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points."; + } + + public String describe(Destination destination) { + return "You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants."; + } + + public String describe(TravelMethod travelMethod) { + if (travelMethod == TravelMethod.WALKING) { + return "You're traveling to your destination by walking."; + } + return "You're traveling to your destination on horseback."; + } + + public String describe(Character character, Destination destination, TravelMethod travelMethod) { + return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points. " + + (travelMethod == TravelMethod.WALKING ? "You're traveling to your destination by walking. " : "You're traveling to your destination on horseback. ") + + "You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants."; + } + + public String describe(Character character, Destination destination) { + return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points. " + + "You're traveling to your destination by walking. " + + "You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants."; + } +} \ No newline at end of file diff --git a/tests/wizards-and-warriors-2/use-string-format/.meta/config.json b/tests/wizards-and-warriors-2/use-string-format/.meta/config.json new file mode 100644 index 00000000..decfa680 --- /dev/null +++ b/tests/wizards-and-warriors-2/use-string-format/.meta/config.json @@ -0,0 +1,32 @@ +{ + "authors": [ + "sougat818" + ], + "contributors": [ + "jagdish-15", + "sanderploegsma" + ], + "files": { + "solution": [ + "src/main/java/GameMaster.java" + ], + "test": [ + "src/test/java/GameMasterTest.java" + ], + "exemplar": [ + ".meta/src/reference/java/GameMaster.java" + ], + "editor": [ + "src/main/java/Character.java", + "src/main/java/Destination.java", + "src/main/java/TravelMethod.java" + ], + "invalidator": [ + "build.gradle" + ] + }, + "forked_from": [ + "csharp/wizards-and-warriors-2" + ], + "blurb": "Learn about method overloading by extending your favorite RPG." +} \ No newline at end of file diff --git a/tests/wizards-and-warriors-2/use-string-format/expected_analysis.json b/tests/wizards-and-warriors-2/use-string-format/expected_analysis.json new file mode 100644 index 00000000..2b150682 --- /dev/null +++ b/tests/wizards-and-warriors-2/use-string-format/expected_analysis.json @@ -0,0 +1,14 @@ +{ + "comments": [ + { + "comment": "java.general.prefer_string_concatenation", + "params": {}, + "type": "informative" + }, + { + "comment": "java.general.feedback_request", + "params": {}, + "type": "informative" + } + ] +} \ No newline at end of file diff --git a/tests/wizards-and-warriors-2/use-string-format/expected_tags.json b/tests/wizards-and-warriors-2/use-string-format/expected_tags.json new file mode 100644 index 00000000..eb25b190 --- /dev/null +++ b/tests/wizards-and-warriors-2/use-string-format/expected_tags.json @@ -0,0 +1,3 @@ +{ + "tags": [] +} \ No newline at end of file diff --git a/tests/wizards-and-warriors-2/use-string-format/src/main/java/GameMaster.java b/tests/wizards-and-warriors-2/use-string-format/src/main/java/GameMaster.java new file mode 100644 index 00000000..e32fd7d4 --- /dev/null +++ b/tests/wizards-and-warriors-2/use-string-format/src/main/java/GameMaster.java @@ -0,0 +1,28 @@ +public class GameMaster { + + public String describe(Character character) { + return "You're a level %d %s with %d hit points.".formatted(character.getLevel(), + character.getCharacterClass(), character.getHitPoints()); + } + + public String describe(Destination destination) { + return "You've arrived at %s, which has %d inhabitants.".formatted(destination.getName(), + destination.getInhabitants()); + } + + public String describe(TravelMethod travelMethod) { + if (travelMethod == TravelMethod.WALKING) { + return "You're traveling to your destination by walking."; + } + return "You're traveling to your destination on horseback."; + + } + + public String describe(Character character, Destination destination, TravelMethod travelMethod) { + return "%s %s %s".formatted(describe(character), describe(travelMethod), describe(destination)); + } + + public String describe(Character character, Destination destination) { + return describe(character, destination, TravelMethod.WALKING); + } +} From 42c95419e1104e2da8b89384a043b1456fd31af0 Mon Sep 17 00:00:00 2001 From: chiarazarrella Date: Fri, 11 Apr 2025 10:22:18 +0200 Subject: [PATCH 2/2] minor change --- .../wizardsandwarriors2/WizardsAndWarriors2Analyzer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/analyzer/exercises/wizardsandwarriors2/WizardsAndWarriors2Analyzer.java b/src/main/java/analyzer/exercises/wizardsandwarriors2/WizardsAndWarriors2Analyzer.java index 8eaed9e2..4a745e88 100644 --- a/src/main/java/analyzer/exercises/wizardsandwarriors2/WizardsAndWarriors2Analyzer.java +++ b/src/main/java/analyzer/exercises/wizardsandwarriors2/WizardsAndWarriors2Analyzer.java @@ -12,7 +12,7 @@ import java.util.List; /** - * The {@link WizardsAndWarriors2Analyzer} is the analyzer implementation for the {@code wizards-and-warriors-2} practice exercise. + * The {@link WizardsAndWarriors2Analyzer} is the analyzer implementation for the {@code wizards-and-warriors-2} concept exercise. * * @see The wizards-and-warriors exercise on the Java track */