Implement the ElasticBand class. A skeleton can be found here.
equals method generated by eclipse does not satisfy the API.
package test3;
/**
* This class represents an elastic band. Each elastic band
* has a length.
*
* @author Franck van Breugel
*/
public class ElasticBand
{
private double length;
/**
* Initializes this elastic band with the given length.
*
* @param length the length of this elastic band.
* @pre. length > 0
*/
private ElasticBand(double length)
{
this.setLength(length);
}
/**
* Returns the length of this elastic band.
*
* @return the length of this elastic band.
*/
public double getLength()
{
return this.length;
}
/**
* Sets the length of this elastic band to the given length.
*
* @param length the new length of this elastic band.
* @pre. length > 0
*/
public void setLength(double length)
{
this.length = length;
}
/**
* Returns an elastic band with the given length.
*
* @param length the length of the elastic band.
* @pre. length > 0
* @return an elastic band with the given length.
*/
public static ElasticBand getInstance(double length)
{
return new ElasticBand(length);
}
/**
* Tests whether this elastic band is the same as the
* given object. Two elastic bands are the same if
* their length differ by less than 0.01.
*
* @param object an object.
* @return true if this elastic band is the same as the
* given object, false otherwise.
*/
public boolean equals(Object object)
{
final double EPSILON = 0.01;
boolean equal;
if (object != null && this.getClass() == object.getClass())
{
ElasticBand other = (ElasticBand) object;
equal = Math.abs(this.getLength() - other.getLength()) < EPSILON;
}
else
{
equal = false;
}
return equal;
}
/**
* Stretches the length of this elastic band by the given factor.
* For example, if the length of this elastic band is 3 and it
* is stretched by a factor 1.5 then its length becomes 4.5.
*
* @param factor the stretch factor
* @pre. factor > 0 && factor < 5
*/
public void stretch(double factor)
{
this.setLength(factor * this.getLength());
}
}