From 6ffc230f95e11bf8801bbacdc412d6be309feaa1 Mon Sep 17 00:00:00 2001 From: Yousif Date: Sun, 19 May 2024 11:49:38 +0200 Subject: [PATCH] Here is a solution for the tasks. --- JavaKeywords.java | 29 ++++++++++ JavaKeywordsTest.java | 14 +++++ OddIntegers.java | 13 +++++ OddIntegersTest.java | 14 +++++ Player.java | 119 ++++++++++++++++++++++++++++++++++++++++++ PlayerTest.java | 51 ++++++++++++++++++ 6 files changed, 240 insertions(+) create mode 100644 JavaKeywords.java create mode 100644 JavaKeywordsTest.java create mode 100644 OddIntegers.java create mode 100644 OddIntegersTest.java create mode 100644 Player.java create mode 100644 PlayerTest.java diff --git a/JavaKeywords.java b/JavaKeywords.java new file mode 100644 index 0000000..8eb98c9 --- /dev/null +++ b/JavaKeywords.java @@ -0,0 +1,29 @@ +import java.util.HashSet; +import java.util.Set; + +public class JavaKeywords { + + private static final Set JAVA_KEYWORDS = new HashSet<>(); + + static { + String[] keywords = { + "abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", + "default", "do", "double", "else", "enum", "extends", "final", "finally", "float", "for", "goto", "if", + "implements", "import", "instanceof", "int", "interface", "long", "native", "new", "package", "private", + "protected", "public", "return", "short", "static", "strictfp", "super", "switch", "synchronized", "this", + "throw", "throws", "transient", "try", "void", "volatile", "while" + }; + for (String keyword : keywords) { + JAVA_KEYWORDS.add(keyword); + } + } + + public static boolean containsJavaKeyword(String input) { + for (String keyword : JAVA_KEYWORDS) { + if (input.contains(keyword)) { + return true; + } + } + return false; + } +} diff --git a/JavaKeywordsTest.java b/JavaKeywordsTest.java new file mode 100644 index 0000000..30d802c --- /dev/null +++ b/JavaKeywordsTest.java @@ -0,0 +1,14 @@ +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +public class JavaKeywordsTest { + + @Test + public void testContainsJavaKeyword() { + assertTrue(JavaKeywords.containsJavaKeyword("Don't break my heart")); + assertFalse(JavaKeywords.containsJavaKeyword("I love to breakdance")); + assertTrue(JavaKeywords.containsJavaKeyword("Public announcement")); + assertFalse(JavaKeywords.containsJavaKeyword("This is a regular sentence")); + assertTrue(JavaKeywords.containsJavaKeyword("return the value")); + } +} \ No newline at end of file diff --git a/OddIntegers.java b/OddIntegers.java new file mode 100644 index 0000000..8b0f5e5 --- /dev/null +++ b/OddIntegers.java @@ -0,0 +1,13 @@ +import java.util.ArrayList; +import java.util.List; + +public class OddIntegers { + + public static int[] getOddIntegers(int n) { + List oddNumbers = new ArrayList<>(); + for (int i = 1; i <= n; i += 2) { + oddNumbers.add(i); + } + return oddNumbers.stream().mapToInt(i -> i).toArray(); + } +} \ No newline at end of file diff --git a/OddIntegersTest.java b/OddIntegersTest.java new file mode 100644 index 0000000..6ea4520 --- /dev/null +++ b/OddIntegersTest.java @@ -0,0 +1,14 @@ +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +public class OddIntegersTest { + + @Test + public void testOddIntegers() { + assertArrayEquals(new int[]{1, 3, 5, 7, 9}, OddIntegers.getOddIntegers(10)); + assertArrayEquals(new int[]{1, 3, 5, 7}, OddIntegers.getOddIntegers(8)); + assertArrayEquals(new int[]{}, OddIntegers.getOddIntegers(0)); + assertArrayEquals(new int[]{1}, OddIntegers.getOddIntegers(1)); + assertArrayEquals(new int[]{1, 3}, OddIntegers.getOddIntegers(3)); + } +} \ No newline at end of file diff --git a/Player.java b/Player.java new file mode 100644 index 0000000..576fc00 --- /dev/null +++ b/Player.java @@ -0,0 +1,119 @@ +public abstract class Player { + + protected int health; + protected int strength; + protected int lives; + protected final int initialHealth; + + public Player(int health, int strength, int lives) { + this.health = health; + this.strength = strength; + this.lives = lives; + this.initialHealth = health; + } + + public int getHealth() { + return health; + } + + public void setHealth(int health) { + this.health = health; + } + + public int getStrength() { + return strength; + } + + public void setStrength(int strength) { + this.strength = strength; + } + + public int getLives() { + return lives; + } + + public void setLives(int lives) { + this.lives = lives; + } + + public void decrementLive() { + if (lives > 0) { + lives--; + health = initialHealth; + } else { + System.out.println("This character is dead"); + } + } + + public void attack(Player playerToAttack) { + playerToAttack.setHealth(playerToAttack.getHealth() - this.strength); + playerToAttack.checkHealth(); + } + + public void checkHealth() { + if (this.health <= 0) { + this.decrementLive(); + } + } +} + +public class Warrior extends Player { + private int force; + + public Warrior(int health, int strength, int lives, int force) { + super(health, strength, lives); + this.force = force; + } + + public int getForce() { + return force; + } + + public void setForce(int force) { + this.force = force; + } + + public Elf convertToElf() { + return new Elf(this.health, this.strength, this.lives, this.force); + } +} + +public class Elf extends Player { + private int speed; + + public Elf(int health, int strength, int lives, int speed) { + super(health, strength, lives); + this.speed = speed; + } + + public int getSpeed() { + return speed; + } + + public void setSpeed(int speed) { + this.speed = speed; + } +} + + +public class Wizard extends Player { + private String spell; + + public Wizard(int health, int strength, int lives, String spell) { + super(health, strength, lives); + this.spell = spell; + } + + public String getSpell() { + return spell; + } + + public void setSpell(String spell) { + this.spell = spell; + } + + public Elf convertToElf() { + return new Elf(this.health, this.strength, this.lives, 0); // Assuming spell to speed conversion isn't specified + } +} + diff --git a/PlayerTest.java b/PlayerTest.java new file mode 100644 index 0000000..a0f4fab --- /dev/null +++ b/PlayerTest.java @@ -0,0 +1,51 @@ +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +public class PlayerTest { + + @Test + public void testPlayerAttributes() { + Warrior warrior = new Warrior(100, 10, 3, 5); + assertEquals(100, warrior.getHealth()); + assertEquals(10, warrior.getStrength()); + assertEquals(3, warrior.getLives()); + assertEquals(5, warrior.getForce()); + } + + @Test + public void testDecrementLive() { + Warrior warrior = new Warrior(100, 10, 1, 5); + warrior.setHealth(0); + warrior.checkHealth(); + assertEquals(0, warrior.getLives()); + assertEquals(100, warrior.getHealth()); + } + + @Test + public void testAttack() { + Warrior warrior = new Warrior(100, 10, 3, 5); + Elf elf = new Elf(50, 5, 3, 10); + warrior.attack(elf); + assertEquals(40, elf.getHealth()); + } + + @Test + public void testConvertToElf() { + Warrior warrior = new Warrior(100, 10, 3, 5); + Elf elf = warrior.convertToElf(); + assertEquals(100, elf.getHealth()); + assertEquals(10, elf.getStrength()); + assertEquals(3, elf.getLives()); + assertEquals(5, elf.getSpeed()); + } + + @Test + public void testWizardToElf() { + Wizard wizard = new Wizard(100, 10, 3, "Fireball"); + Elf elf = wizard.convertToElf(); + assertEquals(100, elf.getHealth()); + assertEquals(10, elf.getStrength()); + assertEquals(3, elf.getLives()); + assertEquals(0, elf.getSpeed()); // Assuming 0 speed as spell to speed conversion is not defined + } +}