package lect2_hierarchy; import java.lang.String; import static utils.DomainConstraint.ZERO_PLUS; import utils.DomainConstraint; import utils.NotPossibleException; import utils.OptType; import utils.AttrRef; import utils.DOpt; /** * @overview A projectile that can be fired by tank unit in the game * * @attributes * damage Integer int * angle Integer int * radius Integer int * * @object * A typical Projectile is
pt = 
, where *
damage(d), angle(ang), radius(r)
* * @abstract_property * mutable(damage) = true /\ optional(damage) = false /\ min(damage) = 1 * mutable(angle) = false /\ optional(angle) = false /\ min(angle) = 0 * mutable(radius) = false /\ optional(radius) = false /\ min(radius) = 0+ * * @author namhv */ public class Projectile { @DomainConstraint(type="Integer", mutable=true, optional=false, min=1) private int damage; @DomainConstraint(type="Integer", mutable=false, optional=false, min=0) private int angle; @DomainConstraint(type="Integer", mutable=false, optional=false, min=ZERO_PLUS) private int radius; /** * @effects * if d, ang, r are valid * initialize this as * else * throws NotPossibleException */ public Projectile(@AttrRef("damage") int d, @AttrRef("angle") int ang, @AttrRef("radius") int r) throws NotPossibleException { if (!validateDamage(d)) { throw new NotPossibleException( "Projectile.init: Invalid Projectile damage: " + d ); } if (!validateAngle(ang)) { throw new NotPossibleException( "Projectile.init: Invalid Projectile angle: " + ang ); } if (!validateRadius(r)) { throw new NotPossibleException( "Projectile.init: Invalid Projectile radius: " + r ); } this.damage = d; this.angle = ang; this.radius = r; } /** * @effects * return damage */ @DOpt(type=OptType.Observer) @AttrRef("damage") public int getDamage() { return damage; } /** * @effects * return angle */ @DOpt(type=OptType.Observer) @AttrRef("angle") public int getAngle() { return angle; } /** * @effects * return radius */ @DOpt(type=OptType.Observer) @AttrRef("radius") public int getRadius() { return radius; } /** * @effects * if d is valid * set this.damage = d * return true * else * return false */ @DOpt(type=OptType.Mutator) @AttrRef("damage") public boolean setDamage(int d) { if (validateDamage(d)) { this.damage = d; return true; } else { return false; } } @Override public String toString() { return String.format("Projectile: <%d,%d,%d>", damage, angle, radius); } /** * @effects
     *      if this satisfies abstract properties
     *          return true
     *      else
     *          return false
     *      
*/ public boolean repOK() { if (!validateDamage(damage) || !validateAngle(angle) || !validateRadius(radius)) { return false; } return true; } /** * @effects
     *      if d is valid
     *          return true
     *      else
     *          return false
     *      
*/ private boolean validateDamage(int d) { if (d < 1) { return false; } return true; } /** * @effects
     *      if ang is valid
     *          return true
     *      else
     *          return false
     *      
*/ private boolean validateAngle(int ang) { if (ang < 0) { return false; } return true; } /** * @effects
     *      if r is valid
     *          return true
     *      else
     *          return false
     *      
*/ protected boolean validateRadius(int r) { if (r <= 0) { return false; } return true; } }