JAVA,J2EE QUESTIONS - GetACoder

grapedraughtΛογισμικό & κατασκευή λογ/κού

2 Δεκ 2013 (πριν από 4 χρόνια και 29 μέρες)

398 εμφανίσεις


1.]

Following the
HelloDate.java

example in this chapter, create a “hello,
world” program that simply prints out that statement. You need only a single
method in your class (the “main” one that gets executed when the program
starts). Remember to make it
static

and to include the argument list, even
though you don’t use the argument list. Compile the program with
javac

and run
it using
java
. If you are using a different development environment than the
JDK, learn how to compile and run programs in that env
ironment.


2.]

Find the code fragments involving
ATypeName

and turn them into a
program that compiles and runs.



3.]

Turn the
DataOnly

code fragments into a program that compiles and runs.



4.]

Modify Exercise 3 so that the values of the data in
DataOnly

are assi
gned to
and printed in
main(

)
.


5.]

Write a program that includes and calls the
storage(

)

method defined as a
code fragment in this chapter.



6.]

Turn the
StaticFun

code fragments into a working program.



Write a program that prints three arguments t
aken from the command line. To
do this, you’ll need to index into the command
-
line array of
String
s.



7.]

Turn the
AllTheColorsOfTheRainbow

example into a program that
compiles and runs.



8.]

Find the code for the second version of
HelloDate.java
, which
is the simple
comment documentation example. Execute
javadoc

on the file and view the
results with your Web browser.



9.]

Turn
docTest

into a file that compiles and then run it through
javadoc
.
Verify the resulting documentation with your Web browser.



1
0.]

Add an HTML list of items to the documentation in Exercise 10.



11.]

Take the program in Exercise 1 and add comment documentation to it.
Extract this comment documentation into an HTML file using
javadoc

and view
it with your Web browser.



12.]

There

are two expressions in the section labeled “precedence” early in this
chapter. Put these expressions into a program and demonstrate that they produce
different results.



13.]

Put the methods ternary(

) and alternative(

) into a working program.



14.]
Fr
om the sections labeled “if
-
else” and “return”, put the methods test(

) and
test2(

) into a working program.



15.]

Write a program that prints values from one to 100.



16.]
Modify Exercise 4 so that the program exits by using the break keyword at
value 4
7. Try using return instead.



17.]

Write a function that takes two String arguments, and uses all the Boolean
comparisons to compare the two Strings and print the results. For the == and !=,
also perform the equals(

) test. In main(

), call your function
with some different
String objects.



18.]

Write a program that generates 25 random int values. For each value, use
an if
-
then
-
else statement to classify it as greater than, less than or equal to a
second randomly
-
generated value.



19.]

Modify Exercise 7
so that your code is surrounded by an “infinite” while
loop. It will then run until you interrupt it from the keyboard (typically by
pressing Control
-
C).


20.]

Write a program that uses two nested for loops and the modulus operator
(%) to detect and print

prime numbers (integral numbers that are not evenly
divisible by any other numbers except for themselves and 1).



21.]

Create a switch statement that prints a message for each case, and put the
switch inside a for loop that tries each case. Put a break a
fter each case and test it,
then remove the breaks and see what happens.


1)

Create a class with a default constructor (one that takes no arguments)
that prints a message. Create an object of this class.

2)

Add an overloaded constructor to Exercise 1 that takes
a
String

argument
and prints it along with your message.

3)

Create an array of object references of the class you created in Exercise 2,
but don’t actually create objects to assign into the array. When you run the
program, notice whether the initialization me
ssages from the constructor
calls are printed.

4)

Complete Exercise 3 by creating objects to attach to the array of
references.

5)

Create an array of
String

objects and assign a string to each element.
Print the array using a
for

loop.

6)

Create a class called
Dog

with an overloaded
bark(

)

method. This
method should be overloaded based on various primitive data types, and
print different types of barking, howling, etc., depending on which
overloaded version is called. Write a
main(

)

that calls all the different
v
ersions.

7)

Modify Exercise 6 so that two of the overloaded methods have two
arguments (of two different types), but in reversed order relative to each
other. Verify that this works.

8)

Create a class without a constructor, and then create an object of that clas
s
in
main(

)

to verify that the default constructor is automatically
synthesized.

9)

Create a class with two methods. Within the first method, call the second
method twice: the first time without using
this
, and the second time using
this
.

10)

Create a class with

two (overloaded) constructors. Using
this
, call the
second constructor inside the first one.

11)

Create a class with a
finalize(

)

method that prints a message. In
main(

)
, create an object of your class. Explain the behavior of your
program.

12)

Modify Exercise
11 so that your
finalize(

)

will always be called.

13)

Create a class called
Tank

that can be filled and emptied, and has a
death
condition

that it must be empty when the object is cleaned up. Write a
finalize(

)

that verifies this death condition. In
main(

)
,

test the possible
scenarios that can occur when your
Tank

is used.

14)

Create a class containing an
int

and a
char

that are not initialized, and
print their values to verify that Java performs default initialization.

15)

Create a class containing an uninitialized

String

reference. Demonstrate
that this reference is initialized by Java to
null
.

16)

Create a class with a
String
field that is initialized at the point of
definition, and another one that is initialized by the constructor. What is
the difference between the

two approaches?

17)

Create a class with a
static String
field that is initialized at the point of
definition, and another one that is initialized by the
static
block. Add a
static

method that prints both fields and demonstrates that they are both
initialized
before they are used.

18)

Create a class with a
String

that is initialized using “instance
initialization.” Describe a use for this feature (other than the one specified
in this book).

19)

Write a method that creates and initializes a two
-
dimensional array of
doub
le
. The size of the array is determined by the arguments of the
method, and the initialization values are a range determined by beginning
and ending values that are also arguments of the method. Create a second
method that will print the array generated by

the first method. In
main(

)

test the methods by creating and printing several different sizes of arrays.

20)

Repeat Exercise 19 for a three
-
dimensional array.

21)

Comment the line marked (1) in
ExplicitStatic.java

and verify that the
static initialization clause

is not called. Now uncomment one of the lines
marked (2) and verify that the static initialization clause
is

called. Now
uncomment the other line marked (2) and verify that static initialization
only occurs once.

22)

Experiment with
Garbage.java

by running th
e program using the
arguments “gc,” “finalize,” or “all.” Repeat the process and see if you detect
any patterns in the output. Change the code so that
System.runFinalization(

)
is called
before

System.gc(

)

and observe
the results.

23)

Write a program that cre
ates an
ArrayList

object without explicitly
importing
java.util.*
.

24)

In the section labeled “package: the library unit,” turn the code fragments
concerning
mypackage

into a compiling and running set of Java files.

25)

In the section labeled “Collisions,” take th
e code fragments and turn them
into a program, and verify that collisions do in fact occur.

26)

Generalize the class
P

defined in this chapter by adding all the overloaded
versions of
rint(

)

and
rintln(

)

necessary to handle all the different basic
Java types
.

27)

Change the import statement in
TestAssert.java

to enable and disable
the assertion mechanism.

28)

Create a class with
public
,
private
,
protected,

and “friendly” data
members and method members. Create an object of this class and see what
kind of compiler mes
sages you get when you try to access all the class
members. Be aware that classes in the same directory are part of the
“default” package.

29)

Create a class with
protected

data. Create a second class in the same file
with a method that manipulates the
protect
ed

