Exception handling features of Java are presented in this lab along with a review of other language features. You will also learn to write pre and post conditions and develop test programs.
You will initially copy the Tank class, its supporting exception classes, along with the test programs that have been written for it. You will make changes in the state and behavior of Tank and write the pre/post condition for one its methods. You will also write test programs to test the new version of tank.
cd public-html/classes/csc241
mkdir Tank
chmod og+rx Tank
cd Tank
mkdir testTank
-- you don't need the protection
changed.
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/*.java .
-- don't forget the dot
cp /csclass/mohammad/public-html/classes/csc241/Tank/testTank/*.java testTank
cp /csclass/mohammad/public-html/classes/csc241/Tank/testTank/test.dat testTank
javac Tank.java
-- to compile Tank
cd testTank
javac *.java
-- to compile all test programs
java test_simple_tank
-- to run test_simple_tank
public void add(int amount) /** Precondtion: amount>=0 && (amount+content_) <= capacity Postcondition: content_ == content_~+amount **/
In the precondition we are specifying what we consider valid for our
parameter, our assumptions. 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 you check the code, 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 done.
Write the pre/post conditions for remove.
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 state variable. 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 (similar to how content_ is handled now). Also, we no longer want to set the initial value of content_ at the time of the construction, we want all contents to start as zero.
The following are the things that you need to do:
Use test_simple_tank as an example and write a 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 instead of their contents.
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 content_ and capacity are both int, you must cast them as float before dividing. Assuming a variable, say, x is an int, (float)x casts it as float in an expression. Also note that if you have a parameter t1 for the samePercentage method, to refer to its capacity, you may use t1.capacity_. Similarly, you may refer to t1's content by using t1.content_.
Use test_more_tank.java as a model and write a test program for this
new method. Also create a data file, initially, with the following values:
1 23
2 230
1 13
2 130
1 10
2 100
Your test program must first constructs two tank objects, say, t1 and t2, t1 with capacity of 100 and t2 with 1000. Each line of input is read, and depending on the first character on the line, you will either add 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, you should use the Tank.samePercentage method to determine if t1 has the same content/capacity ratio as t2 and output a message appropriately.
Note that there are two integer values on each line. The code that I wrote in test_more_tank.java won't work as it only extracts one integer value from an input line.
In test_more_tank.java, The statement text = istream.readLine(); reads a line of input from the data file and stores it in a String variable text. int am = Integer.parseInt(text); converts the value in text to an integer.
You need to extract two integers from a string, look in String Class Specification to find the appropriate methods for accomplishing this. You need one method that gets you the first character for comparing against '1' or '2' (this determines which tank is to be added to). You also need the appropriate substring method to extract the second value and convert it to an integer in order to add it to either tank.
Test with the data given, also test to make sure that the method correctly handles tanks with different fullness level.
Email your .java files (Tank.java, your versions of test_simple_tank.java and test_more_tank.java) as attachments to me. Be sure to include the pre/post condition for remove in your mail. DO NOT SEND .CLASS FILES