CSC 241- Lab 2 (Due September 22nd 1999)


Theme ...

You will make changes in the state and behavior of Tank and test your changes. You will also write the pre/post conditions.


Create directories

  1. cd public-html/classes/csc241
  2. mkdir Tank
  3. chmod og+rx Tank
  4. cd Tank

Copy Tank, test programs, and a data file

I am assuming that you are in your Tank directory. You can use pwd at your unix prompt to see where you are.

  1. cp /csclass/mohammad/public-html/classes/csc241/Tank/* . -- don't forget the space followed by dot

Compile Tank and the test programs and run the programs

  1. javac *.java -- to compile Tank and test programs
  2. Briefly study the code for each test program, then run it.
    1. more testNormal1.java
    2. java testNormal1
    3. more testNormal2.java
    4. java testNormal2
      When prompted for a data file, enter test.dat
    5. more testOverflow.java
    6. java testOverflow
    7. more testUnderflow.java
    8. java testUnderflow
    9. more testIllegalParam.java
    10. java testIllegalParam

    Modify the Tank class

    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:

    Test Changes

    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.


    Write Pre/post conditions

    Here are the pre/post conditions for method add.
                       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 new method

    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.


    What to turn in

    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