data in the first class.

30)

Change the class
Cookie
as specified in the section labeled “
protected
:
‘sort of friendly.’” Verify that
bite(

)
is not
public
.

31)

In the section titled “Class access” you’ll find code fragments describing
mylib

and
Widget
. Create
this library, then create a
Widget

in a class
that is not part of the
mylib

package.

32)

Create a new directory and edit your CLASSPATH to include that new
directory. Copy the
P.class

file (produced by compiling
com.bruceeckel.tools.P.java
)

to your new directo
ry and then change
the names of the file, the
P

class inside and the method names. (You might
also want to add additional output to watch how it works.) Create another
program in a different directory that uses your new class.

33)

Following the form of the exa
mple
Lunch.java
, create a class called
ConnectionManager

that manages a fixed array of
Connection

objects. The client programmer must not be able to explicitly create
Connection

objects, but can only get them via a
static

method in
ConnectionManager
. When
the
ConnectionManager

runs out of
objects, it returns a
null

reference. Test the classes in
main(

)
.

34)

Create the following file in the c05/local directory (presumably in your
CLASSPATH):

///: c05:local:PackagedClass.java

package

c05.local;

class

PackagedCla
ss {


public

PackagedClass() {


System.out.println(


"Creating a packaged class"
);


}

}
///:~

Then create the following file in a directory other than c05:

///: c05:foreign:Foreign.java

package

c05.foreign;

import

c05.local.*;

public

class

Foreig
n {


public

static

void

main (String[] args) {


PackagedClass pc =
new

PackagedClass();


}

}
///:~

Explain why the compiler generates an error. Would making the
Foreign

class
part of the
c05.local

package change anything?


34.]

Create two classes
,
A

and
B
, with default constructors (empty argument
lists) that announce themselves. Inherit a new class called
C

from
A
, and create a
member of class
B

inside
C
. Do not create a constructor for
C
. Create an object of
class
C

and observe the results.



35
.]

Modify Exercise 1 so that
A

and
B

have constructors with arguments
instead of default constructors. Write a constructor for
C

and perform all
initialization within
C
’s constructor.



36.]

Create a simple class. Inside a second class, define a field for

an object of
the first class. Use lazy initialization to instantiate this object.



37.]
Inherit a new class from class
Detergent
. Override
scrub(

)

and add a
new method called
sterilize(

)
.



38.]
Take the file
Cartoon.java

and comment out the constructo
r for the
Cartoon

class. Explain what happens.



39.]

Take the file
Chess.java

and comment out the constructor for the
Chess

class. Explain what happens.



40.]

Prove that default constructors are created for you by the compiler.



41.]

Prove that the base
-
class constructors are (a) always called, and (b) called
before derived
-
class constructors.



42.]

Create a base class with only a nondefault constructor, and a derived class
with both a default and nondefault constructor. In the derived
-
class constructor
s,
call the base
-
class constructor.



43.]

Create a class called
Root

that contains an instance of each of classes (that
you also create) named
Component1
,
Component2
,

and
Component3
.
Derive a class
Stem

from
Root

that also contains an instance of each
“co
mponent.” All classes should have default constructors that print a message
about that class.



44.]

Modify Exercise 10 so that each class only has nondefault constructors.



45.]

Add a proper hierarchy of
cleanup(

)

methods to all the classes in Exercise
11.



46.]

Create a class with a method that is overloaded three times. Inherit a new
class, add a new overloading of the method, and show that all four methods are
available in the derived class.



47.]

In
Car.java

add a
service(

)

method to
Engine

and ca
ll this method in
main(

)
.



48.]

Create a class inside a package. Your class should contain a
protected

method. Outside of the package, try to call the
protected

method and explain
the results. Now inherit from your class and call the
protected

method fro
m
inside a method of your derived class.


49.]

Create a class called
Amphibian
. From this, inherit a class called
Frog
.
Put appropriate methods in the base class. In
main(

)
, create a
Frog

and upcast
it to
Amphibian
, and demonstrate that all the methods s
till work.



50.]

Modify Exercise 16 so that
Frog

overrides the method definitions from the
base class (provides new definitions using the same method signatures). Note
what happens in
main(

)
.



51.]

Create a class with a
static final

field and a
final

fi
eld and demonstrate
the difference between the two.



52.]

Create a class with a blank
final

reference to an object. Perform the
initialization of the blank
final

inside a method (not the constructor) right before
you use it. Demonstrate the guarantee that

the
final

must be initialized before
use, and that it cannot be changed once initialized.


53.]

Create a class with a
final

method. Inherit from that class and attempt to
override that method.


54.]

Create a
final

class and attempt to inherit from it.



55.]
Prove that class loading takes place only once. Prove that loading may be
caused by either the creation of the first instance of that class, or the access of a
static

member.

56.]


In
Beetle.java
, inherit a specific type of beetle from class
Beetle
,

following the same format as the existing classes. Trace and explain the
output.


57.]

Add a new method in the base class of
Shapes.java

that prints a message,
but don’t override it in the derived classes. Explain what happens. Now override
it in one of
the derived classes but not the others, and see what happens. Finally,
override it in all the derived classes.



58.]

Add a new type of
Shape

to
Shapes.java

and verify in
main(

)
that
polymorphism works for your new type as it does in the old types.



59.]

Change
Music3.java

so that
what(

)

becomes the root
Object
method
toString(

)
. Try printing the
Instrument

objects using
System.out.println(

)

(without any casting).


60.]

Add a new type of
Instrument

to
Music3.java

and verify that
polymorphism works for

your new type.


61.]

Modify
Music3.java

so that it randomly creates
Instrument

objects the
way
Shapes.java

does.


62.]

Create an inheritance hierarchy of
Rodent
:
Mouse
,
Gerbil
,
Hamster
,
etc. In the base class, provide methods that are common to all
Rode
nt
s, and
override these in the derived classes to perform different behaviors depending on
the specific type of
Rodent
. Create an array of
Rodent
, fill it with different
specific types of
Rodent
s, and call your base
-
class methods to see what happens.


63.]

Modify Exercise 6 so that
Rodent

is an
abstract

class. Make the methods
of
Rodent

abstract whenever possible.


64.]

Create a class as
abstract

without including any
abstract

methods, and
verify that you cannot create any instances of that class.


65.]

Add class
Pickle

to
Sandwich.java
.



66.]

Modify Exercise 6 so that it demonstrates the order of initialization of the
base classes and derived classes. Now add member objects to both the base and
derived classes, and show the order in which their initiali
zation occurs during
construction.



67.]

Create a 3
-
level inheritance hierarchy. Each class in the hierarchy should
have a
finalize(

)

method, and it should properly call the base
-
class version of
finalize(

)
. Demonstrate that your hierarchy works properl
y.



68.]

Create a base class with two methods. In the first method, call the second
method. Inherit a class and override the second method. Create an object of the
derived class, upcast it to the base type, and call the first method. Explain what
happens.



69.]

Create a base class with an
abstract

print(

)
method that is overridden in
a derived class. The overridden version of the method prints the value of an
int
variable defined in the derived class. At the point of definition of this variable,
give it
a nonzero value. In the base
-
class constructor, call this method. In
main(

)
, create an object of the derived type, and then call its
print(

)

method.
Explain the results.



70.]

Following the example in
Transmogrify.java
, create a
Starship

class
containin
g an
AlertStatus

