Java Reference
In-Depth Information
Use GUI JLiveWindow as a test driver
As mentioned earlier, a test driver is a program that has been written to test
execution of another program (or program unit) on some test cases. GUI
JLiveWindow can be used as a test driver in some cases. This may be easier than
using DrJava's Interactions pane, but it has the same disadvantage: one must
manually type in the test cases.
We provide an example of the use of GUI JLiveWindow . Suppose we have
written a class that contains static methods to convert among three different tem-
perature scales: Fahrenheit, Centigrade, and Kelvin, e.g.
Activity 1-5.3
gives a thor-
ough introduc-
tion to GUI
JLiveWindow.
/** Conversions to and from Fahrenheit, Centigrade, Kelvin */
public class TempConvert {
/** = Fahrenheit equivalent of Centigrade temp c */
public static double FahrFromCent( double c)
{ return 9*c/5+32; }
...
}
Our GUI test driver consists of two classes, JLiveWindow and MyJLive-
Window . It works as follows. When you start the test driver, the GUI window will
appear on your monitor. You can type a value in the first double field and press
button Ready . The driver will use the value as the argument to method
FahrFromCent and put the result in the second double field. It will also put text
in the String fields of the GUI to label the values. You can run many test cases
during one execution of the program.
Class JLiveWindow is changed in two ways to make it into our test driver.
First, the call to the constructor in method main is changed to have the arguments
0 , 2 , and 2 , so that the GUI has 0 int fields, 2 double fields, and 2 String fields.
Second, method buttonPressed , which is called when button Ready of the
GUI is pressed, is changed to do the following:
1. Copy the value in the first double field into variable b .
2. Place the title for the first double field into the first String field.
3. Place the value of the call FahrFromCent(b) in the second double field.
4. Place the title for the second double field into the second String field.
5. Return the value null .
Figure 14.5 contains this method buttonPressed .
You can use GUI JLiveWindow as a test driver for many methods. You have
control over the number of int , double , and String fields in the GUI, and you
can change method buttonPressed to suit your needs.
14.4
The Java assert statement
Throughout this text, we have annotated programs with comments that contained
Search WWH ::




Custom Search