add method comments

This commit is contained in:
Siwat Sirichai 2020-11-10 14:17:32 +07:00
parent 6c9ea0fe19
commit 60ddc77b58
4 changed files with 95 additions and 1 deletions

Binary file not shown.

Binary file not shown.

View File

@ -1,14 +1,24 @@
public class Witch { public class Witch {
// Static Fields
private static int totalWitches = 0; private static int totalWitches = 0;
// Non-Static Fields
private String name; private String name;
private String school; private String school;
private int magicPower; private int magicPower;
// Static Constants
static final int MAX_LEVEL = 100; static final int MAX_LEVEL = 100;
static final int MIN_LEVEL = 0; static final int MIN_LEVEL = 0;
// Constructors // Constructors
/**
* This is the default constructor
*
* @category constructor
* @param None This constructor takes no parameter
*/
public Witch() { public Witch() {
this.name = "no one"; this.name = "no one";
this.school = "unknown school"; this.school = "unknown school";
@ -16,6 +26,14 @@ public class Witch {
Witch.totalWitches++; Witch.totalWitches++;
} }
/**
* Initialize this witch object with data from parameter
*
* @category constructor
* @param name The name of the witch
* @param school The school that the witch belong to
* @param magicPower The magicPower of the witch
*/
public Witch(String name, String school, int magicPower) { public Witch(String name, String school, int magicPower) {
this.name = name; this.name = name;
this.school = school; this.school = school;
@ -23,6 +41,12 @@ public class Witch {
Witch.totalWitches++; Witch.totalWitches++;
} }
/**
* Initialize this witch object with data from w
*
* @category constructor
* @param w Witch to clone from
*/
public Witch(Witch w) { public Witch(Witch w) {
this.magicPower = w.getMagicPower(); this.magicPower = w.getMagicPower();
this.name = w.getName(); this.name = w.getName();
@ -31,31 +55,74 @@ public class Witch {
} }
// Getters // Getters
/**
* Get the number of witch created
*
* @category getter
* @return totalWitches The total number of witch created
*/
public static int getTotalWitches() { public static int getTotalWitches() {
return Witch.totalWitches; return Witch.totalWitches;
} }
/**
* Get the name of the witch
*
* @category getter
* @return name name of the witch
*/
public String getName() { public String getName() {
return this.name; return this.name;
} }
/**
* Get the magic power of the witch
*
* @category getter
* @return magicPower The magic power of the witch
*/
public int getMagicPower() { public int getMagicPower() {
return this.magicPower; return this.magicPower;
} }
/**
* Get the school of the witch
*
* @category getter
* @return school The school of the witch
*/
public String getSchool() { public String getSchool() {
return this.school; return this.school;
} }
// Setters // Setters
/**
* Set the totalWitches field
*
* @category setter
* @param totalWitches The value of totalWitches to set
*/
public static void setTotalWitches(int totalWitches) { public static void setTotalWitches(int totalWitches) {
Witch.totalWitches = totalWitches; Witch.totalWitches = totalWitches;
} }
/**
* Set the name of the witch
*
* @category setter
* @param name The name of the witch to set
*/
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
/**
* Set the magic power of the witch
*
* @category setter
* @param magicPower the magic power to set (0<=magicPower<=100), numbers out of
* range will be rounded up/down to MAX_LEVEL and MIN_LEVEL
*/
public void setMagicPower(int magicPower) { public void setMagicPower(int magicPower) {
if (magicPower < MIN_LEVEL) if (magicPower < MIN_LEVEL)
magicPower = MIN_LEVEL; magicPower = MIN_LEVEL;
@ -64,15 +131,36 @@ public class Witch {
this.magicPower = magicPower; this.magicPower = magicPower;
} }
/**
* Set the school of the witch
*
* @category setter
* @param school The school that the witch belong to.
*/
public void setSchool(String school) { public void setSchool(String school) {
this.school = school; this.school = school;
} }
// Utilities // Utilities
/**
* Return the object as a formatted string
*
* @category utility
* @Override java.lang.Object.toString()
* @return WitchString The formatted string containing all field of Object Witch
*/
@Override
public String toString() { public String toString() {
return String.format("Witchname: %s, School: %s, Magic Power: %s", this.name, this.school, this.magicPower); return String.format("Witchname: %s, School: %s, Magic Power: %s", this.name, this.school, this.magicPower);
} }
/**
* Compare the power level of this witch to witch w
*
* @category utility
* @param w The second witch object
* @return intPowerCompare 1 if this>w, 2 if this==w, 3 if this<w
*/
public int compareTo(Witch w) { public int compareTo(Witch w) {
if (this.magicPower > w.magicPower) if (this.magicPower > w.magicPower)
return 1; return 1;
@ -83,6 +171,13 @@ public class Witch {
return 9999; // This statement should not be called return 9999; // This statement should not be called
} }
/**
* Increase the witch's magicPower
*
* @category utility
* @param increase a positive integer to increase the level of the witch by
* (this.magicPower+increase must be less than MAX_LEVEL)
*/
public void levelUp(int increase) { public void levelUp(int increase) {
if (increase < 0) if (increase < 0)
return; return;

View File

@ -15,7 +15,6 @@ public class WitchGame {
witch1.setName("Hermione Granger"); witch1.setName("Hermione Granger");
witch1.setSchool("Hogwarts"); witch1.setSchool("Hogwarts");
witch1.setMagicPower(85); witch1.setMagicPower(85);
//compareTo(Witch w) test //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%s%n%s%n", LINE_BREAK, "Printing the comparison", LINE_BREAK);
System.out.format("%s%n", parseCompareTo(witch1,witch2)); System.out.format("%s%n", parseCompareTo(witch1,witch2));