reference that can indicate three different states.
Include methods to change the states.

71.]


Create an
abstract

class with no methods. Derive a class and add a
method. Create a
static

method that takes a reference to the base class,
do
wncasts it to the derived class, and calls the method. In
main(

)
,
demonstrate that it works. Now put the
abstract

declaration for the method
in the base class, thus eliminating the need for the downcast.


72.]

Prove that the fields in an
interface

are im
plicitly
static

and
final
.



73,]

Create an
interface

containing three methods, in its own
package
.
Implement the interface in a different
package
.



74.]

Prove that all the methods in an
interface

are automatically
public
.



75.]

In
c07:Sandwich.java
, cre
ate an interface called
FastFood

(with
appropriate methods) and change
Sandwich

so that it also implements
FastFood
.



76.]

Create three
interface
s, each with two methods. Inherit a new
interface

from the three, adding a new method. Create a class by imple
menting the new
interface

and also inheriting from a concrete class. Now write four methods,
each of which takes one of the four
interface
s as an argument. In
main(

)
,
create an object of your class and pass it to each of the methods.



77.]
Modify Exercis
e 5 by creating an
abstract

class and inheriting that into the
derived class.



78.]

Modify
Music5.java

by adding a
Playable

interface
. Remove the
play(

)

declaration from
Instrument
. Add
Playable

to the derived classes by
including it in the
implements

li
st. Change
tune(

)

so that it takes a
Playable

instead of an
Instrument
.



79.]

Change Exercise 6 in Chapter 7 so that
Rodent

is an
interface
.



80.]

In
Adventure.java
, add an
interface

called
CanClimb
, following the
form of the other interfaces.


81.]

Wr
ite a program that imports and uses
Month2.java
.



82.]

Following the example given in
Month2.java
, create an enumeration of
days of the week.



83.]

Create an
interface

with at least one method, in its own package. Create a
class in a separate package. Ad
d a
protected

inner class that implements the
interface
. In a third package, inherit from your class and, inside a method,
return an object of the
protected

inner class, upcasting to the
interface

during
the return.



84.]

Create an
interface

with at least

one method, and implement that
interface

by defining an inner class within a method, which returns a reference
to your
interface
.


85.]

Repeat Exercise 13 but define the inner class within a scope within a
method.



86.]

Repeat Exercise 13 using an anony
mous inner class.



87.]

Create a
private

inner class that implements a
public

interface
. Write a
method that returns a reference to an instance of the
private

inner class, upcast
to the
interface
. Show that the inner class is completely hidden by trying t
o
downcast to it.


88.]

Create a class with a nondefault constructor and no default constructor.
Create a second class that has a method which returns a reference to the first
class. Create the object to return by making an anonymous inner class that
inhe
rits from the first class.


89.]

Create a class with a
private

field and a
private

method. Create an inner
class with a method that modifies the outer class field and calls the outer class
method. In a second outer class method, create an object of the in
ner class and
call it’s method, then show the effect on the outer class object.



90.]

Repeat Exercise 18 using an anonymous inner class.



91.]

Create a class containing a
static

inner class. In
main(

)
, create an
instance of the inner class.


92.]

Creat
e an
interface

containing a
static

inner class. Implement this
interface

and create an instance of the inner class.


93.]

Create a class containing an inner class that itself contains an inner class.
Repeat this using
static

inner classes. Note the names
of the
.class

files
produced by the compiler.


94.]

Create a class with an inner class. In a separate class, make an instance of
the inner class.



95.]

Create a class with an inner class that has a nondefault constructor. Create
a second class with an in
ner class that inherits from the first inner class.



96.]

Repair the problem in
WindError.java
.



97.]

Modify
Sequence.java

by adding a method
getRSelector(

)

that
produces a different implementation of the
Selector

interface

that moves
backward through t
he sequence from the end to the beginning.


98.]

Create an
interface

U
with three methods. Create a class
A
with a method
that produces a reference to a
U

by building an anonymous inner class. Create a
second class
B
that contains an array of
U
.
B

should
have one method that
accepts and stores a reference to a
U

in the array, a second method that sets a
reference in the array (specified by the method argument) to
null

and a third
method that moves through the array and calls the methods in
U
. In
main(

)
,
c
reate a group of
A

objects and a single
B
. Fill the
B

with
U

references produced
by the
A

objects. Use the
B

to call back into all the
A

objects. Remove some of the
U

references from the
B
.



99.]

In
GreenhouseControls.java
, add
Event

inner classes that turn fans on
and off.

100.]


Show that an inner class has access to the
private

elements of its outer
class. Determine whether the reverse is true.



101.]

Create an array of
double

and
fill(

)

it using
RandDoubleGenerator
.
Print the results.


102.]

Create a new class called
Gerbil

with an
int gerbilNumber
that’s
initialized in the constructor (similar to the
Mouse
example in this chapter).
Give it a method called
hop(

)

that prints out which

gerbil number this is, and
that it’s hopping. Create an
ArrayList

and add a bunch of
Gerbil

objects to the
List
. Now use the
get(

)

method to move through the
List

and call
hop(

)

for
each
Gerbil
.


103.]

Modify Exercise 2 so you use an
Iterator

to move t
hrough the
List

while
calling
hop(

)
.



104.]

Take the
Gerbil

class in Exercise 2 and put it into a
Map

instead,
associating the name of the
Gerbil

as a
String

(the key) for each
Gerbil
(the
value) you put in the table. Get an
Iterator

for the
keySet(

)

an
d use it to move
through the
Map
, looking up the
Gerbil

for each key and printing out the key
and telling the
gerbil

to
hop(

)
.



105.]

Create a
List

(try both
ArrayList
and
LinkedList
)

and fill it using
Collections2.countries
. Sort the list and print it,
then apply
Collections.shuffle(

)

to the list repeatedly, printing it each time so that you
can see how the
shuffle(

)

method randomizes the list differently each time.


106.]

Demonstrate that you can’t add anything but a
Mouse

to a
MouseList
.


107.]

Mod
ify
MouseList.java

so that it inherits from
ArrayList

instead of
using composition. Demonstrate the problem with this approach.


108.]

Repair
CatsAndDogs.java

by creating a
Cats

container (utilizing
ArrayList
)

that will only accept and retrieve
Cat

object
s.


109.]

Create a container that encapsulates an array of
String
, and that only
adds
String
s and gets
String
s, so that there are no casting issues during use. If
the internal array isn’t big enough for the next add, your container should
automatically re
size it. In
main(

)
, compare the performance of your container
with an
ArrayList

holding
String
s.



110.]

Repeat Exercise 9 for a container of
int
, and compare the performance to
an
ArrayList

holding
Integer

objects. In your performance comparison,
include

the process of incrementing each object in the container.


111.]

Using the utilities in
com.bruceeckel.util
, create an array of each
primitive type and of
String
, then fill each array using an appropriate generator,
and print each array using the appropr
iate
print(

)

method.


112.]

Create a generator that produces character names from your favorite
movies (you can use
Snow White

or
Star Wars
as a fallback), and loops around to
the beginning when it runs out of names. Use the utilities in
com.bruceeckel.u
til

to fill an array, an
ArrayList
, a
LinkedList

and both
types of
Set
, then print each container.


113.]

Create a class containing two
String

objects, and make it
Comparable

