package edu.columbia.cs.cs1007.comics;
import java.util.Random;
/**
* This class implements a supernatural being.
* @author Julia Stoyanovich (jds2109@columbia.edu)
* COMS 1007, Summer 2009
*
*/
public class SuperBeing implements Comparable<SuperBeing>{
private String _name;
private int _strength;
private boolean _isAlive;
/**
* Constructor.
* @param name
*/
public SuperBeing(String name) {
_name = name;
_isAlive = true;
Random rand = new Random();
_strength = 1 + rand.nextInt(10);
}
/**
* Accessor.
* @return name
*/
public String getName() {
return _name;
}
/**
* Accessor.
* @return true if the being is alive, false otherwise
*/
public boolean isAlive() {
return _isAlive;
}
/**
* Accessor.
* @return the strength of the supernatural being
*/
public int getStrength() {
return _strength;
}
/**
* Convey your message to the world.
* @return message
*/
public String speakUp() {
return "I am " + _name + ", I have a strength of " + _strength;
}
/**
* Farewell, cruel world.
* @return Parting message
*/
public String die() {
_isAlive = false;
return "RIP " + _name;
}
/**
* A method that compares two supernatural beings by strength,
* and breaks ties by name.
*/
public int compareTo(SuperBeing that) {
int strengthComp = 0;
if (this._strength < that._strength) {
strengthComp = -1;
} else if (this._strength > that._strength) {
strengthComp = 1;
}
return strengthComp != 0 ? strengthComp : this._name.compareTo(that._name);
}
/**
* Overriding the equals method of the Object class.
* Make sure that whenever this.equals(that) then also this.compareTo(that) == 0!
* @return true if two SuperBeings are the same, false otherwise.
*/
public boolean equals(Object other) {
if ((other == null) || (!(other instanceof SuperBeing))) {
return false;
}
SuperBeing that = (SuperBeing) other;
return this._name.equals(that._name) && (this._strength == that._strength);
}
/**
* Overriding the hashCode method of the Object class.
* Must agree with the equals method: equal objects must have
* equal hash codes!
* @return hash code
*/
public int hashCode() {
return _name.hashCode() + _strength;
}
}
|