Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Wiki Markup
h1.  Lab 06 \- Simple Java Classes and JUnit

...

 Testing

h2.  1. Writing Simple Java Classes \(45 min\)

...



*First steps:

...

1. If you have not done it already, download the latest DrJava to your Desktop. Use this link!

2. Create a Comp211 directory in your U: drive (or My Documents--same place)

3. Create a "lab02" directory in your Comp211 directory. Do the same sort of thing when doing homework, etc.

4. The following is due to a bug in the current DrJava and will not be necessary in the future:

  • Copy your entire Comp211 directory to the C:\temp directory -- DrJava will not run properly when the files are saved to your U: drive.
  • At the end of lab, copy your Comp211\lab02 directory back to your U: drive and delete it off of the C: drive!

5. Set your DrJava "Language Level" to "Elementary"

Creating a class to calculate rectangle areas in DrJava

We begin the process of writing a class that knows how to compute the area of any rectangle given a width and a height. We will write the class in several small steps. At each step, we will compile the code to ensure that everything is syntactically correct. By compiling the code at each small step, we hope to avoid seeing a large number of error messages that can be rather intimidating.

1. In the Definitions pane (upper right pane), type the following:

Code Block
*

# If you have not done it already, download the latest DrJava to your Desktop.  [Use this link!|http://drjava.org.}
# Create a {{lab07}} directory within your Comp211 directory.
# Set your DrJava "Language Level" to "Elementary"
# Create a class to calculate rectangle areas in DrJava by following the steps below. We will begin the process of writing a class that can compute the area of any rectangle given a width and a height.  We will write the class in several small steps.  At each step, we will compile the code to ensure that everything is syntactically correct.  By compiling the code at each small step, we hope to avoid seeing a large number of error messages that can be rather intimidating.
# *In the Definitions pane \(upper right pane\), type the following: 
{code}
class AreaCalc { 
}
{code}
Note the placement of the curly braces: the opening brace is on the same line as the class name, while the closing brace lines up with the beginning of the class definition on a new line.  This is the de-facto Java coding style.

...


This class is not very interesting because we cannot

...

 do much with it.  However, it is

...

2. Save the file in your Comp211\lab02 folder. The file will be saved as a ".dj0" file, which is a special Elementary level file.

3. Click the "Compile All" button in DrJava. If all is well, the file should compile with no errors.

4. In the Interactions pane (bottom pane), type in the following:

Code Block
 syntactically correct and can
be compiled.
* Save the file in your Comp211\lab07 folder under the default name AreaCalc.  he file will be saved as  a {.dj0} file, which is a DrJava Elementary level file. I
* Click the "Compile All" button in DrJava.  The file should compile with no errors.
* In the Interactions pane \(bottom pane\), type the following:
{code}
AreaCalc calc = new AreaCalc();

This defines a variable called "calc" of type AreaCalc and initializes it to refer to a new object made by your AreaCalc class definition.

5. There isn't much you can do with this class yet, but that doesn't mean that it is non-interacting. In the Interactions pane, type "calc" and hit Enter. This action will call the toString method of the object referred to by calc. All objects in Java have a toString method and this one is no exception. What you will get back is the current String representation of the object. Elementary language level will return something like "AreaCalc ()" while if you wrote in Full Java language level it will return something more cryptic.

6. Take a look at your Comp211\lab02 directory and you will see several new files.

  • The compiler automatically created a file called AreaCalc .class that is the compiled code, now in "Java bytecode", ready for the Java Virtual Machine (JVM).
  • DrJava's Elementary language level has automatically generated the "real" Java file, AreaCalc .java which has all the full-blown Java code in it that we don't want you worrying about at this stage. Feel free to open the .java file in DrJava. Close it when you are done so as not to confuse anything.

The purpose of this step is to give you some idea of what DrJava is doing "behind the scene". You do not have to concern yourself with all the code that is automatically generated at all.

7. Now you are ready to add to AreaCalc a method to compute the area of a rectangle. Edit the above AreaCalc class as follows:

Code Block

{code}
This code defines a variable called {{calc}} of type {{AreaCalc}} and binds it to a new object belonging to class
AreaCalc.
* There isn't much we can do with this class yet, but we can interact with it.   In the Interactions pane, type {{calc}} and hit Enter.  This action will print the string reprentation of object referred to by {{calc}}.  In the DrJava Elementary language level, the string representation of an {{AreaCalc}} object is {{"AreaCalc \(\)"}}. 
Conventional Java would return something much more cryptic.
* Take a look at your {{Comp211/lab07}} directory and you will see several new files.  
* The compiler automatically created a file called {{AreaCalc.class}} containing the compiled code expressed as "Java bytecode" ready execution on the Java Virtual Machine \(JVM\).
* DrJava's Elementary language level has automatically generated the "real" Java file, AreaCalc .java which has all the full-blown Java code in it that we don't want you worrying about at this stage.   Feel free to open the .java file in DrJava.    Close it when you are done so as not to confuse anything.

