From 2fbee1b1b16043dfbd65a3cfa32fc31f60cdd613 Mon Sep 17 00:00:00 2001 From: nicoagostini Date: Sun, 21 Sep 2025 18:58:18 -0700 Subject: [PATCH] Comments added --- src/code/ca/bcit/comp2522/lab02/Creature.java | 25 ++- src/code/ca/bcit/comp2522/lab02/Date.java | 20 ++- src/code/ca/bcit/comp2522/lab02/Dragon.java | 7 +- src/code/ca/bcit/comp2522/lab02/Elf.java | 5 +- src/code/ca/bcit/comp2522/lab02/Orc.java | 5 +- src/tests/ca/bcit/comp2522/CreatureTest.java | 159 +++++++++--------- 6 files changed, 127 insertions(+), 94 deletions(-) diff --git a/src/code/ca/bcit/comp2522/lab02/Creature.java b/src/code/ca/bcit/comp2522/lab02/Creature.java index 70e7518..a1d8ba1 100644 --- a/src/code/ca/bcit/comp2522/lab02/Creature.java +++ b/src/code/ca/bcit/comp2522/lab02/Creature.java @@ -2,9 +2,9 @@ package ca.bcit.comp2522.lab02; /** * Creature contains simple information regarding - * a creature such as its health and name. + * a creature such as its health, name and date of birth. * - * @author Braeden Sowinski + * @author Braeden Sowinski, Nicolas Agostini, Trishaan Shetty * @version 1.0.0 */ public class Creature { @@ -17,8 +17,8 @@ public class Creature { private int health; - /* - * validateName ensures input name is not empty. + /** + * validateName ensures input name is not null or empty. * * @param name input * @throws IllegalArgumentException @@ -26,12 +26,12 @@ public class Creature { private static void validateName(final String name) throws IllegalArgumentException { - if (name.isEmpty()) { + if (name == null || name.isEmpty()) { throw new IllegalArgumentException("Name cannot be null or empty"); } } - /* + /** * validateHealth ensures input health is within valid range * of MAX_HEALTH and MIN_HEALTH * @@ -46,7 +46,7 @@ public class Creature { } } - /* + /** * validateDateOfBirth ensures the date of birth * is not in the future. * @@ -100,6 +100,7 @@ public class Creature { * * @param damage to be dealt * @throws DamageException if damage is negative + * @throws RuntimeException if the creature is not alive */ public void takeDamage(final int damage) throws DamageException, RuntimeException @@ -187,8 +188,16 @@ public class Creature { public void printDetails() { System.out.println(getDetails()); } - + /** + * Prints the details of the creature. + * @return health value of the creature + */ public int getHealth() { return this.health;} + + /** + * Returns the date of birth of the creature. + * @return Date of Birth of the creature. + */ public Date getDateOfBirth() { return this.dateOfBirth;} } \ No newline at end of file diff --git a/src/code/ca/bcit/comp2522/lab02/Date.java b/src/code/ca/bcit/comp2522/lab02/Date.java index 8a777b5..5d0d393 100644 --- a/src/code/ca/bcit/comp2522/lab02/Date.java +++ b/src/code/ca/bcit/comp2522/lab02/Date.java @@ -144,8 +144,9 @@ public final class Date { } } - /* + /** validateYear checks input year is in valid range + @param year to be validated */ private static void validateYear(final int year) throws IllegalArgumentException @@ -156,8 +157,9 @@ public final class Date { } } - /* + /** validateMonth checks input month is in valid range + @param month to be validated */ private static void validateMonth(final int month) throws IllegalArgumentException @@ -168,9 +170,12 @@ public final class Date { } } - /* - validateDay ensures the day given is less than the max day - calculated by daysInMonth and greater than the MIN_DAY + /** + * validateDay ensures the day given is less than the max day + * calculated by daysInMonth and greater than the MIN_DAY + * @param day + * @param month + * @param year */ private static void validateDay( final int day, @@ -385,6 +390,11 @@ public final class Date { return day; } + /** + * isInFuture validates if the Date instance is from a date greater than + * MAX_YEAR, which corresponds to the current year + * @return if the date is in the future + */ public boolean isInFuture() { if (this.year > MAX_YEAR) { diff --git a/src/code/ca/bcit/comp2522/lab02/Dragon.java b/src/code/ca/bcit/comp2522/lab02/Dragon.java index cabba5c..86e0170 100644 --- a/src/code/ca/bcit/comp2522/lab02/Dragon.java +++ b/src/code/ca/bcit/comp2522/lab02/Dragon.java @@ -43,9 +43,12 @@ public class Dragon extends Creature { final Date dateOfBirth, final int health, final int firePower - ) { + ) + { super(name, dateOfBirth, health); + validateFirePower(firePower); + this.firePower = firePower; } @@ -70,12 +73,14 @@ public class Dragon extends Creature { return builder.toString(); } + /** * breathFire onto Creatures to deal damage * and reduces current firePower value * * @param target Creature to deal damage to * @throws LowFirePowerException if not enough firePower + * @throws RuntimeException if the dragon is not alive to accomplish the action */ public void breatheFire(final Creature target) throws LowFirePowerException, RuntimeException { diff --git a/src/code/ca/bcit/comp2522/lab02/Elf.java b/src/code/ca/bcit/comp2522/lab02/Elf.java index 5d88329..10c0e24 100644 --- a/src/code/ca/bcit/comp2522/lab02/Elf.java +++ b/src/code/ca/bcit/comp2522/lab02/Elf.java @@ -2,7 +2,7 @@ package ca.bcit.comp2522.lab02; /** * Elf extends Creature and - * holds a mana value? Never heard of that + * holds a mana value * * @author Braeden Sowinski * @version 1.0.0 @@ -15,7 +15,7 @@ public class Elf extends Creature { private int mana; - /* + /** * validateManaValue ensures a given value is within * the correct mana range * @@ -76,6 +76,7 @@ public class Elf extends Creature { * * @param target Creature to deal damage to * @throws LowManaException if mana value is too low + * @throws RuntimeException if the Elf is not alive to accomplish the action */ public void castSpell(final Creature target) throws LowManaException, RuntimeException diff --git a/src/code/ca/bcit/comp2522/lab02/Orc.java b/src/code/ca/bcit/comp2522/lab02/Orc.java index c4e7c39..58a1a07 100644 --- a/src/code/ca/bcit/comp2522/lab02/Orc.java +++ b/src/code/ca/bcit/comp2522/lab02/Orc.java @@ -17,7 +17,7 @@ public class Orc extends Creature { private int rage; - /* + /** * validateRageValue ensures a given value is within * the correct rage range * @@ -73,12 +73,13 @@ public class Orc extends Creature { } /** - * bezerk goes the orc when he is really mad. + * berserk goes the orc when he is really mad. * dealing damage to target Creatures with the * potential for double damage. * * @param target Creature to deal damage to * @throws LowRageException if rage value is too low + * @throws RuntimeException if the Orc is not alive to accomplish the action */ public void berserk(final Creature target) throws LowRageException, RuntimeException diff --git a/src/tests/ca/bcit/comp2522/CreatureTest.java b/src/tests/ca/bcit/comp2522/CreatureTest.java index 07d03e7..7d78e76 100644 --- a/src/tests/ca/bcit/comp2522/CreatureTest.java +++ b/src/tests/ca/bcit/comp2522/CreatureTest.java @@ -17,103 +17,110 @@ public class CreatureTest { * Main entry point for the test program. * * @param args command-line arguments (not used) - * @throws LowFirePowerException if a dragon attempts to breathe fire with insufficient fire power - * @throws LowManaException if an elf attempts to cast a spell with insufficient mana */ - public static void main(String[] args) throws LowFirePowerException, LowManaException, LowRageException { + public static void main(String[] args) { - final Creature creatureDragon; - final Creature creatureElf; - final Creature creatureOrc; + final Creature creatureDragon; + final Creature creatureElf; + final Creature creatureOrc; - final Date dragonBirthDate; - final Date elfBirthDate; - final Date orcBirthDate; + final Date dragonBirthDate; + final Date elfBirthDate; + final Date orcBirthDate; - dragonBirthDate = new Date(2012, 9, 17); - elfBirthDate = new Date(1900, 11, 5); - orcBirthDate = new Date(500, 5, 3); + dragonBirthDate = new Date(2012, 9, 17); + elfBirthDate = new Date(1900, 11, 5); + orcBirthDate = new Date(500, 5, 3); - creatureDragon = new Dragon("Sparky", dragonBirthDate, 5, 60); - creatureElf = new Elf("Ben", elfBirthDate, 10, 15); - creatureOrc = new Orc("Shobob", orcBirthDate, 12, 3); - - final Dragon dragon = (Dragon)creatureDragon; - final Elf elf = (Elf)creatureElf; - final Orc orc = (Orc)creatureOrc; + creatureDragon = new Dragon("Sparky", dragonBirthDate, 5, 60); + creatureElf = new Elf("Ben", elfBirthDate, 10, 15); + creatureOrc = new Orc("Shobob", orcBirthDate, 12, 3); + if (creatureDragon instanceof Dragon) { + + System.out.println("creatureDragon is a Dragon."); + } - dragon.printDetails(); - if (dragon instanceof Creature) System.out.println("This creature is a Dragon."); - elf.printDetails(); - if (elf instanceof Creature) System.out.println("This creature is an Elf."); - orc.printDetails(); - if (orc instanceof Creature) System.out.println("This creature is an Orc."); + System.out.println("creatureDragon type: " + creatureDragon.getClass().getSimpleName()); + if (creatureElf instanceof Elf) { - try { + System.out.println("creatureElf is an Elf."); - System.out.println("- Dragon breathed fire on the Elf"); - dragon.breatheFire(elf); + } + System.out.println("creatureElf type: " + creatureElf.getClass().getSimpleName()); - dragon.printDetails(); - elf.printDetails(); - orc.printDetails(); + if (creatureOrc instanceof Orc) { - } catch (LowFirePowerException e) { - System.out.println("Attack Not allowed: the dragon's fire power was too low to breathe fire."); - } catch (Exception e) { - System.out.println("Unexpected problem during attack: " + e.getMessage()); - } + System.out.println("creatureOrc is an Orc."); + } - try { + System.out.println("creatureOrc type: " + creatureOrc.getClass().getSimpleName()); - System.out.println("- Elf cast a spell on the Dragon"); - elf.castSpell(dragon); + creatureDragon.printDetails(); + creatureElf.printDetails(); + creatureOrc.printDetails(); - dragon.printDetails(); - elf.printDetails(); - orc.printDetails(); + final Dragon dragon = (Dragon) creatureDragon; + final Elf elf = (Elf) creatureElf; + final Orc orc = (Orc) creatureOrc; - } catch (LowManaException e) { - System.out.println("Attack Not allowed: the elf didn't have enough mana to cast the spell."); - } catch (Exception e) { - System.out.println("Unexpected problem during attack: " + e.getMessage()); - } + try { - try { + System.out.println("- Dragon breathed fire on the Elf"); - System.out.println("- Orc went berserk on the Elf"); - orc.berserk(elf); - - dragon.printDetails(); - elf.printDetails(); - orc.printDetails(); - - } catch (LowRageException e) { - System.out.println("Attack Not allowed: the orc didn't have enough rage to go berserk"); - } catch (Exception e) { - System.out.println("Unexpected problem during attack: " + e.getMessage()); - } - try { - - System.out.println("- Dragon breathed fire on the Elf"); - dragon.breatheFire(elf); - - dragon.printDetails(); - elf.printDetails(); - orc.printDetails(); - - } catch (LowFirePowerException e) { - System.out.println("Attack Not allowed: the dragon's fire power was too low to breathe fire."); - } catch (Exception e) { - System.out.println("Unexpected problem during attack: " + e.getMessage()); - } - - System.out.println("Final Stats:"); + dragon.breatheFire(elf); dragon.printDetails(); elf.printDetails(); orc.printDetails(); + + } catch (LowFirePowerException e) { + + System.out.println("Attack Not allowed: the dragon's fire power was too low to breathe fire."); + + } catch (Exception e) { + + System.out.println("Unexpected problem during attack: " + e.getMessage()); + } + + try { + + System.out.println("- Elf cast a spell on the Dragon"); + elf.castSpell(dragon); + dragon.printDetails(); + elf.printDetails(); + orc.printDetails(); + + } catch (LowManaException e) { + + System.out.println("Attack Not allowed: the elf didn't have enough mana to cast the spell."); + + } catch (Exception e) { + + System.out.println("Unexpected problem during attack: " + e.getMessage()); + } + + try { + + System.out.println("- Orc went berserk on the Elf"); + orc.berserk(elf); + dragon.printDetails(); + elf.printDetails(); + orc.printDetails(); + + } catch (LowRageException e) { + + System.out.println("Attack Not allowed: the orc didn't have enough rage to go berserk"); + + } catch (Exception e) { + + System.out.println("Unexpected problem during attack: " + e.getMessage()); + } + + System.out.println("Final Stats:"); + dragon.printDetails(); + elf.printDetails(); + orc.printDetails(); } }