From 9bb3af19a915539778496a3fdd49371e2ac21aac Mon Sep 17 00:00:00 2001 From: nicoagostini Date: Sat, 20 Sep 2025 12:46:14 -0700 Subject: [PATCH] Tests running but missing comments --- .../ca/bcit/comp2522/lab02/Creature.class | Bin 0 -> 2454 bytes src/code/ca/bcit/comp2522/lab02/Creature.java | 51 ++++++-- .../bcit/comp2522/lab02/DamageException.class | Bin 0 -> 260 bytes src/code/ca/bcit/comp2522/lab02/Date.class | Bin 0 -> 5038 bytes src/code/ca/bcit/comp2522/lab02/Date.java | 2 +- src/code/ca/bcit/comp2522/lab02/Dragon.class | Bin 0 -> 1755 bytes src/code/ca/bcit/comp2522/lab02/Dragon.java | 35 ++++-- src/code/ca/bcit/comp2522/lab02/Elf.class | Bin 0 -> 1771 bytes src/code/ca/bcit/comp2522/lab02/Elf.java | 32 +++-- .../comp2522/lab02/HealingException.class | Bin 0 -> 262 bytes .../lab02/LowFirePowerException.class | Bin 0 -> 265 bytes .../comp2522/lab02/LowManaException.class | Bin 0 -> 255 bytes .../comp2522/lab02/LowRageException.class | Bin 0 -> 255 bytes src/code/ca/bcit/comp2522/lab02/Orc.class | Bin 0 -> 1772 bytes src/code/ca/bcit/comp2522/lab02/Orc.java | 48 ++++--- src/tests/ca/bcit/comp2522/CreatureTest.java | 119 ++++++++++++++++++ 16 files changed, 241 insertions(+), 46 deletions(-) create mode 100644 src/code/ca/bcit/comp2522/lab02/Creature.class create mode 100644 src/code/ca/bcit/comp2522/lab02/DamageException.class create mode 100644 src/code/ca/bcit/comp2522/lab02/Date.class create mode 100644 src/code/ca/bcit/comp2522/lab02/Dragon.class create mode 100644 src/code/ca/bcit/comp2522/lab02/Elf.class create mode 100644 src/code/ca/bcit/comp2522/lab02/HealingException.class create mode 100644 src/code/ca/bcit/comp2522/lab02/LowFirePowerException.class create mode 100644 src/code/ca/bcit/comp2522/lab02/LowManaException.class create mode 100644 src/code/ca/bcit/comp2522/lab02/LowRageException.class create mode 100644 src/code/ca/bcit/comp2522/lab02/Orc.class create mode 100644 src/tests/ca/bcit/comp2522/CreatureTest.java diff --git a/src/code/ca/bcit/comp2522/lab02/Creature.class b/src/code/ca/bcit/comp2522/lab02/Creature.class new file mode 100644 index 0000000000000000000000000000000000000000..d9ff845f1d7fdaf36f4ce9f0cef435f6f747379e GIT binary patch literal 2454 zcmaJ?T~k|C6kP{WE`-ZhTKWN{y&|-bV)>}8+SCdSAgO$)AT6!7JxLDXmYaJqIYIlX zGmeh0KIlw+aK_OWA9Ti+PMxvu{uBR%<2uPDq$On-l6%fwXYIAu-e>2Z|DOI0UkyYDP@JnN!l!8&1AdEJsW$ z{h3m}Gww(~>x_?$MR5e(7LM9DX7cTQ3F)-dN+9YMQ~qqJ!hS+v&}=)wx|4Pb46>J) z>pOZwIqPL;6umfUq0i)Y>N!i7()U$HmyKMFq63bNS4{f$w)EUAxs6}u#j7?3Feq?% z_k{_(w0_NmWs*bl2gs&hhT^P^Aq)$&?^0Bp5r_<>hF4pNIA>!7uW`d?RDsxj=M8Gq z#u&y0T8+M{guVk6m~$6wypD@3?MAsG0wQJM4S_Q+m7bS5RntFj37nXgX0ygFXjBtk=T(y=8YTq!#ba%}mX&%n7vZGOS9kq#7AX z7{t`g5Mg$*0xgS6H5*{@l0lZm>zH?awOGohRbfS@J?^wEYnjQi%)U|_N-z~>gV3v~oHBMXLX@j%rKRg}ab#$?o&m;P)A2+v2c<$r z&AMgfyEpp083nvR*MdM7bwO?|D7_J6i_N%8G3MlDM|3nnMv+FwLe|uVYy*2E#Pzp> zO%=|vO1WlbMh69sv+rw72!WLda~u|)Dh|vC!DeYIv0r=5&T|J>9&D*P@Y^-`$_z-< z8WK_@rROcXS|vCTQogSWNlzAws%U|CesEt-^`u(3&n7<@$}3sPi?gSLqY7w359V-@ z-IU_00oS>HuVy{Z^+L^h5iv74h>e-qP(H)E&hIGyIMK+l#8m=~ywA^eVt_vW7MwyC9DKkPPj%r#K16ujA|(e|NI4l&9_@+zgqEJp?9U-> zS3TMhv{Gg_M$5DjG_^JxJ#7SYK!-3--3;it(R zA#YO{VVn_+zwPlqF+o49$ghYr{>Gh#GM~W#pOl3jLW4cYyNZvhUf!mc4FnAKJ~Sp? zT8q88gUiq2Yh>%qQkkKBh~{`>sItP^!}!1^sXSIMz;S#^Wd;6V>0O)KyQ?-|B5t)B z@=h3Xs1h=<7JGXK?}Q=mg&|E*TjHHY!Cs$KqJBZPFB$bKM*W(d_6^SB+iKL~)u;*L zZXg-@G!Xh^cUk{3r}K|7nKdp|T(7wF9Z%fET6m(L-^RNr;qj-K27Z45|0D6Ys`$PT kf5_lpg#N;$Z}WHqK4vk%(hA(;{~fHG5PZtPrJ*PO2XXHjApigX literal 0 HcmV?d00001 diff --git a/src/code/ca/bcit/comp2522/lab02/Creature.java b/src/code/ca/bcit/comp2522/lab02/Creature.java index 4a83b60..d1bbc04 100644 --- a/src/code/ca/bcit/comp2522/lab02/Creature.java +++ b/src/code/ca/bcit/comp2522/lab02/Creature.java @@ -68,7 +68,7 @@ public class Creature { * @param dateOfBirth of creature * @param health of creature to start */ - Creature(final String name, + public Creature(final String name, final Date dateOfBirth, final int health) { @@ -101,17 +101,22 @@ public class Creature { * @param damage to be dealt * @throws DamageException if damage is negative */ - protected void takeDamage(final int damage) - throws DamageException + public void takeDamage(final int damage) + throws DamageException, RuntimeException { - if (damage < NO_HEALTH) { - throw new DamageException("Damage cannot be negative"); + if (!this.isAlive()){ + throw new RuntimeException("The creature is not alive."); } + else{ + if (damage < NO_HEALTH) { + throw new DamageException("Damage cannot be negative"); + } - this.health -= damage; + this.health -= damage; - if (this.health < NO_HEALTH) { - this.health = NO_HEALTH; + if (this.health < NO_HEALTH) { + this.health = NO_HEALTH; + } } } @@ -153,8 +158,19 @@ public class Creature { * * @return details of the creature. */ - protected String getDetails() { - return "Name: " + this.name; + public String getDetails() { + StringBuilder builder; + builder = new StringBuilder(); + builder.append("Is alive: " + this.isAlive()); + builder.append("Name: " + this.name); + builder.append("Date of birth: " + this.dateOfBirth); + builder.append("Age: " + this.getAgeYears()); + builder.append("Health: " + this.health); + + + + return builder.toString(); + } /** @@ -162,7 +178,18 @@ public class Creature { * * @return name of the creature */ - protected String getName() { + public String getName() { return this.name; } -} + + /** + * Prints the details of the creature. + */ + public void printDetails() { + System.out.println(getDetails()); + } + + public int getHealth() { return this.health;} + public Date getDateOfBirth() { return this.dateOfBirth;} + +} \ No newline at end of file diff --git a/src/code/ca/bcit/comp2522/lab02/DamageException.class b/src/code/ca/bcit/comp2522/lab02/DamageException.class new file mode 100644 index 0000000000000000000000000000000000000000..7d21c0007e25459f44e406b7d7953d97087bf106 GIT binary patch literal 260 zcmX^0Z`VEs1_nn4UM>bE24;2!79Ivx1~x_psjS4ZME#t^ymbAb(!7$)+*H?!Bxq$IOnINGBWV{Wag#%mF6a;7KJ1xK^ze@eR?2! z859}VfzAd2Mj!+_97u8kc}hT0kT{!`_I3uwjbM2$APMp$0|Us5yg-%QKsFNt4**O& BI;8*r literal 0 HcmV?d00001 diff --git a/src/code/ca/bcit/comp2522/lab02/Date.class b/src/code/ca/bcit/comp2522/lab02/Date.class new file mode 100644 index 0000000000000000000000000000000000000000..702dfb069eec6e9c64ae5b4365d61d29ced4c790 GIT binary patch literal 5038 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 Fe*uXA^Sl56 literal 0 HcmV?d00001 diff --git a/src/code/ca/bcit/comp2522/lab02/Date.java b/src/code/ca/bcit/comp2522/lab02/Date.java index 00cf2b6..8a777b5 100644 --- a/src/code/ca/bcit/comp2522/lab02/Date.java +++ b/src/code/ca/bcit/comp2522/lab02/Date.java @@ -13,7 +13,7 @@ public final class Date { public static final int MAX_YEAR = 2025; // Domain of dates - private static final int MIN_YEAR = 1800; + private static final int MIN_YEAR = 0; private static final int MIN_DAY = 1; private static final int CURRENT_DAY = 15; private static final int CURRENT_MONTH = 9; diff --git a/src/code/ca/bcit/comp2522/lab02/Dragon.class b/src/code/ca/bcit/comp2522/lab02/Dragon.class new file mode 100644 index 0000000000000000000000000000000000000000..d0932c8e17bccf12da0e5978a8e79387b34bb409 GIT binary patch literal 1755 zcmaJ>ZBrUo6n-wEzzRqZV>P~PQcV!8q$KG}NRtvkWd=19QWKl$*bCg?#$_+Fd(q~f zbf(`r{Q>Pv8)uT4=6iosr)L+G?TGmRyXW3>p7T7*5viuxopYiEop@iWVrE~A9Gr^TG@(eiWWBucB|VKmMZ_$5FKSmD~vIQ z>L>YRWdO zR(@qqLlm(P;%@c?!{Q}Wi?-mZYYT=uLtv_xRF$|A`O3;(1PoWANMe#9@`@BZaRo52 zibgwAQC!6|L#QRx4sTPBRC?ti*CaVQ6U8j9F-*7lktj;5!PRp^9nzblxTPG1TSMqo zhMZUx=Fo&>ff5&)#&zko1?n{N4I%(NKB+1ot6| z+%#(j9WaJy#j=E5G`ZslCxkNH6BjfMqZ+~{!@|c?=?3ZP!gZR(D2?7I*|v;Hif&RA zz%7c?zIcV=j4xiLc+D4oMm2O5ct{%7u|#o}qDORVF?vqT{({JCqWK$Q?=iBU96d$o z!yjI&+w>o&G|-vTFpm@#@i|371hfcp-W&7wBe+9gF6=t(j?y!pSzVhy#du~a_!kl` z9>V}Hix45j2qTEQnC{o!Bx)|Dc;+oqnLm+uhfjdt&uhoL-8|LY!xw!Sd(=Nb8Ov^d zLi^QKF}I&wIEDTpv+u1niNI6f}U;B}ey}smLB$IF(4O%3r5_mz*eNQc3Vg^5)!OsxPSF{ FIRE_POWER_USAGE) { - throw new LowFirePowerException("Fire power too low"); + public void breatheFire(final Creature target) + throws LowFirePowerException, RuntimeException { + + if (!this.isAlive()) { + throw new RuntimeException("The dragon is not alive."); + } + else { + if(firePower < FIRE_POWER_USAGE) { + throw new LowFirePowerException("Fire power too low"); + } } this.firePower -= FIRE_POWER_USAGE; target.takeDamage(FIRE_POWER_DAMAGE); + } /** diff --git a/src/code/ca/bcit/comp2522/lab02/Elf.class b/src/code/ca/bcit/comp2522/lab02/Elf.class new file mode 100644 index 0000000000000000000000000000000000000000..c41c80732c80caba2916640fe2f74c57b0b4543e GIT binary patch literal 1771 zcmaJ>+fo}x5IrMgBo=WK1_wD9fw2w3v25AS#quSxxLA&)BqdBjE=2}05o=bvD(#5G zf0C-arSbtuRbo=ODt^yLxqN0NKq-V@cBgx~&z#fUGylCg{1-qDdd2xMg0R5cbGa|+b8G2>tOm zB4(H>HU=1UA%^!bMMs^R0-|6tFUIgbrUfE3rOUF;`11=rM8_FAV+xiOxYmPYuR=vI z$KW&A7rmbh$K#GfF)uJ(_HQ73~s^s(dAFkH%g-4$iL8G*qdZ#h@Wt*scBMeM-! ztkUtEW7+cF4$m1W+O8_M>f5ThF1H>SAGOf1+JXZ)Qf5aMS)B24&{^4*2P)oG;-F? zA%R6q(Mr(@VTtzjF8c%8H@fUJ?VDZpE#~lA2S}EA1!!kz<@rnr{-+YZVjwY^`5j}g z(6^a5=)YA3o z0}M@s|3N%85&j#K29(bjzi|cuX%59L#OR3=Hq6xN4#-vVH2j8BFOf+7g~>x)0{%G8 z?+oEOM4--DCaQBG)=+B?9D&KM>iRY6>}!8qIXHhPt$V!vcPXaeU8i kY#qT*5hhE@A>79UBbTekJmO*S{S;5=Gw*PL>`It=3nH+RBLDyZ literal 0 HcmV?d00001 diff --git a/src/code/ca/bcit/comp2522/lab02/Elf.java b/src/code/ca/bcit/comp2522/lab02/Elf.java index ccd90c1..4e4d5e7 100644 --- a/src/code/ca/bcit/comp2522/lab02/Elf.java +++ b/src/code/ca/bcit/comp2522/lab02/Elf.java @@ -38,7 +38,7 @@ public class Elf extends Creature { * @param health int * @param mana int */ - Elf( + public Elf( final String name, final Date dateOfBirth, final int health, @@ -57,8 +57,17 @@ public class Elf extends Creature { * @return details of elf */ @Override - protected String getDetails() { - return "Name: " + this.getName() + " Mana: " + this.mana; + public String getDetails() { + StringBuilder builder; + builder = new StringBuilder(); + builder.append("Is alive: " + this.isAlive()); + builder.append(" Name: " + this.getName()); + builder.append(" Date of birth: " + (this.getDateOfBirth()).getYyyyMmDd()); + builder.append(" Age: " + this.getAgeYears()); + builder.append(" Health: " + this.getHealth()); + builder.append(" Mana: " + this.mana); + + return builder.toString(); } /** @@ -68,16 +77,21 @@ public class Elf extends Creature { * @param target Creature to deal damage to * @throws LowManaException if mana value is too low */ - protected void castSpell(final Creature target) - throws LowManaException + public void castSpell(final Creature target) + throws LowManaException, RuntimeException { - if (this.mana < MANA_USAGE) { - throw new LowManaException("Not enough mana to cast spell"); + if(!this.isAlive()){ + throw new RuntimeException("The elf is not alive."); } + else{ + if (this.mana < MANA_USAGE) { + throw new LowManaException("Not enough mana to cast spell"); + } - this.mana -= MANA_USAGE; + this.mana -= MANA_USAGE; - target.takeDamage(MANA_DAMAGE); + target.takeDamage(MANA_DAMAGE); + } } /** diff --git a/src/code/ca/bcit/comp2522/lab02/HealingException.class b/src/code/ca/bcit/comp2522/lab02/HealingException.class new file mode 100644 index 0000000000000000000000000000000000000000..daa9a291469f70d60b00993cda577a58f6d2a3a3 GIT binary patch literal 262 zcmX^0Z`VEs1_nn4UM>bE24;2!79Ivx1~x_psjS4ZME#t^ymbAb(!7$)+*H?!?sbd7o- zn;8@t*ntiQ0Y)GMIvq%I0(nY6QII&BmiBfA#*JWkE+7f=CIbV=le|Ec+(0%H0}lWU C;yalD literal 0 HcmV?d00001 diff --git a/src/code/ca/bcit/comp2522/lab02/LowFirePowerException.class b/src/code/ca/bcit/comp2522/lab02/LowFirePowerException.class new file mode 100644 index 0000000000000000000000000000000000000000..8faa0637731f98f4198643528731f0270cf3d4bf GIT binary patch literal 265 zcmX^0Z`VEs1_nn4UM>bE24;2!79Ivx1~x_p;jF~6ME#t^ymWonisaOSlFa-(Mg}&U z%)HDJJ4Oak4IhMp;F6-uymV{LFm?tGMh2baME#`X%o6?N{M-T~QzIjwDMK^!Tpy7fSA zU{GXW2f7^u7=aMzdLYROy$+S?fzH-hE4fF#JL3=AND@&Z+I1KCUrJOISN BJkkIF literal 0 HcmV?d00001 diff --git a/src/code/ca/bcit/comp2522/lab02/LowManaException.class b/src/code/ca/bcit/comp2522/lab02/LowManaException.class new file mode 100644 index 0000000000000000000000000000000000000000..29398a31769d77dc100bf7a188e2f42c0bfec555 GIT binary patch literal 255 zcmX^0Z`VEs1_nn4UM>bE24;2!79Ivx1~x_p;jF~6ME#t^ymWonisaOSlFa-(Mg}&U z%)HDJJ4Oak4IhMp;F6-uymV{LFm?tGMh5ldME#`X%o6?N{M-T~QzIjwDMNT#tk=clAHGVuFk=B4_T<|d^Ug(N2B07bZh^Gl18Q{6H_98q+QdLWw_6dBlo wE(QTcAOyM@NOA&sNbE24;2!79Ivx1~x_p;jF~6ME#t^ymWonisaOSlFa-(Mg}&U z%)HDJJ4Oak4IhMp;F6-uymV{LFm?tGMh5ldME#`X%o6?N{M-T~QzIjwDM literal 0 HcmV?d00001 diff --git a/src/code/ca/bcit/comp2522/lab02/Orc.class b/src/code/ca/bcit/comp2522/lab02/Orc.class new file mode 100644 index 0000000000000000000000000000000000000000..e2e99b4e4d3f8432f98dd5cc61ab0f51224fa02b GIT binary patch literal 1772 zcmaJ>ZBtW67(ELi2{8nSppq5{3JAd(1<}@6ZAgNoO+qDz(sssf!cM$$b8qJ625bL7 z|3YW_t>a9;wll4CaHf9mkLvX7B|sd4U+(UC_Ib`d&&%$={~i1TAdN=>^a#YNGFhn_ zZnA3E8;i?}i%C;fZY(C(ooWC+fy*!Co}|sHCG)1KYSK(QwN_nOZtjPwYPg1NMM>um zBY>d5#k$;88QZE#_le;?=dS5W%WVpb&gJI|CuC*UF|1lDgkFTgIES!+|DItP?tSfg z+Hrm>ga`%$7}DOu0+VM5&p1lDEk_9~b)hMo5tWs$O6B!W1kQ&sf>D9tJ!u*{JlK#m zwI$6KIepAxtDjC6!?=Vo8XO)9h`f&cD2$IWE)b|Gw&X@Rgd#a-KuoA%2PrV!E(zA{9 z!`!p2bm4JMVBn~6UfR8EFeOKd>8BlD+B!k27nd((Hgd&WX;WMK1ctKdqUPIN+sKvI z)(cr}A7PIV?HUbgwElRdkkg2x=*!qUEZJbeuvDp4uPA3zR!mw3%C4;L7Gm0AiTO7*Rrx6&8rEb>L5kuXo@i@r@3AlX$TMFA?ABz{}+1%_p}HA^lJg z-zLiNB@xJyD!eTSzc!TH3(wefunjQajYG%@P?3l}u0paT%46L6FHFCmO&;@h;OFy1Da zrB1Ccl6Zqy;!j*SpaT4U9Pjr|A28+~K5ysr6NU89BcuI;zhdAmrnaM3_A&D=v8_$t zQ-VIz>GmgvCXV39YlIhuCf;E7Hv|{@xSnJ`i+)lT5k!e1AMwR3V+NaS)Dsr^DS!SA uR`8uy6+e8@OT)h7x=c~feMa}-0lwfG;VwtvY44lCSG4JexIpf&G4?-0YmRdO literal 0 HcmV?d00001 diff --git a/src/code/ca/bcit/comp2522/lab02/Orc.java b/src/code/ca/bcit/comp2522/lab02/Orc.java index 2331f35..20df0d0 100644 --- a/src/code/ca/bcit/comp2522/lab02/Orc.java +++ b/src/code/ca/bcit/comp2522/lab02/Orc.java @@ -40,7 +40,7 @@ public class Orc extends Creature { * @param health int * @param rage int */ - Orc( + public Orc( final String name, final Date dateOfBirth, final int health, @@ -59,8 +59,17 @@ public class Orc extends Creature { * @return details of orc */ @Override - protected String getDetails() { - return "Name: " + this.getName() + " Rage: " + this.rage; + public String getDetails() { + StringBuilder builder; + builder = new StringBuilder(); + builder.append("Is alive: " + this.isAlive()); + builder.append(" Name: " + this.getName()); + builder.append(" Date of birth: " + (this.getDateOfBirth()).getYyyyMmDd()); + builder.append(" Age: " + this.getAgeYears()); + builder.append(" Health: " + this.getHealth()); + builder.append(" Rage: " + this.rage); + + return builder.toString(); } /** @@ -71,21 +80,30 @@ public class Orc extends Creature { * @param target Creature to deal damage to * @throws LowRageException if rage value is too low */ - protected void bezerk(final Creature target) - throws LowRageException + public void berserk(final Creature target) + throws LowRageException, RuntimeException { - this.rage += RAGE_INCREMENT; + if(!this.isAlive()){ + throw new RuntimeException("The orc is not alive."); + } + else{ + + this.rage += RAGE_INCREMENT; + + if (this.rage > MAX_RAGE_VALUE) { + this.rage = MAX_RAGE_VALUE; + } + + if (rage < RAGE_INCREMENT) { + throw new LowRageException("Rage too low"); + } else if (this.rage > DAMAGE_THRESHOLD) { + target.takeDamage(DAMAGE * DOUBLE); + } else { + target.takeDamage(DOUBLE); + } - if (this.rage > MAX_RAGE_VALUE) { - this.rage = MAX_RAGE_VALUE; } - if (rage < RAGE_INCREMENT) { - throw new LowRageException("Rage too low"); - } else if (this.rage > DAMAGE_THRESHOLD) { - target.takeDamage(DAMAGE * DOUBLE); - } else { - target.takeDamage(DOUBLE); - } } + } diff --git a/src/tests/ca/bcit/comp2522/CreatureTest.java b/src/tests/ca/bcit/comp2522/CreatureTest.java new file mode 100644 index 0000000..4619a83 --- /dev/null +++ b/src/tests/ca/bcit/comp2522/CreatureTest.java @@ -0,0 +1,119 @@ +package ca.bcit.comp2522; + +import ca.bcit.comp2522.lab02.Creature; +import ca.bcit.comp2522.lab02.Date; +import ca.bcit.comp2522.lab02.Dragon; +import ca.bcit.comp2522.lab02.Elf; +import ca.bcit.comp2522.lab02.Orc; +import ca.bcit.comp2522.lab02.LowFirePowerException; +import ca.bcit.comp2522.lab02.LowManaException; +import ca.bcit.comp2522.lab02.LowRageException; + +/** + * A test driver for the Creature class and its subclasses. + */ +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 { + + final Creature creatureDragon; + final Creature creatureElf; + final Creature creatureOrc; + + 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); + + 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; + + + + 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."); + + // Fight sequence — all guarded by friendly exception handling + 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()); + } + + 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()); + } + 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.printDetails(); + elf.printDetails(); + orc.printDetails(); + } +}