The purpose of this step is to give you some idea of what DrJava is doing "behind the scene".  You do not have to concern yourself with all the code that is automatically generated at all.

7. Now you are ready to add to AreaCalc a method to compute the area of a rectangle.  Edit the above AreaCalc class as follows:

{code}
class AreaCalc { 
    double rectArea(double width, double height) {
        return width * height;
    }
}
{code}

8. Compile All: the AreaCalc file is automatically saved.

...



9. To create a new instance of an AreaCalc , type the following in the Interactions pane:

...



{code
}AreaCalc calc = new AreaCalc();{code}

To "ask" the AreaCalc object calc to compute the area of a 4 by 5 rectangle, type in

...



{code
}calc.rectArea(4, 5){code}

11. Experiment with using the object calc to compute the area of a few rectangles of different sizes.

...



12. How do you instantiate another AreaCalc and name it robot?

...



*Computing the area of a right triangle

...

*

Suppose you want to compute the area of a right triangle given its base and its height

...

.  What code should you write?  You basically have two options.

...



13. Write another class called AreaCalc2 with a method to compute the area of a right triangle given as input parameters the base and the height.

...



14. Add a method to class AreaCalc to compute the area of a right triangle given as input parameters the base and the height.

...



Now, suppose now you are asked to compute the area of a circle.  What approach would you choose?  What are the pros and cons of each of the possible approaches?

...



*Creating classes with fields

...

*

The following are classes with fields similar to class Entry discussed in lecture #18.

...



15. Write a class called Rectangle that has two double fields, width and height, and a method to compute the area.

...



16. Write a class called RightTriangle that has two double fields, base and height, and a method to compute the area.

...



17. Write a class called Circle that has one double field, radius, and a method to compute the area

...

.

h2.  2. Unit Testing with JUnit \(35 min\)

...



Extreme Programming \(XP\) is a software development methodology that is "very hot" these days and is currently embraced by the software industry.  One key element of XP, called "unit testing", is the idea of writing test code for

