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
52 changes: 52 additions & 0 deletions src1/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>src1</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<!-- Mockito 5 -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.25.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<skipTests>false</skipTests>
<!-- Testing related properties -->
<junit.jupiter.version>5.4.0-RC1</junit.jupiter.version>
</properties>

</project>
16 changes: 16 additions & 0 deletions src1/src/main/java/Elf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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;
}
}
25 changes: 25 additions & 0 deletions src1/src/main/java/JavaKeyWordChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class JavaKeyWordChecker {

private static final Set<String> JAVA_KEYWORDS = new HashSet<>(Arrays.asList(
"abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class",
"const", "continue", "default", "do", "double", "else", "enum", "exports", "extends",
"final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof",
"int", "interface", "long", "module", "native", "new", "package", "private", "protected",
"public", "requires", "return", "short", "static", "strictfp", "super", "switch",
"synchronized", "this", "throw", "throws", "transient", "try", "var", "void", "volatile", "while"
));

public static boolean containsJavaKeyword(String input) {
String[] words = input.split("\\W+");
for (String word : words) {
if (JAVA_KEYWORDS.contains(word)) {
return true;
}
}
return false;
}
}
14 changes: 14 additions & 0 deletions src1/src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class Main {
public static void main(String[] args) {
Warrior warrior1 = new Warrior(100, 20, 3,40);
Warrior warrior2 = new Warrior(80, 15, 2,60);

warrior1.attack(warrior2);
System.out.println("Warrior2 Health: " + warrior2.getHealth());
System.out.println("Warrior2 Lives: " + warrior2.getLives());

warrior2.attack(warrior1);
System.out.println("Warrior1 Health: " + warrior1.getHealth());
System.out.println("Warrior1 Lives: " + warrior1.getLives());
}
}
11 changes: 11 additions & 0 deletions src1/src/main/java/OddNumbers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class OddNumbers {
public static int[] getOddIntegers(int n) {
if (n < 1) {
return new int[0];
}

return java.util.stream.IntStream.rangeClosed(1, n)
.filter(i -> i % 2 != 0)
.toArray();
}
}
63 changes: 63 additions & 0 deletions src1/src/main/java/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
public abstract class Player {
private int health;
private int originalHealth;
private int strength;
private int lives;

public Player(int health, int strength, int lives) {
this.health = health;
this.originalHealth = health; // Store the original health
this.strength = strength;
this.lives = lives;
}

// Getters
public int getHealth() {
return health;
}

public int getStrength() {
return strength;
}

public int getLives() {
return lives;
}

// Setters
public void setHealth(int health) {
this.health = health;
}

public void setStrength(int strength) {
this.strength = strength;
}

public void setLives(int lives) {
this.lives = lives;
}

// Method to decrement lives and restore health
public void decrementLive() {
if (lives > 0) {
lives--;
health = originalHealth;
if (lives <= 0) {
System.out.println("This character is dead");
}
}
}

// Method to attack another player
public void attack(Player playerToAttack) {
playerToAttack.setHealth(playerToAttack.getHealth() - this.strength);
playerToAttack.checkHealth();
}

// Method to check health and call decrementLive() if health is 0 or less
public void checkHealth() {
if (this.health <= 0) {
decrementLive();
}
}
}
20 changes: 20 additions & 0 deletions src1/src/main/java/Warrior.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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;
}

// Additional methods specific to Warrior can be added here
public int getForce() {
return force;
}

public void setForce(int force) {
this.force = force;
}
public Elf convertToElf() {
return new Elf(this.getHealth(), this.getStrength(), this.getLives(), this.force);
}
}
20 changes: 20 additions & 0 deletions src1/src/main/java/Wizard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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.getHealth(), this.getStrength(), this.getLives(), 0); // Assuming spell power doesn't convert to speed, set speed to 0
}
}
31 changes: 31 additions & 0 deletions src1/src/test/java/ElfTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class ElfTest {

private Elf elf;

@BeforeEach
public void setUp() {
elf = new Elf(100, 20, 3, 15);
}

@Test
public void testGettersAndSetters() {
assertEquals(100, elf.getHealth());
assertEquals(20, elf.getStrength());
assertEquals(3, elf.getLives());
assertEquals(15, elf.getSpeed());

elf.setHealth(90);
elf.setStrength(25);
elf.setLives(2);
elf.setSpeed(20);

assertEquals(90, elf.getHealth());
assertEquals(25, elf.getStrength());
assertEquals(2, elf.getLives());
assertEquals(20, elf.getSpeed());
}
}
15 changes: 15 additions & 0 deletions src1/src/test/java/JavaKeywordCheckerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class JavaKeywordCheckerTest {

@Test
public void testContainsJavaKeyword() {
assertTrue(JavaKeyWordChecker.containsJavaKeyword("Don't break my heart"));
assertFalse(JavaKeyWordChecker.containsJavaKeyword("I love to breakdance"));
assertTrue(JavaKeyWordChecker.containsJavaKeyword("This is a class"));
assertFalse(JavaKeyWordChecker.containsJavaKeyword("This is classic music"));
assertTrue(JavaKeyWordChecker.containsJavaKeyword("Switch to the new version"));
assertTrue(JavaKeyWordChecker.containsJavaKeyword("Switching to a new version"));
}
}
35 changes: 35 additions & 0 deletions src1/src/test/java/OddIntegersTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import org.junit.jupiter.api.Test;

public class OddIntegersTest {

@Test
public void testOddIntegersUpTo1() {
int[] expected = {1};
assertArrayEquals(expected, OddNumbers.getOddIntegers(1));
}

@Test
public void testOddIntegersUpTo5() {
int[] expected = {1, 3, 5};
assertArrayEquals(expected, OddNumbers.getOddIntegers(5));
}

@Test
public void testOddIntegersUpTo10() {
int[] expected = {1, 3, 5, 7, 9};
assertArrayEquals(expected, OddNumbers.getOddIntegers(10));
}

@Test
public void testOddIntegersUpTo0() {
int[] expected = {};
assertArrayEquals(expected, OddNumbers.getOddIntegers(0));
}

@Test
public void testOddIntegersUpToNegative() {
int[] expected = {};
assertArrayEquals(expected, OddNumbers.getOddIntegers(-5));
}
}
74 changes: 74 additions & 0 deletions src1/src/test/java/PlayerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class PlayerTest {

private Warrior warrior1;
private Warrior warrior2;

@BeforeEach
public void setUp() {
warrior1 = new Warrior(100, 20, 3,60);
warrior2 = new Warrior(80, 15, 2,70);
}

@Test
public void testInitialValues() {
assertEquals(100, warrior1.getHealth());
assertEquals(20, warrior1.getStrength());
assertEquals(3, warrior1.getLives());

assertEquals(80, warrior2.getHealth());
assertEquals(15, warrior2.getStrength());
assertEquals(2, warrior2.getLives());
}

@Test
public void testAttack() {
warrior1.attack(warrior2);
assertEquals(60, warrior2.getHealth());
assertEquals(2, warrior2.getLives());

warrior2.attack(warrior1);
assertEquals(85, warrior1.getHealth());
assertEquals(3, warrior1.getLives());
}

@Test
public void testCheckHealth() {
warrior2.setHealth(0);
warrior2.checkHealth();
assertEquals(80, warrior2.getHealth()); // Restored health
assertEquals(1, warrior2.getLives());
}

@Test
public void testDecrementLive() {
warrior2.decrementLive();
assertEquals(80, warrior2.getHealth()); // Restored health
assertEquals(1, warrior2.getLives());

warrior2.decrementLive();
assertEquals(80, warrior2.getHealth());
assertEquals(0, warrior2.getLives());

warrior2.decrementLive(); // Should print "This character is dead"
assertEquals(80, warrior2.getHealth());
assertEquals(0, warrior2.getLives());
}

@Test
public void testDeadCharacter() {
warrior2.setLives(1);
warrior2.setHealth(10);

warrior1.attack(warrior2);
assertEquals(0, warrior2.getLives());
assertEquals(80, warrior2.getHealth());

warrior1.attack(warrior2); // Further attack should not decrement lives
assertEquals(0, warrior2.getLives());
assertEquals(60, warrior2.getHealth()); // Health still decreases
}
}
Loading