Instructions:Omitted
Getting Ready:Omitted
Calculator
class contains the following two
methods:
/**
* Calculates the inverse of a number
*
* @param x The number
* @return The inverse (i.e., 1.0/x)
*/
public static double inverse(double x)
/**
* Calculates the percentage that a part is of a whole
* (assuming both are non-negative)
*
* @param part The part (i.e., numerator)
* @param whole The whole (i.e., denominator)
* @return A percentage (where 100.0 = 100%)
*/
public static double percent(double part, double whole)
Make sure you understand the purpose of these two methods.
inverse()
method.
public static void main(String[] args)
{
double x;
// Small Values
x = Double.NEGATIVE_INFINITY;
for (int i=1; i<10; i++)
{
testInverse(x + i);
}
// Large Values
x = Double.POSITIVE_INFINITY - 10.;
for (int i=0; i<10; i++)
{
testInverse(x + i);
}
// Values Near 0
for (x=-2.0; x<2.0; x+=0.1)
{
testInverse(x);
}
// 0
testInverse(0.0);
}
private static void testInverse(double x)
{
System.out.println("Test Case: "+x+"\t"+Calculator.inverse(x));
}
inverse()
to fail?
An argument of 0 yields a result of
Double.POSITIVE_INFINITY
. This may or may not be thought
of as a failure.
percent()
method.
whole
. Now,
however, each of these "loops" was executed for several values of
part
(including extreme positive values, values near
0, 0, and extreme negative values).
percent()
to fail?
When both parameters are 0.0.
percent()
method failed?
It returned Double.NAN
.
Series
class.
arithmetic()
method.
None.
arithmetic()
method? (Note: An oracle is a component that can be
used to verify the function of another component.)
We could compare the result returned by arithmetic()
to (double)(n*(n+1)) / 2.0
.
arithmetic()
method
using this oracle.
It does not work for negative numbers. Should it? (This is a discrete math question that you should be able to find the answer to.)
pi(int)
method.
While desk checking I noticed that denominator
was an int
and, hence, integer division was
being performed. I corrected this by making denominator
a double
(and using 1.0
in the numerator).
Document
and FormattedDocument
classes and refresh your memory of them. (You used them in the lab
on "Experimenting with Accessibility/Visibility".)
The parent class. It is impossible to isolate faults in the subclass when the superclass contains faults.
Document
class, which method should you test first,
getDescription()
or getWordCount()
? Why?
getWordCount()
because it is used by
getDescription()
.
getWordCount()
method.
One with no words, one with 1 word, one that has three words using one delimiter, one that has three words using two delimiters, one that has multiple words using one delimiter, one that has multiple words using multiple delimiters (each used multiple times).
FormattedDocument
class, in what order
should you test the various methods?
First the constructor. Second the steWidth()
method.
Third the getText()
method.
Fourth the getDescription()
method.
Yes. But, if you did a good job of testing the superclass this should be easy.
When they use methods that are overridden in the subclass.
getWordCount()
method) important test cases for
the getText()
method.
One with a break that occurs at a delimiter. One with a break that occurs at the first letter of a word. One with a break that occurs in the middle of a word. One with a break that occurs at the end of a word. One with a break that occurs in the middle of a sequence of delimiters.
Copyright 2011