...

 functions \(or procedures or object methods\) before the functions \(or procedures or object methods\) are even written.  \(Don't ask me how to write test code for the test code themselves!  The whole thing is "kinda" suspiciously recursive with no well-defined base cases.\)

...

 
 
_The test code becomes in effect the "documentation and specification" of the behavior of the functions \(or procedures or object methods\) in code form!

...

_

Each time a function \(or procedure or object method\) is modified, it must pass its existing test code.  Each time a test code for a function \(or procedure or object method\) is modified, the corresponding

...

 function \(or procedure or object method\) may have to be revised to satisfy the new specification.

...



[JUnit

...

|http:-www.junit.org

...

] \(www.junit.org\) is an open source framework developed to support the above concept of unit testing for Java programs.  This tutorial will lead you through a simple example of how to write unit test code in developing a \(simple\) Java program with only one class.

...



*Step 0.

...

*

Run DrJava with Elementary Language level.  We will do all development in DrJava since it has very nicely integrated JUnit with its development environment.

...

 

*Step 1.

...

*

Suppose we want to model the notion of a smart person who knows his/her birthday and can compute the number of months till his/her next birthday given the current month.  For our purpose, a month can simply be represented by an integer, which in Java  is called int.  We start by writing "stub" code for the class Person  that is to represent our smart person.

...


{code
}   
class Person {

    /**
    * Computes the number of months till the next birthday given the current month.
    */
    int nMonthTillBD(int currentMonth) {
        // to do
    }

}
{code}

Notice in the above there is really no concrete code.  As a matter of fact, the above would not compile.  Now we must abandon everything and start writing test code for nMonthTillBD\(...\).

...

 

*Step 2.

...

*

DrJava is very nice to you and will create a test stub class for you if you know what to click:

...

  
* Go to the File menu and select New Junit Test Case...

...


* Create a stub test case called Test_Person.  DrJava will create a stub class that looks like the following

...


{code

...

}
/**
 * A JUnit test case class.
 * Every method starting with the word "test" will be called when running
 * the test with JUnit.
 */
class Test_Person extends TestCase {
    /**
     * A test method.
     * (Replace "X" with a name describing the test.  You may write as
     * many "testSomething" methods in this class as you wish, and each
     * one will be called when running JUnit over this class.)
     */
    void testX() {
    }
}
{code}

...

At this point, do not worry about what "extends TestCase" means in the above.  DrJava is re-using the class TestCase from the JUnit framework in some clever way without you having to deal with all the details of how to use an existing class from some other files.

...



* Compile this test class only \(by clicking on Tools/Compile current document\), and save the class in the same directory as Person.  The Test_Person class should compile, though the Person class does not.

...



* Change the name of the stub method testX\(\) in the above to test_nMonthTillBD\(\)and add code  to make it look like the code below.

...


{code

...

}
class Test_Person extends TestCase {

    void test_nMonthTillBD() {

        Person peter = new Person(9); // a person born in September.

        assertEquals("Calling nMonthTillBD(2).", 7, peter.nMonthTillBD(2));
        assertEquals("Calling nMonthTillBD(9).", 0, peter.nMonthTillBD(9));
        assertEquals("Calling nMonthTillBD(12).", 9, peter.nMonthTillBD(12));
    }
} 

...

{code}
Note that in the code for test_nMonthTillBD\(\), we have decided that we only need to know the birth month of a person in order to compute the number of months till the next birth day.  As a result, we instantiate a Person object by passing only the birth month: new Person\(9\).

...

  

Also the test covers three cases:

...


* the current month is less than the birth month

...

 
* the current month is equal to the birth month

...


* the current month is greater than the birth month.

...



The

...

 {code
assertEquals

method comes from the class TestCase and takes in three parameters:

  • the first parameter is a String
  • the second parameter is the expected result
  • the third parameter is the actual result of the computation you are testing.

Most of the time, your test code will call on the assertEquals method to test for equality between the result of the computation you are testing and the expected result.

When you compile the above code in DrJava, it won't compile. You will have to go in and fix the code for Person to make the test code compile.

Step 3.

Fix the code for Person until Test_Person compiles! (See the code below.)

First add a int field called _bMonth and compile. What happens?

Now add the statement return 0; to the body of the method nMonthTillBD(...) and compile.

Code Block
}assertEquals{code} method comes from the class TestCase and takes in three parameters:
* the first parameter is a String
* the second parameter is the expected result
* the third parameter is the actual result of the computation you are testing. 

Most of the time, your test code will call on the assertEquals method to test for equality between the result of the computation you are testing and the expected result.

When you compile the above code in DrJava, it won't compile.  You will have to go in and fix the code for Person to make the test code compile.

*Step 3.*

Fix the code for Person  until Test_Person compiles!  \(See the code below.\)

First add a int field called _bMonth and compile.  What happens?

Now add the statement return 0; to the body of the method nMonthTillBD\(...\) and compile.
{code}
class Person {
    int _bMonth;

    /**
    * Computes the number of months till the next birthday.
    */
    int nMonthTillBD(int currentMonth) {
        return 0; // to do
    }
}

...

{code}

*Step 4.

...

*

After you have cleaned up your code for Person as shown in the above, you should be able to compile Test_Person.  With Test_Person open, click on the Test button in DrJava tool bar.

...



What do you see?  Something has failed!  The formula for the number of months till the next birth day seems to be the culprit.  We will pretend ignorance and fix the "bug" in two steps.

...



*Step 5.1

...

*

Change the formula to

...


{code
}
    int nMonthTillBD(int currentMonth) {
        return _bMonth - currentMonth; // to do
    }
{code}
Compile all and test again.

...



Still we have errors.

...



*Step 5.2

...

*

Change the formula to

...


{code
}
    int nMonthTillBD(int currentMonth) {
        return (_bMonth - currentMonth + 12) % 12; // to do
    }
{code}

Compile all and test again.  Now everything should pass!  You may now remove the TO DO comment from the code of nMonthTillBD.

...



In XP programming, only after a method has passed its unit test that you are allowed to proceed to another one

...

.

h2.  3. Additional Reading:

...


Lecture notes on Object-Oriented Programming using Java by Dung X. Nguyen and Stephen B. Wong: http://cnx.org/content/col10213/latest

...

 
Here is another link for a more detailed discussion of JUnit testing uisng  DrJava: http://cnx.rice.edu/content/m11707/latest/

...


The following link contains a more elaborate discussion on JUnit testting and a more involved example of testing:  http://junit.sourceforge.net/doc/testinfected/testing.htm

...

.


----

*Access Permissions:* \(Please don't edit\)

...


 
* Set ALLOWTOPICCHANGE = Main.TeachersComp211Group