so that the comparison only cares about the first
String
. Fill an array and an
A
rrayList
with objects of your class, using the
geography
generator.
Demonstrate that sorting works properly. Now make a
Comparator

that only
cares about the second
String

and demonstrate that sorting works properly; also
perform a binary search using your
Comparator
.


114.]

Modify Exercise 13 so that an alphabetic sort is used.



115.]

Use
Arrays2.RandStringGenerator

to fill a
TreeSet

but using
alphabetic ordering. Print the
TreeSet

to verify the sort order.


116.]

Create both an
ArrayList

and a
LinkedList
, and fill
each using the
Collections2.capitals

generator. Print each list using an ordinary
Iterator
,
then insert one list into the other using a
ListIterator
, inserting at every other
location. Now perform the insertion starting at the end of the first list and
mov
ing backward.


117.]

Write a method that uses an
Iterator

to step through a
Collection

and
print the
hashCode(

)

of each object in the container. Fill all the different types
of
Collection
s with objects and apply your method to each container.



118.]
Repair the proble
m in
InfiniteRecursion.java
.



119.]

Create a class, then make an initialized array of objects of your class. Fill a
List

from your array. Create a subset of your
List

using
subList(

)
, and then
remove this subset from your
List

using
removeAll(

)
.



120.]

Change Exercise 6 in Chapter 7 to use an
ArrayList

to hold the
Rodent
s
and an
Iterator

to move through the sequence of
Rodent
s. Remember that an
ArrayList

holds only
Object
s so you must use a cast when accessing individual
Rodent
s.



121.]

Following the
Q
ueue.java

example, create a
Deque

class and test it.


122.]

Use a
TreeMap

in
Statistics.java
. Now add code that tests the
performance difference between
HashMap

and
TreeMap

in that program.


123.]

Produce a
Map

and a
Set
containing all the countries that

begin with ‘A.’



124.]

Using
Collections2.countries
, fill a
Set

multiple times with the same
data and verify that the
Set

ends up with only one of each instance. Try this with
both kinds of
Set
.



125.]

Starting with
Statistics.java
, create a program tha
t runs the test
repeatedly and looks to see if any one number tends to appear more than the
others in the results.



126.]

Rewrite
Statistics.java

using a
HashSet

of
Counter

objects (you’ll
have to modify
Counter

so that it will work in the
HashSet
). Which

approach
seems better?



127.]

Modify the class in Exercise 13 so that it will work with
HashSet
s and as a
key in
HashMap
s.


128.]

Using
SlowMap.java

for inspiration, create a
SlowSet
.


129.]

Apply the tests in
Map1.java

to
SlowMap

to verify that it wor
ks. Fix
anything in
SlowMap
that doesn’t work correctly.


130.]

Implement the rest of the
Map

interface for
SlowMap
.



131.]

Modify
MapPerformance.java

to include tests of
SlowMap
.



132.]

Modify
SlowMap

so that instead of two
ArrayList
s, it holds a singl
e
ArrayList

of
MPair

objects. Verify that the modified version works correctly.
Using
MapPerformance.java
, test the speed of your new
Map
. Now change
the
put(

)

method so that it performs a
sort(

)

after each pair is entered, and
modify
get(

)

to use
Colle
ctions.binarySearch(

)

to look up the key. Compare
the performance of the new version with the old ones.


133.]

Add a
char

field to
CountedString

that is also initialized in the
constructor, and modify the
hashCode(

)

and
equals(

)

methods to include the
value of this
char
.



134.]

Modify
SimpleHashMap

so that it reports collisions, and test this by
adding t
he same data set twice so that you see collisions.



135.]

Modify
SimpleHashMap

so that it reports the number of “probes”
necessary when collisions occur. That is, how many calls to
next(

)

must be made
on the
Iterator
s that walk the
LinkedList
s looking fo
r matches?



136.]

Implement the
clear(

)

and
remove(

)

methods for
SimpleHashMap
.



137.]
Implement the rest of the
Map

interface for
SimpleHashMap
.



138.]

Add a
private

rehash(

)

method to
SimpleHashMap

that is invoked
when the load factor exceeds 0.75.

During rehashing, double the number of
buckets, then search for the first prime number greater than that to determine
the new number of buckets.



139.]

Following the example in
SimpleHashMap.java
, create and test a
SimpleHashSet
.



140.]

Modify
SimpleHas
hMap

to use
ArrayList
s instead of
LinkedList
s.
Modify
MapPerformance.java

to compare the performance of the two
implementations.



141.]
Using the HTML documentation for the JDK (downloadable from
java.sun.com
), look up the
HashMap

class. Create a
HashMap
,

fill it with
elements, and determine the load factor. Test the lookup speed with this map,
then attempt to increase the speed by making a new
HashMap

with a larger
initial capacity and copying the old map into the new one, running your lookup
speed test a
gain on the new map.



142.]

In Chapter 8, locate the
GreenhouseControls.java

example, which
consists of three files. In
Controller.java
, the class
EventSet

is just a
container. Change the code to use a
LinkedList

instead of an
EventSet
. This
will require more than just replac
ing
EventSet

with
LinkedList
; you’ll also
need to use an
Iterator

to cycle through the set of events.



143.]

(Challenging). Write your own hashed map class, customized for a
particular key type:
String
for this example. Do not inherit it from
Map
.
Instead
, duplicate the methods so that the
put(

)

and
get(

)

methods specifically
take
String

objects, not
Object
s, as keys. Everything that involves keys should
not use generic types, but instead work with
String
s, to avoid the cost of
upcasting and downcasting.

Your goal is to make the fastest possible custom
implementation. Modify
MapPerformance.java

to test your implementation
vs. a
HashMap
.

144.]


(Challenging). Find the source code for
List

in the Java source code
library that comes with all Java distributions. Copy this code and make a
special version called
intList

that holds only
int
s. Consider what it would
take to make a special version of
List

for all the primitive types. Now consider
what happens if you want to make a linked list class that works with all the
primitive types. If parameterized types are ever implemented in Java, they
will provide a way to do this work for you automatically
(as well as many other
benefits).


145.]

Create a class with a
main(

)

that throws an object of class
Exception

inside a
try

block. Give the constructor for
Exception

a
String

argument. Catch
the exception inside a
catch

clause and print the
String

argument. Add a
finally

clause and print

a message to prove you were there.



146.]

Create your own exception class using the
extends

keyword. Write a
constructor for this class that takes a
String

argument and stores it inside the
object with a
String

reference. Write a method that prints out t
he stored
String
.
Create a
try
-
catch

clause to exercise your new exception.


147.]

Write a class with a method that throws an exception of the type created in
Exercise 2. Try compiling it without an exception specification to see what the
compiler says. A
dd the appropriate exception specification. Try out your class
and its exception inside a try
-
catch clause.



148.]

Define an object reference and initialize it to
null
. Try to call a method
through this reference. Now wrap the code in a
try
-
catch

clause t
o catch the
exception.


149.]

Create a class with two methods,
f(

)

and
g(

)
. In
g(

)
, throw an exception
of a new type that you define. In
f(

)
, call
g(

)
, catch its exception and, in the
catch

clause, throw a different exception (of a second type that y
ou define). Test
your code in
main(

)
.



150.]

Create three new types of exceptions. Write a class with a method that
throws all three. In
main(

)
, call the method but only use a single
catch

clause
that will catch all three types of exceptions.



151.]

