The 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 a test program.
You will initially get a copy of the Tank class and the test programs that were written for it. We will then make sure that you can correctly compile and execute Java programs. Your main task here will be to make some changes in the state and behavior of tank and to write a few of your own test programs to test those changes.
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.
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 .
-- don't forget the dot
javac Tank.java
-- to compile Tank
cd testTank
javac *.java
-- to compile all test programs
java test_simple_tank
-- to run test_simple_tank
Here is when you begin working.
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 and the state of content_. If you check the code, you
will notice that if our parameter is negative, we throw an exception. We
also throw an exception, if amount causes an overflow. 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.
public void remove(int amount) /** Precondition: Postcondition: **/
The Tank class has a static variable capacity. static final means all tanks will end up with same capacity and it can't change. We want to change that here, capacity should be either defaulted to 5000, or be given by the user of the class at the time of the object construction. Also, we no longer want to set initial content at the time of the construction.
In responding to the following, begin modifying the Tank class.
Use test_simple_tank as an example and write one or two test programs that ensure your changes worked correctly. You need to make sure that both the default constructor and the one that sets initial capacity work. Also, if you added a method to provide the value of capacity to the class user, you would need to test that method also.
Add a boolean method samePercentage to the Tank class that allows for comparing the fullness of two tanks. This method will have a Tank object as a parameter. The content/capacity of this parameter is compared against content/capacity of the object's own, if they are the same, the method shall return true, otherwise, it returns false. Keep in mind that content and capacity are both int, and dividing int variables results in another int, so, instead of getting, say, .67 when looking for a percentage, you will get 0. Convert them to float or double before dividing.
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
Note that there are two integer values on each line. The code that
I wrote in test_more_tank.java to extract an integer from an input
line won't work here. text = istream.readLine();
will read a line
from the data file and stored it in text. text is a
String variable. int am = Integer.parseInt(text);
converts the value in text to an integer. How can you extract two
integers from a string? Read the specifications for input carefully and
find the appropriate methods in the String Class that enable you to accomplish this.
The first number on a line indicate which tank. Assume there are only two tanks and that the first value is either 1 or 2. Also assume that this first value is always the first character on a line. The second value determines the amount that you will be adding to the specified tank. Once all data values are processed, you should use the samePercentage method to determine if both tanks are at the same fullness level and output an appropriate message. Assume that tank1 will have a capacity of 100 and tank2 a capacity of 1000. Test with the data given, also test to make sure that the method handles tanks with different fullness level correctly also.