Note: Some of the code used in the following reading differ from that used in class!
lab06
directory within your Comp211 directory.class Rectangle { double width; double height; } |
double
define fields within the class, much like the fields of a structure in Scheme.double
is a the name for the principal real number type in Java. So the width
and height
fields of a Rectangle
have type double
. The Rectangle
class is not very interesting because we cannot do much with it. However, it is syntactically correct and we can compile it.define-struct Rectangle(width height) |
Comp211/lab06
folder under the default name Rectangle
. The file will be saved as a .dj0
file, which is a DrJava Elementary level file.Rectangle r = new Rectangle(5, 10); |
r
of type Rectangle
and binds it to a new object belonging to class Rectangle
.Rectangel
class yet, but we can print the string representations of Rectangle
objets. In the Interactions pane, type r
and hit Enter. This action will print the string representation of object bound to calc
. In the DrJava Elementary language level, the string representation of the Rectangle
object r
is {"Rectanlge(5.,10.)"}}. DrJava automatically strips the quotation marks off string representations when it prints them. Conventional Java would return a much more cryptic string representation for a Rectangle
object.Comp211/lab07
directory and you will see several new files. The compiler automatically created a file called Rectangle.class
containing the compiled cJava bytecode ready execution on the Java Virtual Machine (JVM). The DrJava Interactions Pane is a convenient user interface to a Java Virtual Machine. When you refer to a Java class in the Interactions pane, DrJava automatically loads the byte code for that class. When you compile the file Rectangle.dj0
using DrJava, DrJava generates a corresponding conventional Java source file called Rectangle.java
and invokes the Java compiler to translate this Java source file to bytecode.Rectangle
a method to compute the area of a rectangle. Change the definition of the Rectangle
class to read:
class Rectangle { double width; double height; // The only new lines: double area() { return width * height; } } |
Rectangle
file is automatically saved and recompiled.Rectangle
, type the following in the Interactions pane:
Rectangle r = newRectangle(4,5); |
Rectangle
class,r
in the Interactions pane.Rectangle
object bound to r2
to compute its area, type
r2.area(); |
Suppose we want to define a class representing a right triangle given the length of the two sides forming the right angle, which we call base
and height
. What code should we write? Use the DrJava New
command to
create a new document. In this document define a class called RightTriangle
with fields of type double
called base
and height
, and a method
double area() |
to compute the area of this right triangle. Save and compile this file. Interact with it in the Interactions pane.
Circle
that has one double field, radius, and a method to compute the area.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, we require that it passes its existing test suite. 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. Tests and code must be developed in tandem.
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.
class Person { /** * Computes the number of months till the next birthday given the current month. */ int nMonthTillBD(int currentMonth) { // todo } } |
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:
/** * 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() { } } |
If you have the "stable release, v. 20100913-r5387, you may receive a compile error. To remedy this, at the top of the Test_Person file, add the line
import junit.framework.TestCase;
Or simply download and replace with a later version, e.g. the latest development version.
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)); } } |
Also the test covers three cases:
The
assertEquals |
method comes from the class TestCase and takes in three parameters:
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.
class Person { int _bMonth; /** * Computes the number of months till the next birthday. */ int nMonthTillBD(int currentMonth) { return 0;// todo } } |
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
int nMonthTillBD(int currentMonth) { return _bMonth - currentMonth; // todo } |
Compile all and test again.
Still we have errors.
Step 5.2
Change the formula to
int nMonthTillBD(int currentMonth) { return(_bMonth - currentMonth + 12) % 12;// todo } |
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.
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.