CS 1054: Lab 10

Lab meeting 10: Testing

The purpose of this lab is to give you more hands-on experience in testing.
  1. This exercise will introduce you to code testing. Testing is an attempt to minimize the errors in our programs. It is one of the most important steps in developing software and yet, among students, it is one of the areas where the least amount of time is spent. While testing does not guarantee that your code is error free, it does provide some measure of confidence in the software. 
  2. In this lab, you will be testing to discover three types of programming errors:
    • COMPILE TIME – errors occurring at compile or program translation time. These errors range from omitting a semicolon (syntax errors) to using an uninitialized variable (semantic error).
    • RUN TIME – errors occurring during execution of a program. Often these errors appear as Java exceptions that halt execution of the program. An example is a null pointer exception such as java.lang.NullPointerException.
    • LOGIC – errors occurring in the logic of the program. These errors may or may not cause visible problems such as exceptions. In fact, the program may run to completion but still contain logic errors. Usually these errors are first detected when output from the program differs from the expected values.
  3. To begin this lab, open BlueJ and then start a new project. Name it "lab10". Note: Please follow the next two instructions carefully.
  4. Create two new classes in your project called Time and TestTime. Do not open these classes yet.
  5. Download the files Time.java and TestTime.java from the course web page using the following location: http://courses.cs.vt.edu/~cs1054/fall2003/labs/lab10/ in the same directory where you have your project stored.
  6. Now try to compile the Time class. You will run into a few errors. The errors are basic errors that you should be able to fix after reading the error messages. Each of the errors results from a simple syntax error. Carefully examine the code containing the error and look for missing characters or improper use of operators.
  7. After you fix the errors in the Time class, try to compile the TestTime class. You will get an error saying
    cannot resolve symbol
    symbol :method testDriver () location: class Time.testDriver();
  8. This error appears because the testDriver() method is missing from the Time class. Follow the instructions below to write this method and prepare your project for testing.
  9. You can have a method containing your test code in the same class that you are testing. To do this, you need to add a static method called testDriver() to the Time class. The example below shows the skeleton for this method.
    public static void testDriver(){ /* test code here */ }
  10. NOTE: You should not add any code to the TestTime class. Remember that static methods can be called using the class name instead of a reference variable. As an example, consider the call to testDriver() in the TestTime class:
    Time.testDriver();
  11. Now you are ready to begin adding test code to your testDriver() method. Each step below calls a particular method in the Time class in order to verify the code is working correctly. Unless you understand what the method is supposed to do, you will not be able to verify the output is correct. Whenever you are unsure of the purpose of a method, ask your TA for assistance.
  12. Time()
    • Create a new instance of the Time class by calling the constructor and passing in 11 hours, 15 minutes, and “am”.
    • Create a second instance and pass in 10 hours, 43 minutes, and “pm”.
    • Call the print methods on these objects to verify that the correct data has been entered. What problem do you see?
    • To correct the logic error revealed above, examine the code in Time() (the constructor) and the print() method. You may want to use your debugger.
  13. addDuration()
    • Call the addDuration() method of both objects and pass in a time of 4 hours and 15 minutes to be added to the time. Notice that the addDuration() method returns a reference to new Time object with the requested duration added. The original object remains unchanged. Therefore, you need to store the returned reference in a new Time reference variable. The code below shows an example of how to do this.
      Time newTime = oldTime.addDuration(4, 15);
    • Call the print method on both of the references returned by the calls to addDuration(). What problem do you see?
    • To correct the logic error revealed above, examine the code in the addDuration() method. Use your debugger to watch the value of the instance variable totalMinutes.
    • HINT: Remember that there are only 1,440 minutes in one day (24 hours x 60 minutes = 1440 minutes).
  14. isBefore() and isAfter()
    • To test the isBefore() and isAfter() methods, you must test four cases. To illustrate, consider the two Time objects created in part a.
      Time t1 = new Time(11, 15, "am");
      Time t2 = new Time(10, 43, "pm");
      The four cases you need to consider are these:
      t1.isBefore(t2);   // should return true
      t2.isBefore(t1);   // should return false
      t1.isAfter(t2);    // should return false
      t2.isAfter(t1);    // should return true
    • You can display the return value by storing it in a boolean variable and using the System.out object to display the variable.
    • Add the code described above and run your program. What problem do you see?
    • By comparing your output with the expected output, you should be able to determine which method contains the logic error. Examine the code in this method and determine what is wrong.
  15. Show TA the errors found as well as the testDriver() method you created.

© Mir Farooq Ali 2003.