Wr
ite code to generate and catch an
ArrayIndexOutOfBoundsException
.



152.]

Create your own resumption
-
like behavior using a
while

loop that repeats
until an exception is no longer thrown.



153.]

Create a three
-
level hierarchy of exceptions. Now create a ba
se
-
class
A
with a method that throws an exception at the base of your hierarchy. Inherit
B

from
A

and override the method so it throws an exception at level two of your
hierarchy. Repeat by inheriting class
C

from
B
. In
main(

)
, create a
C

and upcast
it to

A
, then call the method.


154.]

Demonstrate that a derived
-
class constructor cannot catch exceptions
thrown by its base
-
class constructor.



155.]

Show that
OnOffSwitch.java

can fail by throwing a
RuntimeException

inside the
try

block.



156.]

Show that
WithFinally.java

doesn’t f
ail by throwing a
RuntimeException

inside the
try

block.


156.]

Modify Exercise 6 by adding a
finally

clause. Verify your
finally

clause is
executed, even if a
NullPointerException

is thrown.



157.]

Create an example where you use a flag to control wheth
er cleanup code is
called, as described in the second paragraph after the heading “Constructors.”


158.]

Modify
StormyInning.java

by adding an
UmpireArgument

exception
type, and methods that throw this exception. Test the modified hierarchy.



159.]

Remov
e the first catch clause in
Human.java

and verify that the code
still compiles and runs properly.



160.]

Add a second level of exception loss to
LostMessage.java

so that the
HoHumException

is itself replaced by a third exception.


161.]

In Chapter 5, fin
d the two programs called
Assert.java

and modify these
to throw their own type of exception instead of printing to
System.err
. This
exception should be an inner class that extends
RuntimeException
.

162.]


Add an appropriate set of exceptions to
c08:Greenh
ouseControls.java
.



163.]

Open a text file so that you can read the file one line at a time. Read each
line as a
String
and place that
String
object into a
LinkedList
. Print all of the
lines in the
LinkedList

in reverse order.



164.]

Modify Exercise 1 so

that the name of the file you read is provided as a
command
-
line argument.



Modify Exercise 2 to also open a text file so you can write text into it. Write the
lines in the
ArrayList
, along with line numbers (do not attempt to use the
“LineNumber” class
es), out to the file.


165.]

Modify Exercise 2 to force all the lines in the
ArrayList

to upper case and
send the results to
System.out
.


166.]

Modify Exercise 2 to take additional command
-
line arguments of words to
find in the file. Print any lines in w
hich the words match.



167.]

Modify
DirList.java

so that the
FilenameFilter

actually opens each file
and accepts the file based on whether any of the trailing arguments on the
command line exist in that file.


168.]

Create a class called
SortedDirList

wi
th a constructor that takes file path
information and builds a sorted directory list from the files at that path. Create
two overloaded
list(

)
methods that will either produce the whole list or a subset
of the list based on an argument. Add a
size(

)

meth
od that takes a file name and
produces the size of that file.



169.]

Modify
WordCount.java

so that it produces an alphabetic sort instead,
using the tool from Chapter 9.



170.]

Modify
WordCount.java

so that it uses a class containing a
String

and
a count value to store each different word, and a
Set

of these object
s to maintain
the list of words.



171.]

Modify
IOStreamDemo.java

so that it uses
LineNumberInputStream

to keep track of the line count. Note that it’s much
easier to just keep track programmatically.



172.]

Starting with section 4 of
IOStreamDemo.java
, w
rite a program that
compares the performance of writing to a file when using buffered and
unbuffered I/O.



173.]

Modify section 5 of
IOStreamDemo.java

to eliminate the spaces in the
line produced by the first call to
in5br.readLine(

)
. Do this using a
whi
le

loop
and
readChar(

)
.



174.]

Repair the program
CADState.java

as described in the text.



175.]

In
Blips.java
, copy the file and rename it to
BlipCheck.java
and
rename the class
Blip2

to
BlipCheck
(making it
public

and removing the
public scope from th
e class
Blips

in the process). Remove the
//!

marks in the
file and execute the program including the offending lines. Next, comment out
the default constructor for
BlipCheck
. Run it and explain why it works. Note
that after compiling, you must execute the

program with “
java Blips
” because
the
main(

)

method is still in class
Blips
.



176.]

In
Blip3.java
, comment out the two lines after the phrases “You must do
this:” and run the program. Explain the result and why it differs from when the
two lines are in the program.

177.]


(Intermediate) In Chapter 8, locate the
GreenhouseControls
.java

example, which consists of three files. In
GreenhouseControls.java
, the
Restart(

)

inner class has a hard
-
coded set of events. Change the program so
that it reads the events and their relative times from a text file. (Challenging:
Use a design patter
ns
factory method
to build the events

see
Thinking in
Patterns with Java
, downloadable at
www.BruceEckel.com
.
)



178.]

Add
Rhomboid
to
Shapes.java
. Create a
Rhomboid
, upcast it to a
Shape
, then downcast it back to a
Rhomboid
. Try downcasting to a
Circle

and
see what happens.



179.]

Modify Exercise 1 so that it uses
instanceof

to check the type before
performing th
e downcast.


180.]

Modify
Shapes.java

so that it can “highlight” (set a flag) in all shapes of a
particular type. The
toString(

)

method for each derived
Shape

should indicate
whether that
Shape
is “highlighted.”


181.]

Modify
SweetShop.java

so that each

type of object creation is controlled
by a command
-
line argument. That is, if your command line is “
java
SweetShop Candy
,” then only the
Candy

object is created. Notice how you can
control which
Class

objects are loaded via the command
-
line argument.



18
2.]

Add a new type of
Pet

to
PetCount3.java
. Verify that it is created and
counted correctly in
main(

)
.



183.]
Write a method that takes an object and recursively prints all the classes in
that object’s hierarchy.



184.]

Modify Exercise 6 so that it use
s
Class.
getDeclaredFields
(

)

to also
display information about the fields in a class.



185.]

In
ToyTest.java
, comment out
Toy
’s default constructor and explain
what happens.



186.]

Incorporate a new kind o
f
interface

into
ToyTest.java

and verify that it
is detected and displayed properly.



187.]

Create a new type of container that uses a
private

ArrayList

to hold the
objects.

Capture the type of the first object you put in it, and then allow the user
to in
sert objects of only that type from then on.


188.]

Write a program to determine whether an array of
char

is a primitive type
or a true object.



189.]

Implement
clearSpitValve(

)

as described in the summary.


190.]

Implement the
rotate(Shape)

method des
cribed in this chapter, such
that it checks to see if it is rotating a
Circle

(and, if so, doesn’t perform the
operation).



191.]

Modify Exercise 6 so that it uses reflection instead of RTTI.



192.]

Modify Exercise 7 so that it uses reflection instead of

RTTI.



193.]

In
ToyTest.java
, use reflection to create a
Toy

object using the
nondefault constructor.

194.]


Look up the interface for
java.lang.Class

in the HTML Java
documentation from
java.sun.com
. Write a program that takes the name of a
class as a
command
-
line argument, then uses the
Class

methods to dump all
the information available for that class. Test your program with a standard
library class and a class you create.


195.]

Create an applet/application using the
Console

class as shown in this
c
hapter. Include a text field and three buttons. When you press each button,
make some different text appear in the text field.



196.]

Add a check box to the applet created in Exercise 1, capture the event, and
insert different text into the text field.



