Comp201: Principles of Object-Oriented Programming I
Spring 2008 -- HW 02   

See Schedule Page for due date!

Save all of your work in a directory called HW02 on your U: drive (Owlnet account) inside a directory called Comp201 .

You must use at least Advanced Language Level for this assignment.    Set DrJava to at least "Advanced Language Level" before starting the assignment.

  1. (35 pts total) Write a JUnit test class called Test_MathFuncs that tests the MathFuncs class from HW01.   This problem is a refinement of the testing that you did in HW01.
    1. Copy your MathFuncs class from your HW01 folder to your HW02 folder.  Do a "File/Save As..." to save the file at the proper language level.
    2. Add a (private) field of type double called _tol to Test_MathFuncs. Initialize its value to 1e-15. Use this variable for all the tolerance values needed below (See the Connexions page on Unit Testing with DrJava for more information on how to use tolerances to compare floating point values).
    3. (10 pts) Write a test method called test_add that tests at least the following inputs (use multiple assertEquals statements and be sure that your error strings are meaningful--i.e. indicative of what is being tested!):
      1. 0+0
      2. 0 + a non-trivial value
      3. a positive value + a positive value
      4. pi + -pi (i.e. adding two non-trivial doubles to get zero)
      5. a postive non-trivial value + a negative non-trivial value
    4. (10 pts) Write a method called test_sqr that tests at least the following inputs
      1. 0
      2. a non-trivial positive value whose square is known exactly
      3. a non-trivial negative value whose square is known exactly
      4. pi
      5. an integer
    5. (10 pts) Write a method called test_sumsqr that tests at least the following
      1. 0 & 0
      2. 0 & a non-trivial value
      3. a positive value & a positive value
      4. pi & -pi
      5. a postive non-trivial value & a negative non-trivial value
    6. (5 pts) Adjust the value of _tol to find the minimum power of ten that will enable all of the tests to pass. That is, try 1e-12, 1e-13, 1e-14, 1e-15, 1e-16, 1e-17, etc.
  2. (20 pts total) Write and test an interface called IShape.
    1. (10 pts) Create a new interface called IShape with an abstract method called getArea() that take no input parameters and returns a double.
    2. (10 pts) Copy your Rectangle, Triangle, Circle and Square classes from your HW01 folder into your HW02 folder.  Do a "File/Save As..." for each file to convert them them all to the new language level.     
      1. Advanced Language Level (actually Intermediate and above) require that you write the constructor for the class explicitly.   That is, if you want to initialize a field of a class with a value supplied when you instantiate and instance of that class, you need to write something like the following:

        public class MyClass {
           private double _x;
           public MyClass(double x) {		
        	   _x = x;
           // rest of the code

        You will need to add constructors to your various shape classes.

      2. Modify each of them to implement IShape.   You will need to also make each getArea() method public.
      3. Test them using the following file: Test_IShape.dj2 ( for Full Java users)  download into your HW02 folder.  Examine the test code closely to see how a variable of type IShape can refer to different kinds of concrete shapes and give the proper area for each of them. This is polymophism in action!
  3. (35 points total) Write and test a class called Pizza
    1. Pizza has the following specification:
      1. a Pizza has a double which is the price of the pizza in dollars and has an IShape representing its shape.
      2. Pizza should have a method called getPricePerSqInch that takes no parameters and returns the dollars per square inch for that pizza.
    2. (25 pts) Write the test class, Test_Pizza, that tests Pizza given a complete set of combinations of prices and shape and areas. Be sure to test the cases where the values are zero! Write separate test methods for each shape tested.
    3. (10 pts) Write the above getPricePerSqInch method..
    4. Make sure all your test run and that they completely test your Pizza in all possible manners. See below for hints.



90 pts total.

When you have completed your homework, zip up the entire HW02 directory and submit the zipped file to your Comp201 WEBCT account.

Grading Criteria

  1. 35 pts total:
    1. 2 points for each of i-v
    2. 2 points for each of i-v
    3. 2 points for each of i-v
    4. 3 points if all tests pass, 2 points if no lower power of ten allows all to pass
  2. 20 pts total:
    1. 3 points for IShape being there, 4 points for IShape actually being an interface, 3 points for correct declaration of getArea()
    2. 2 points for each of Rectangle,Triangle,Square,Circle to implement IShape correctly, 2 points for passing all the test cases
  3. 35 pts total:
    1. 4 pts for each type of Pizza being specifially tested (16 pts total, 4 for each shape),
    2. 4 pts for having the correct fields.
    3. 5 pts for testing for Infinity and/or NaN somewhere.
    4. 5 pts for returning the correct output, 5 pts if it does so by calling shape.getArea()


Last Revised Thursday, 03-Jun-2010 09:50:22 CDT

©2008 Stephen Wong and Dung Nguyen