Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions JavaKeywords.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.util.HashSet;
import java.util.Set;

public class JavaKeywords {

private static final Set<String> 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;
}
}
14 changes: 14 additions & 0 deletions JavaKeywordsTest.java
Original file line number Diff line number Diff line change
@@ -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"));
}
}
13 changes: 13 additions & 0 deletions OddIntegers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import java.util.ArrayList;
import java.util.List;

public class OddIntegers {

public static int[] getOddIntegers(int n) {
List<Integer> oddNumbers = new ArrayList<>();
for (int i = 1; i <= n; i += 2) {
oddNumbers.add(i);
}
return oddNumbers.stream().mapToInt(i -> i).toArray();
}
}
14 changes: 14 additions & 0 deletions OddIntegersTest.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
119 changes: 119 additions & 0 deletions Player.java
Original file line number Diff line number Diff line change
@@ -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
}
}

51 changes: 51 additions & 0 deletions PlayerTest.java
Original file line number Diff line number Diff line change
@@ -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
}
}