197.]

Create an applet/application using
Console
. In the HTML documentation
from
java.sun.com
, find the
JPasswordField

and add this to the program. If
the user types in the correct password, use
Joptionpane

to provide a success
message to the user.



198.]

Create an applet/application using
Console
, and add all the components
that have an
addActionListener(

)

method. (Look these up in the HTML
documentation from
java.sun.com
. Hint: use the index.) Capture their events
and display an appropriate message for
each inside a text field.



199.]

Create an applet/application using
Console
, with a
JButton

and a

JTextField
. Write and attach the appropriate listener so that if the button has
the focus, characters typed into it will appear in the

JTextField
.


200.]

Create an applet/application

using
Console
. Add to the main frame all
the components described in this chapter, including menus and a dialog box.


201.]

Modify
TextFields.java

so that the characters in
t2

retain the original
case that they were typed in, instead of automatically bei
ng forced to upper case.



202.]

Locate and download one or more of the free GUI builder development
environments available on the Internet, or buy a commercial product. Discover
what is necessary to add
BangBean

to this environment and to use it.



203.]

Add
Frog.class

to the manifest file as shown in this chapter and run
jar

to create a JAR file containing both
Frog

and
BangBean
. Now either download
and install the BDK from Sun or use your own Beans
-
enabled program builder
tool and add the JAR file to you
r environment so you can test the two Beans.



204.]

Create your own JavaBean called
Valve
that contains two properties: a
boolean

called “on” and an
int

called “level.” Create a manifest file, use
jar

to
package your Bean, then load it into the beanbox or into a Beans
-
enabled
program builder tool so

that you can test it.


205.]

Modify
MessageBoxes.java

so that it has an individual
ActionListener

for each button (instead of matching the button text).



206.]

Monitor a new type of event in
TrackEvent.java

by adding the new event
handling code. You’ll
need to discover on your own the type of event that you
want to monitor.



207.]

Inherit a new type of button from
JButton
. Each time you press this
button, it should change its color to a randomly
-
selected value. See
ColorBoxes.java

in Chapter 14 for an e
xample of how to generate a random
color value.


208.]

Modify
TextPane.java

to use a
JTextArea

instead of a
JTextPane
.


209.]

Modify
Menus.java

to use radio buttons instead of check boxes on the
menus.



210.]

Simplify
List.java

by passing the array to t
he constructor and eliminating
the dynamic addition of elements to the list.


211.]

Modify
SineWave.java
to turn
SineDraw

into a JavaBean by adding
“getter” and “setter” methods.



212.]

Remember the “sketching box” toy with two knobs, one that controls t
he
vertical movement of the drawing point, and one that controls the horizontal
movement? Create one of those, using
SineWave.java
to get you started.
Instead of knobs, use sliders. Add a button that will erase the entire sketch.



213.]

Create an “asympto
tic progress indicator” that gets slower and slower as it
approaches the finish point. Add random erratic behavior so it will periodically
look like it’s starting to speed up.



214.]

Modify
Progress.java

so that it does not share models, but instead uses
a
listener to connect the slider and progress bar.


215.]

Follow the instructions in the section titled “Packaging an applet into a
JAR file” to place
TicTacToe.java

into a JAR file. Create an HTML page with
the (messy, complicated) version of the applet
tag, and modify it to use the
archive tag so as to use the JAR file. (Hint: start with the HTML page for
TicTacToe.java
that comes with this book’s source
-
code distribution.)



216.]

Create an applet/application using
Console
. This should have three
slider
s, one each for the red, green, and blue values in
java.awt.Color
. The rest
of the form should be a
JPanel

that displays the color determined by the three
sliders. Also include non
-
editable text fields that show the current RGB values.



217.]

In the HTML
documentation for
javax.swing
, look up the
JColorChooser
. Write a program with a button that brings up the color chooser
as a dialog.



218.]

Almost every Swing component is derived from
Component
, which has a
setCursor(

)

method. Look this up in the Java
HTML documentation. Create an
applet and change the cursor to one of the stock cursors in the
Cursor

class.

219.]


Starting with
ShowAddListeners.java
, create a program with the
full functionality of
ShowMethodsClean.java

from Chapter 12.



220.]

Inherit
a class from
Thread

and override the
run(

)

method. Inside
run(

)
, print a message, and then call
sleep(

)
. Repeat this three times, then
return from
run(

)
. Put a start
-
up message in the constructor and override
finalize(

)

to print a shut
-
down message. M
ake a separate thread class that calls
System.gc(

)

and
System.runFinalization(

)
inside
run(

)
, printing a
message as it does so. Make several thread objects of both types and run them to
see what happens.



221.]

Modify
Sharing2.java

to add a
synchronized

block inside the
run(

)

method of
TwoCounter

instead of synchronizing the entire
run(

)

method.



222.]

Create two
Thread

subclasses, one with a
run(

)

that starts up, captures
the reference of the second
Thread

object and then calls
wait(

)
. The other
class’
run(

)

should call
notifyAll(

)

for the first thread after some number of
seconds have passed, so the first thread can print a message.


223.]

In
Counter5.java

inside
Ticker2
, remove the
yield(

)

and
explain the
results. Replace the
yield(

)

with a
sleep(

)

and explain the results.


224.]

In
ThreadGroup1.java
, replace the call to
sys.suspend(

)

with a call to
wait(

)

for the thread group, causing it to wait for two seconds. For this to work
correctly
you must acquire the lock for
sys
inside a
synchronized

block.


225.]

Change
Daemons.java

so that
main(

)

has a
sleep(

)

instead of a
readLine(

)
. Experiment with different sleep times to see what happens.



226.]

In Chapter 8, locate the
GreenhouseContro
ls.java

example, which
consists of three files. In
Event.java
, the class
Event

is based on watching the
time. Change
Event

so that it is a
Thread
, and change the rest of the design so
that it works with this new
Thread
-
based
Event
.



227.]

Modify Exercise
7 so that the
java.util.Timer

class found in JDK 1.3 is
used to run the system.



228.]

Starting with
SineWave.java

from Chapter 13, create a program (an
applet/application using the
Console
class) that draws an animated sine wave
that appears to scrolls p
ast the viewing window like an oscilloscope, driving the
animation with a
Thread
. The speed of the animation should be controlled with
a
java.swing.JSlider

control.



229.]

Modify Exercise 9 so that multiple sine wave panels are created within the
applicat
ion. The number of sine wave panels should be controlled by HTML tags
or command
-
line parameters.

230.]


Modify Exercise 9 so that the
java.swing.Timer

class is used to drive
the animation. Note the difference between this and
java.util.Timer
.



231.]

Com
pile and run the
JabberServer

and
JabberClient

programs in this
chapter. Now edit the files to remove all of the buffering for the input and output,
then compile and run them again to observe the results.


232.]

Create a server that asks for a password, t
hen opens a file and sends the
file over the network connection. Create a client that connects to this server, gives
the appropriate password, then captures and saves the file. Test the pair of
programs on your machine using the
localhost

(the local loopba
ck IP address
127.0.0.1

produced by calling
InetAddress.getByName(null)
).



233.]

Modify the server in Exercise 2 so that it uses multithreading to handle
multiple clients.



234.]

Modify
JabberClient.java

so that output flushing doesn’t occur and
observe the effect.



235.]
Modify
MultiJabberServer

