You will make changes in the state and behavior of Tank and test your changes. You will also write the pre/post conditions.
cd public-html/classes/csc241
mkdir Tank
chmod og+rx Tank
cd Tank
I am assuming that you are in your Tank
directory.
You can use pwd at your unix prompt to see where you are.
cp /csclass/mohammad/public-html/classes/csc241/Tank/* .
-- don't forget the space followed by dot
javac *.java
-- to compile Tank and test programs
more testNormal1.java
java testNormal1
more testNormal2.java
java testNormal2
more testOverflow.java
java testOverflow
more testUnderflow.java
java testUnderflow
more testIllegalParam.java
java testIllegalParam
Currently, capacity is public static final which means there is only one constant capacity for all tanks and it is accessible in applications that use Tank. If we wish for each tank to have a different capacity, we need to change capacity into a field like content_. We want to either set capacity to a default value of 5000 or let its value be specified via a parameter at the time of construction.
The following are the things that you need to do:
Use testNormal1 as an example and write a simple test program that ensure your changes worked correctly. You need to make sure that both the default constructor and the one that sets initial capacity work. Use the capacity method after constructing the two tanks and output their capacities as well as their contents.
public void add(double amount) /** Precondtion: amount>=0 && (amount+content_) <= capacity Postcondition: content_ == content_~+amount **/
In the precondition we are specifying what we consider valid for our
parameter (i.e. what are our assumptions about them, inorder for the method
to behave correctly). In the avove precondition, amount>=0
indicates that the amount parameter must be zero or more;
(amount+content_) <= capacity indicates that if amount
is added to content_, the total must not go over capacity_.
If either condition is not met, we throw the corresponding exception.
In the Postcondition, content_~ refers to the old value of
content_, and it simply states that the new value
must be equal to the old value plus amount when this method is executed.
Add a boolean method samePercentage to the Tank class that allows for comparing the fullness of two tanks. This method will be static with two Tank objects as parameters. The content/capacity of the tanks are to be compared, if they are the same, this method will return true, otherwise, it must return false. Note that if you have a tank parameter t1 for the samePercentage method, to refer to its capacity, you may use t1.capacity_; to refer to its content, you may use t1.content_. The same thing holds, for your tank parameter t2Test samePercentage
testSamePercentage.java is similar to testNormal2.java, but it tests your samepercentage method. testSamePercentage.dat is a sample data file for you to use as well. This test program constructs two tanks, t1 and t2. t1 has a capacity of 100 and t2 1000. Each line of input is read, and depending on the first character on the line, the program either adds the second value to t1 or to t2 (if first character is 1, add to t1, otherwise to t2). Once all input lines are processed, the result of Tank.samePercentage is output.
Test with the data given, also test to make sure that the method correctly handles tanks with different fullness level.
Email me Tank.java as an attachment. The changes due to capacity, the pre/post condition for remove, and the samePercentage method must be present in this file. DO NOT SEND .CLASS FILES