public class Tank extends Object {
/** ~mohammad/public_html/classes/csc241/samples/Tank/Tank.java
This is a simple tank class. Every Tank object has a capacity of
5000.0 gallons. A tank Object is constructed as empty. We have
mutator method(s) that enable us to add water to a tank object
up to its capcity. You may also remove any portion of a tank's content.
We need an accessor method for the current content. The accesss to
a tank's content needs to be controlled so that it does not exceed
capacity or fall below empty.
**/
public static final double capacity=5000.0;
/**Each object of class Tank will have its own variable content_ **/
protected double content_;
public Tank() {
/** Construct a new Tank with 0 content.**/
content_ = 0.0;
}
public double content () {
/** Return current content of tank **/
return content_;
}
public void add(double amount) throws
TankOverFlowException, IllegalArgumentException {
/** Add amount to tank's content. If content_
would exceeds capacit, method throws TankOverFlowException and
the amount is not added. If amount is negative, method
throws IllegalArgumentException. **/
if (amount < 0.0)
throw new IllegalArgumentException();
double overflow = (amount+content_) - Tank.capacity;
if (overflow > 0.0)
throw new TankOverFlowException(this, overflow);
content_ +=amount;
}
public void remove(double amount) throws
TankUnderFlowException, IllegalArgumentException {
/** Remove amount from tank. If removing amount would cause
the content to go below capacity, method throws
TankUnderFlowException and content_ stays the same.
If amount is negative, this method throws an
IllegalArgumentException **/
if (amount < 0)
throw new IllegalArgumentException();
double underflow = amount - content_;
if (underflow > 0)
throw new TankUnderFlowException(this, underflow);
content_ -=amount;
}
public class TankUnderFlowException extends Exception {
/** An exception class designed for handling tank content falling
below empty **/
public Tank underflowTank;
public double underflow;
public TankUnderFlowException(Tank t, double amount) {
super(); underflowTank = t; underflow = amount;
}
}
public class TankOverFlowException extends Exception {
/** An exception class intended for when a tank overflows. **/
public Tank overflowTank;
public double overflow;
public TankOverFlowException(Tank t, double amount) {
super(); overflowTank = t; overflow = amount;
}
}
}