Java Reference
In-Depth Information
Enhancingj--
Although j-- is a subset of Java, it provides an elaborate framework with which one may
add new Java constructs to j--. This will be the objective of many of the exercises in this
topic. In fact, with what we know so far about j--, we are already in a position to start
enhancing the language by adding new albeit simple constructs to it.
As an illustrative example, we will add the division 12 operator to j--. This involves
modifying the scanner to recognize / as a token, modifying the parser to be able to parse
division expressions, implementing semantic analysis, and finally, code generation for the
division operation.
In adding new language features to j--, we advocate the use of the Extreme Program-
ming 13 (XP) paradigm, which emphasizes writing tests before writing code. We will do
exactly this with the implementation of the division operator.
Writing Tests
Writing tests for new language constructs using the j-- test framework involves
Writing pass tests, which are j-- programs that can successfully be compiled using the
j-- compiler;
Writing JUnit test cases that would run these pass tests;
Adding the JUnit test cases to the j-- test suite; and finally
Writing fail tests, which are erroneous j-- programs. Compiling a fail test using j--
should result in the compiler's reporting the errors and gracefully terminating without
producing any .class files for the erroneous program.
We first write a pass test Division.java for the division operator, which simply has a
method divide() that accepts two arguments x and y, and returns the result of dividing
x by y. We place this file under the $j/j--/tests/pass folder; pass is a package.
packagepass;
publicclassDivision{
publicintdivide(intx,inty){
returnx/y;
}
}
Next, we write a JUnit test case DivisionTest.java , with a method testDivide()
that tests the divide() method in Division.java with various arguments. We place this
file under $j/j--/tests/junit folder; junit is a package.
publicclassDivisionTestextendsTestCase{
privateDivisiondivision;
protectedvoidsetUp()throwsException{
super.setUp();
division=newDivision();
}
protectedvoidtearDown()throwsException{
super.tearDown();
12 We only handle integer division sincej--supports only int s as numeric types.
13 http://www.extremeprogramming.org/ .
 
Search WWH ::




Custom Search