so that it uses
thread pooli
ng
. Instead of
throwing away a thread each time a client disconnects, the thread should put
itself into an “available pool” of threads. When a new client wants to connect, the
server will look in the available pool for a thread to handle the request, and i
f one
isn’t available, make a new one. This way the number of threads necessary will
naturally grow to the required quantity. The value of thread pooling is that it
doesn’t require the overhead of creating and destroying a new thread for each
new client.



236.]

Starting with
ShowHTML.java
, create an applet that is a password
-
protected gateway to a particular portion of your Web site.



237.]
Modify
CIDCreateTables.java

so that it reads the SQL strings from a
text file instead of
CIDSQL
.


238.]

Configure
your system so that you can successfully execute
CIDCreateTables.java

and
LoadDB.java
.


239.]

Modify
ServletsRule.java

by overriding the
destroy(

)

method to save
the value of
i

to a file, and and the
init(

)

method to restore the value.
Demonstrate that
it works by rebooting the servlet container. If you do not have
an existing servlet container, you will need to download, install, and run Tomcat
from
jakarta.apache.org

in order to run servlets.



240.]

Create a servlet that adds a cookie to the response
object, thereby storing it
on the client’s site. Add code to the servlet that retrieves and displays the cookie.
If you do not have an existing servlet container, you will need to download,
install, and run Tomcat from
jakarta.apache.org

in order to run se
rvlets.



241.]

Create a servlet that uses a
Session

object to store session information of
your choosing. In the same servlet, retrieve and display that session information.
If you do not have an existing servlet container, you will need to download,
install, and

run Tomcat from
jakarta.apache.org

in order to run servlets.



242.]

Create a servlet that changes the inactive interval of a session to 5 seconds
by calling
getMaxInactiveInterval(

)
. Test to see that the session does indeed
expire after 5 seconds. If yo
u do not have an existing servlet container, you will
need to download, install, and run Tomcat from
jakarta.apache.org

in order to
run servlets.


243.]

Create a JSP page that prints a line of text using the <H1> tag. Set the
color of this text randomly,
using Java code embedded in the JSP page. If you do
not have an existing JSP container, you will need to download, install, and run
Tomcat from
jakarta.apache.org

in order to run JSPs.



244.]
Modify the maximum age value in
Cookies.jsp

and observe the beh
avior
under two different browsers. Also note the difference between just re
-
visiting the
page, and shutting down and restarting the browser. If you do not have an
existing JSP container, you will need to download, install, and run Tomcat from
jakarta.apac
he.org

in order to run JSPs.



245.]

Create a JSP with a field that allows the user to enter the session
expiration time and and a second field that holds data that is stored in the
session. The submit button refreshes the page and fetches the current expiration
time and session data
and puts them in as default values of the aforementioned
fields. If you do not have an existing JSP container, you will need to download,
install, and run Tomcat from
jakarta.apache.org

in order to run JSPs.

246.]


(More challenging) Take the
VLookup.java

program and modify it so
that when you click on the resulting name it automatically takes that name
and copies it to the clipboard (so you can simply paste it into your email).
You’ll need to look back at Chapter 13 to remember how to use the clipboard
in

JFC.


247.]

Compile and run the
JabberServer

and
JabberClient

programs in this
chapter. Now edit the files to remove all of the buffering for the input and output,
then compile and run them again to observe the results.


248.]

Create a server that asks for a
password, then opens a file and sends the
file over the network connection. Create a client that connects to this server, gives
the appropriate password, then captures and saves the file. Test the pair of
programs on your machine using the
localhost

(the l
ocal loopback IP address
127.0.0.1

produced by calling
InetAddress.getByName(null)
).


249.]

Modify the server in Exercise 2 so that it uses multithreading to handle
multiple clients.



250.]

Modify
JabberClient.java

so that output flushing doesn’t occur and
observe the effect.



251.]

Modify
MultiJabberServer

so that it uses
thread pooli
ng
. Instead of
throwing away a thread each time a client disconnects, the thread should put
itself into an “available pool” of threads. When a new client wants to connect, the
server will look in the available pool for a thread to handle the request, and i
f one
isn’t available, make a new one. This way the number of threads necessary will
naturally grow to the required quantity. The value of thread pooling is that it
doesn’t require the overhead of creating and destroying a new thread for each
new client.



252.]

Starting with
ShowHTML.java
, create an applet that is a password
-
protected gateway to a particular portion of your Web site.



253.]

Modify
CIDCreateTables.java

so that it reads the SQL strings from a
text file instead of
CIDSQL
.



254.]
Configure
your system so that you can successfully execute
CIDCreateTables.java

and
LoadDB.java
.



255.]

Modify
ServletsRule.java

by overriding the
destroy(

)

method to save
the value of
i

to a file, and and the
init(

)

method to restore the value.
Demonstrate that
it works by rebooting the servlet container. If you do not have
an existing servlet container, you will need to download, install, and run Tomcat
from
jakarta.apache.org

in order to run servlets.



256.]

Create a servlet that adds a cookie to the response
object, thereby storing it
on the client’s site. Add code to the servlet that retrieves and displays the cookie.
If you do not have an existing servlet container, you will need to download,
install, and run Tomcat from
jakarta.apache.org

in order to run se
rvlets.



257.]

Create a servlet that uses a
Session

object to store session information of
your choosing. In the same servlet, retrieve and display that session information.
If you do not have an existing servlet container, you will need to download,
install, and

run Tomcat from
jakarta.apache.org

in order to run servlets.


258.]

Create a servlet that changes the inactive interval of a session to 5 seconds
by calling
getMaxInactiveInterval(

)
. Test to see that the session does indeed
expire after 5 seconds. If yo
u do not have an existing servlet container, you will
need to download, install, and run Tomcat from
jakarta.apache.org

in order to
run servlets.



259.]

Create a JSP page that prints a line of text using the <H1> tag. Set the
color of this text randomly,
using Java code embedded in the JSP page. If you do
not have an existing JSP container, you will need to download, install, and run
Tomcat from
jakarta.apache.org

in order to run JSPs.


260.]

Modify the maximum age value in
Cookies.jsp

and observe the beh
avior
under two different browsers. Also note the difference between just re
-
visiting the
page, and shutting down and restarting the browser. If you do not have an
existing JSP container, you will need to download, install, and run Tomcat from
jakarta.apac
he.org

in order to run JSPs.


261.]

Create a JSP with a field that allows the user to enter the session
expiration time and and a second field that holds data that is stored in the
session. The submit button refreshes the page and fetches the current expiration
time and session data
and puts them in as default values of the aforementioned
fields. If you do not have an existing JSP container, you will need to download,
install, and run Tomcat from
jakarta.apache.org

in order to run JSPs.

262.]


(More challenging) Take the
VLookup.java

program and modify it so
that when you click on the resulting name it automatically takes that name
and copies it to the clipboard (so you can simply paste it into your email).
You’ll need to look back at Chapter 13 to remember how to use the clipboard
in

JFC.

1.
Create a directory on your machine that will be your sandbox for this exercise. We’ll use

C:
\
BJEE5
\
Ch02
in this example.

2.
Create a new file called
index.jsp
in that directory using your favorite text editor. Here’s

the code for that file:

<%
--

f
ile: index.jsp

desc: Test installation of Java EE SDK 5

--
%>

<html>

<head>

<title>Hello World
-

test the Java EE SDK installation

