Java Reference
In-Depth Information
Output
Average area: 625
Expected: 625
Maximum area rectangle:
java.awt.Rectangle[x=10,y=20,width=30,height=40]
Expected:
java.awt.Rectangle[x=10,y=20,width=30,height=40]
S
ELF
C
HECK
8.
Suppose you want to use the
DataSet
class of
Section 9.1
to find the
longest
String
from a set of inputs. Why can't this work?
9.
How can you use the
DataSet
class of this section to find the longest
String
from a set of inputs?
10.
Why does the
measure
method of the
Measurer
interface have one
more parameter than the
getMeasure
method of the
Measurable
interface?
402
403
9.5 Inner Classes
The
RectangleMeasurer
class is a very trivial class. We need this class only
because the
DataSet
class needs an object of some class that implements the
Measurer
interface. When you have a class that serves a very limited purpose, such
as this one, you can declare the class inside the method that needs it:
public class DataSetTester3
{
public static void main(String[] args)
{
class RectangleMeasurer implements Measurer
{
. . .
}
Measurer m = new RectangleMeasurer();
DataSet data = new DataSet(m);
. . .
}
}