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 0000000..879edc3 Binary files /dev/null and b/Assignment11/bin/Witch.class differ diff --git a/Assignment11/bin/WitchGame.class b/Assignment11/bin/WitchGame.class new file mode 100644 index 0000000..09ec891 Binary files /dev/null and b/Assignment11/bin/WitchGame.class differ 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 ""; + }; + } +}