</title>

</head>

<body>

<%

for (int i = 1; i < 5; i++)

{

%>

36
CHAPTER 2

GETTING STARTED

<h<%=i%>>Hello World</h<%=i%>>

<%

}

%>

</body>

</html>

3.
Create a subdirectory called
META
-
INF
, and in this directory create a file called

application.xml
. This file contains settings used to identify your application and the

resources it depends on to JBoss, and to configure how users wil
l access said resources.

Create this file with the following contents:

<?xml version="1.0"?>

<application>

<display
-
name>Hello Java EE World!</display
-
name>

<module>

<web>

<web
-
uri>web
-
app.war</web
-
uri>

<context
-
root>/hello</context
-
root>

</web>

</module>

</application>

4.
You need to create a new WAR and EAR file. The WAR file will contain the web components

of the Java EE application, along with a descriptor or “table of contents” that

describes what is in the archive. Web applications frequently consist
of many more files

than this simple application, and the WAR is a convenient means of bundling up all of

those files into a single file for deployment. Likewise, an EAR file is a collection of WAR

files, JAR files, and resources that are all meant to opera
te within the context of a single

application. To create these files, open a command
-
line window, change your current

directory to the folder you created for this example (e.g.,
cd
\
BJEE5
\
Ch02
), and type the

following two commands:

>jar cf web
-
app.war index
.jsp

>jar cf helloworld.ear web
-
app.war META
-
INF

5.
Copy the resultant EAR file,
helloworld.ear
, to your JBoss server deployment directory

(
C:
\
jboss
\
server
\
all
\
deploy
) and start JBoss from the command line with the command

shown in Figure 2
-
7.

6.
It’s time

to test your first JSP page. Start a web browser and open the following URL:

http://localhost:8080/hello

After a couple of seconds, you should see the web page shown in Figure 2
-
8.

35)

Congratulations! Your first JSP page is a success.

1.
Implement a client w
eb page that has numerous input elements such as text fields,

buttons, check boxes, radio buttons, and so on. Using only EL, create a JSP page that

echoes back to the caller the request parameters from the request sent by the web page.

2.
Modify
Jsp_Ex05
t
o include two additional topics in addition to the EL topics. Also, add

a page that the user can use to select the desired topic.

3.
Write two custom actions, where one action is nested inside the other action. For

example, with standard actions, a
<param>

tag is nested inside a
<useBean>
tag. Do

something similar with your tags.

4.
When using a classic tag handler, investigate if there is any difference in how the tag

handler methods are called for the two different forms of the empty tag:
<empty/>
and

<em
pty></empty>
. Perform the same investigation for simple tag handlers.

5.
Develop a JSP that uses the
sql
tag library of the JSTL to communicate with a database.

(You may want to defer this exercise until after completing Chapter 7.)

6.
Use the Jakarta tag
libraries (
http://jakarta.apache.org/taglibs/index.html
) to

create one or more JSP pages. Here are some ideas:

7
• Use the Scrape tag library to search and scrape book information from an online

bookstore.

8
• Use the I18N library to create a JSP that displa
ys in two or more languages based on

user preference.

9
• Use the Random tag library to create a web application that simulates a dice rolling

game or some other game that relies on chance.

1.
If your database does support stored procedures, find an existin
g stored procedure, or

create one of your own, and write JDBC code that calls the stored procedures.

2.
Create a database table containing some number of columns of various types. Create a

program that inserts to the table using a
Statement
and a
PreparedS
tatement
. Make

timing measurements to determine the performance difference between the two.

3.
If your database has multiple driver types (for example, you can use either a Type 2 or a

Type 4), create a database table and perform various insertions, deleti
ons, and updates

to the rows. Make timing measurements to determine if one driver has a performance

advantage.

4.
Create a program that makes multiple insertions into a database table. Use the default

autocommit setting and measure the performance of inser
ting data this way. Now set

the autocommit setting to
false
and commit only after all data has been inserted.

36)

Measure the performance of this and compare it to autocommitting every insertion

1.
Create an entity bean for a fictitious Audio CD Collection app
lication named

CompactDiscTitle
. Attributes should be
name
(String, and it will be the primary key) and

price
(double). Write a stateless session bean that allows you to get, add, update, and

remove a CD title. Write a client application to test the beans.

37)

2.
Modify the previous exercise to use local references for the
CompactDiscTitle
entity bean

3.
Modify the previous exercise to implement a finder that returns all CD titles in ascending

order by name, and a finder that returns all CD titles within a cert
ain price range in

ascending order by name. Write a simple client application to test the new methods.

4.
Modify the EJB QL stock list example presented at the end of this chapter to return a list

of stock tickers that start with a string entered by the us
er. Order the list as ascending by

38)

ticker symbol.

39)

Write an algorithm in informal pseudo
-
code to compute the perimeter of a
triangle.

40)

Write the flowchart description for the problem to compute the perimeter of a
triangle.

41)

Write a complete KJP program for co
mputing the area of a triangle. Use only two
classes.

42)

Write a complete algorithm in pseudo
-
code that computes the average of grades
for students.

43)

Write the complete KJP program for the problem that computes the grades of
students.

44)

Write an algorithm in inf
ormal pseudo
-
code that calculates a bonus for
employees; the bonus is calculated to be 1.75% of the current salary.

45)

Write the complete program in KJP for the problem that calculates a bonus for
employees. Use only assignment and input/output statements.

46)

Wr
ite an algorithm in flowchart and in informal pseudo
-
code to compute the
conversion from inches to centimeters.

47)

Write an algorithm in flowchart and in informal pseudo
-
code to compute the
conversion from centimeters to inches.

48)

Write an algorithm in flowchar
t and in informal pseudo
-
code to compute the
conversion from a temperature reading in degrees Fahrenheit to Centigrade.

49)

Write a complete algorithm in form of a flowchart and informal pseudo
-
code to
compute the conversion from a temperature reading in degre
es Centigrade to
Fahrenheit.

50)

Write a complete program in KJP to compute the conversion from inches to
centimeters.

51)

Write a complete program in KJP to compute the conversion from centimeters to
inches.

52)

Write a complete program in KJP to compute the conversi
on from a temperature
reading in degrees Fahrenheit to Centigrade.

53)

Write a complete program in KJP to compute the conversion from a temperature
reading in degrees Centigrade to Fahrenheit.

54)

Write a complete program in KJP that combines the conversions from
inches to
centimeters and from centimeters to inches.

55)

Write a complete program in KJP that combines the two types of temperature
conversion.

56)

Write a complete program in KJP that combines the calculation of the perimeter
and the area of a triangle.

57)

Write a
complete algorithm in flowchart and pseudo
-
code to compute the distance
between two points. Each point,
P
, is defined by a pair of values (
x
,
y
). The
algorithm must check that the point values are different than zero, and that the
two points are not the sa
me. The distance,
d
, between two points,
P
1
(
x
1
,
y
1
) and
P
2
(
x
2
,
y
2
) is given by the expression:


58)

Given four numbers, find the largest one. Write a complete algorithm in pseudo
-
code that reads the four numbers and prints the largest one.

59)

Given four numbers, find the smallest one. Write a complete algo
rithm in pseudo
-
code that reads the four numbers and prints the smallest one.

60)

A car rental agency charges $34.50 per day for a particular vehicle. This amount
includes up to 75 miles free. For every additional mile, the customer must pay