This is mainly to review Java. However, the exception handling features of the language are also discussed here. I also want you to think about writing pre and post conditions and generating test cases for programs, so I have included things in this module for that purpose.
You will initially get a copy of the Tank class and the test programs that where 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 the tank class 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 means all tanks will end up with same capacity. We want to change that here. We want capacity to 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. I guess, it will be my job to see if you made the right changes.
Add a boolean method samePercentage to the Tank class that allows for comparing the fullness of two tanks. This method will have a parameter which is a Tank object and compares the content/capacity of the parameter against content/capacity of the object for which the method was invoked. If they are the same, retuns true, otherwise 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();
read a line
from the data file and stored it in text which 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 follow this link
for the definition of the String Class if you need to.
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.