From 27b840e0df342d3d1f9a2c9ed1852660f2cddc25 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Tue, 10 Nov 2020 11:56:36 +0700 Subject: [PATCH] Upload Assignment 11 --- Assignment11/.classpath | 11 ++ Assignment11/.project | 17 ++++ .../.settings/org.eclipse.jdt.core.prefs | 14 +++ Assignment11/bin/Witch.class | Bin 0 -> 2423 bytes Assignment11/bin/WitchGame.class | Bin 0 -> 2155 bytes Assignment11/src/Witch.java | 94 ++++++++++++++++++ Assignment11/src/WitchGame.java | 48 +++++++++ 7 files changed, 184 insertions(+) create mode 100644 Assignment11/.classpath create mode 100644 Assignment11/.project create mode 100644 Assignment11/.settings/org.eclipse.jdt.core.prefs create mode 100644 Assignment11/bin/Witch.class create mode 100644 Assignment11/bin/WitchGame.class create mode 100644 Assignment11/src/Witch.java create mode 100644 Assignment11/src/WitchGame.java diff --git a/Assignment11/.classpath b/Assignment11/.classpath new file mode 100644 index 0000000..70188ff --- /dev/null +++ b/Assignment11/.classpath @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/Assignment11/.project b/Assignment11/.project new file mode 100644 index 0000000..bf69837 --- /dev/null +++ b/Assignment11/.project @@ -0,0 +1,17 @@ + + + Assignment11 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/Assignment11/.settings/org.eclipse.jdt.core.prefs b/Assignment11/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..910a770 --- /dev/null +++ b/Assignment11/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,14 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=14 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=14 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=14 diff --git a/Assignment11/bin/Witch.class b/Assignment11/bin/Witch.class new file mode 100644 index 0000000000000000000000000000000000000000..879edc3c180165b614796d9a7f457064361252a0 GIT binary patch literal 2423 zcmaJ@ZF3W46g`{wgbhhwfd)$2rfs@GS``#%MJg1HrZ1RMs4pYibSZ&!x07z^SI4jZ z1wS|rGk)O%s71yZ{Q>?c$8(=uw%atCndW)B_n!Nld+w9|{m&nN0l0>{NklZn?={`V zqa^GDH|?!i4T;^x zBgbiJ$ZVTi&BlteZ?!d~7w7MPQ(IVDsA#o^&vS{x3{HJ;T20Z0Do={TwNQ0rs5-nXTj_+P5dRE?CMJy`y$VLXYa65?w z4LygNtJmZ=2No z)h>7aG8EG5joC0t&;aR%v%O=styL#nT6RscWj(c8cXz1mrrl^;<}M|hsXKe^hIP9s z`pqapF3Jrt!um-oBq%V-owJBYQjHX!8Yw-Fd66=$b+wn}nt~42Ipk=aM-O*;X)(@G zKJnqkm8I822VS(JKOe zL9oyL4|HD$x*z=nKSRJ_UT*@Y6|~`n_n`@-c$N!KN}Cg2X2NxpDV;*aPwZQs$l1)X z#9o1EO&FC|n2{VgUSgX6Mb1zt!vf=<#d(|~;xMK;6LJPUoFKy)yic0g*SJC(udXGI z@`(}VImd8W$5a%_Rg<^F_YhCujq7$#u)tOIG7w1wZ z@^lyHG_La3%DbopQE3ky5n4uZbctMEJ`%v|9SyUX^8;L`rErljL_vow;6=E?Zo8@? zc&sM^))T%iG<*;wv*pJcDEz>Vnk(ctpCOUooHGg(fP{{!?i!+azyxA+P2f7!{vq{$ zgL0e?K@9{@14?chEm=@R`17REqw57qofo*p3oL}9hA$v;7WQrgrKr(b*c|PhPyC7K zdQ`+vUypGp)z?KD6R&Zz{uQ!B}Reu3!jv_pOH1z)n=E<-D4H*Gw3>BtS|V0e1$Q59ZI4UBvE35 S@|OBZ#QWsGggRj(Sp65f9cya< literal 0 HcmV?d00001 diff --git a/Assignment11/bin/WitchGame.class b/Assignment11/bin/WitchGame.class new file mode 100644 index 0000000000000000000000000000000000000000..09ec89182575c9e43e94af5599a8f825bf33787e GIT binary patch literal 2155 zcmZ`)U3U{z6y3K;nrWDp8fYmXWdI9F(+?^5L0iza0qrp zbHkQuLpNvI8MADu_l7a(6|9QDk>pG8L=h~SmLo72yWfd1o(W@6AmsZ;Fn~S{CmAxg zv*T(wO&XF_$23yzuBlefC^*9cu~=uSPRmj}(*pzN!#LMQVER$TlSLgD@CFNZtK@Q^ z9&NdktK`ljgVHR{Xt*e#k5opS=1)V6rTadl*qAc*RF`BWf>Dfxkq|iM_n|$Jj&V$o zF$2=A&Kw6j0yp>yZ_hgxUjj<;hZVH@u$)Q(}M$RpksHBSPXt+%`Rb2!EOFBLT z)!oO{id^u$%+s)=qXt*md(!TDwo@pD7evFqX;4l@2X2=h50YGYNK7Nt#XSXkCJM@vt-4r3s70TY0u21>Fz8sh6}Z8GGqC2F|L z)iE`wr9||t8s14nXKJ{f=)YUT`yGisXis!2SWh(e9E*Mmg(I<#WNN&BwT2ZH-|!h5 zZ~Am3z0;nwfxH!6HZMt}y<2!jA#^d~-Y9Fz{lpn?=U;v}s%~H7?<#+%Um%|yWuebu?Zb^etf5lF z-cwwNe)$x`YE_Z)>S`$Z)z6F$uqqIJH=iRgaEW>2xPb)L`InNVP1P`1N8%&rEAIKo Y1-|Ayf{-y39GVOBd<%R-OBcTV50w1*YXATM literal 0 HcmV?d00001 diff --git a/Assignment11/src/Witch.java b/Assignment11/src/Witch.java new file mode 100644 index 0000000..2664a13 --- /dev/null +++ b/Assignment11/src/Witch.java @@ -0,0 +1,94 @@ +public class Witch { + private static int totalWitches = 0; + + private String name; + private String school; + private int magicPower; + + final int MAX_LEVEL = 100; + final int MIN_LEVEL = 0; + + // Constructors + public Witch() { + this.name = "no one"; + this.school = "unknown school"; + this.magicPower = 10; + Witch.totalWitches++; + } + + public Witch(String name, String school, int magicPower) { + this.name = name; + this.school = school; + this.magicPower = magicPower; + Witch.totalWitches++; + } + + public Witch(Witch w) { + this.magicPower = w.getMagicPower(); + this.name = w.getName(); + this.school = w.getSchool(); + Witch.totalWitches++; + } + + // Getters + public static int getTotalWitches() { + return Witch.totalWitches; + } + + public String getName() { + return this.name; + } + + public int getMagicPower() { + return this.magicPower; + } + + public String getSchool() { + return this.school; + } + + // Setters + public static void setTotalWitches(int totalWitches) { + Witch.totalWitches = totalWitches; + } + + public void setName(String name) { + this.name = name; + } + + public void setMagicPower(int magicPower) { + if (magicPower < MIN_LEVEL) + magicPower = MIN_LEVEL; + if (magicPower > MAX_LEVEL) + magicPower = MAX_LEVEL; + this.magicPower = magicPower; + } + + public void setSchool(String school) { + this.school = school; + } + + // Utilities + public String toString() { + return String.format("Witchname: %s, School: %s, Magic Power: %s", this.name, this.school, this.magicPower); + } + + public int compareTo(Witch w) { + if (this.magicPower > w.magicPower) + return 1; + else if (this.magicPower == w.magicPower) + return 0; + else if (this.magicPower < w.magicPower) + return -1; + return 9999; // This statement should not be called + } + + public void levelUp(int increase) { + if (increase < 0) + return; + if (this.magicPower + increase > MAX_LEVEL) + this.magicPower = MAX_LEVEL; + else + this.magicPower += increase; + } +} diff --git a/Assignment11/src/WitchGame.java b/Assignment11/src/WitchGame.java new file mode 100644 index 0000000..bcb2b3a --- /dev/null +++ b/Assignment11/src/WitchGame.java @@ -0,0 +1,48 @@ + +public class WitchGame { + static final String LINE_BREAK = "===================="; + + public static void main(String[] args) { + //Object Creation and Initialization + Witch witch1 = new Witch(); + Witch witch2 = new Witch("Akko", "Luna Nova", 25); + + //toString() method test + System.out.format("%s%n%s%n%s%n", LINE_BREAK, "Printing 2 witches", LINE_BREAK); + System.out.format("%s%n%s%n", witch1.toString(), witch2.toString()); + + //Getters and Setters test + witch1.setName("Hermione Granger"); + witch1.setSchool("Hogwarts"); + witch1.setMagicPower(85); + + //compareTo(Witch w) test + System.out.format("%s%n%s%n%s%n", LINE_BREAK, "Printing the comparison", LINE_BREAK); + System.out.format("%s%n", parseCompareTo(witch1,witch2)); + + //Copy Constructor test + Witch witch3 = new Witch(witch1); + witch3.levelUp(80); + System.out.format("%s%n", parseCompareTo(witch2,witch3)); + + //Print all witch + System.out.format("%s%n%s%n%s%n", LINE_BREAK, "Now, printing all witches.", LINE_BREAK); + System.out.format("%s%n%s%n%s%n",witch1.toString(),witch2.toString(),witch3.toString()); + + //Print Total witches + System.out.format("%s%n%s%n%s%n", LINE_BREAK, "Now, printing total number of witches", LINE_BREAK); + System.out.println(Witch.getTotalWitches()); + } + static String parseCompareTo(Witch witch1,Witch witch2) { + return switch (witch1.compareTo(witch2)) { + case 1: + yield witch1.toString()+" has higher magic power."; + case 0: + yield "2 witch have equal magic power."; + case -1: + yield witch2.toString()+" has higher magic power."; + default: + yield ""; + }; + } +}