Upload Assignment 11
This commit is contained in:
parent
1ff61e69ec
commit
27b840e0df
|
@ -0,0 +1,11 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="src" path="src"/>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-14">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="module" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="lib" path="class"/>
|
||||||
|
<classpathentry kind="output" path="bin"/>
|
||||||
|
</classpath>
|
|
@ -0,0 +1,17 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>Assignment11</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
|
@ -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
|
Binary file not shown.
Binary file not shown.
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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 "";
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue