Java Reference
In-Depth Information
E2.
Look at the specification of class
Integer
in the Java API specifications.
See whether it implements interface
Comparable
. Write a program that has an
array of
Integer
s, fill the array with values, and test some of the methods of
exercise E1 using this array.
E3.
Look at the specification of class
Point
in package
java.awt
. Notice that it
does not implement interface
Comparable
. This is because there is no standard
way to order points
(x,y)
in the plane. Write (and test) a subclass of
Point
that
does implement class
Comparable
. Here is one way to compare two
Point
s
p
and
q
.
p<q
if one of the following holds:
p.x
2
+ p.y
2
< q.x
2
+ q.y
2
p.x
2
+ p.y
2
= q.x
2
+ q.y
2
and
p.x < q.x
p.x
2
+ p.y
2
= q.x
2
+ q.y
2
and
p.x = q.x
and
p.y < q.y
E4.
Write a class that enumerates the indices of the letter
c
in a String
s
. The
class should implement interface
Enumeration
. The constructor of the class
should have
c
and
s
as parameters.
E5.
Write a class that enumerates the factors of an integer
k
, where
k≥1
. The
class should implement interface
Enumeration
. The constructor of the class
should have
k
as a parameter. The factors of
k
are all positive integers that divide
k
, including
1
and
k
. For example, the factors of
6
are
1
,
2
,
3
, and
6
.
E6.
Write a class that enumerates the primes that are at most
k
. The class should
implement interface
Enumeration
. The constructor of the class should have
k
as
a parameter. A
prime
is an integer that is at least
2
and has no factors other than
1
and itself. Can you use the
Enumeration
from exercise E5 to write this class?
E7.
Write a class that enumerates the indices
i
of an array
b
such that either
i=
0
or
b[i - 1] > b[i]
. The class should implement interface
Enumeration
. The
constructor of the class should have
b
as a parameter. Here is an example. For the
array
{4,5,3,6,2,1,2,4,6,6}
, the enumeration consists of the integers
0, 2,
4, 5
.
E8.
Write a class that enumerates the upper-case letters (i.e. characters in the
range
'A'..'Z'
) in a
String s
. The class should implement interface
Enumeration
. The constructor of the class should have
s
as a parameter.
E9.
Write a class that enumerates html tags in a
String
. The class should imple-
ment interface
Enumeration
. An html tag is a sequence of characters of this
form:
<
(any sequence of characters not containing
'>'
, including white space)
>
So, a tag begins with
<
and ends with
>
. Examples:
<html>
,
<body>
,
<p>
,
</p>
,
and
<br>
. Do not worry about the correctness of the stuff between
<
and
>
.
E10.
Write a class that enumerates the html tags in a String
s
that start with
”<a”
.
Search WWH ::
Custom Search