Java Reference
In-Depth Information
9. The program in Display 19.12 creates a database with the Names table
from Display 19.10. Modify this program so it also creates the Titles and
BooksAuthors tables with identical data entries as shown in Display 19.10.
Next, create a separate program that prompts the user to input the name of an
author and then outputs all book titles written by that author.
10. Create an HTML form that prompts the user to enter a temperature in Fahrenheit.
When the form is submitted, a JSP Web page should run that displays the tempera-
ture converted to Celsius. The temperature in Celsius is 5 * (Fahrenheit - 32) / 9.
11. Create an HTML form that serves as a random baby name generator. The form
should prompt the user to enter a last name. When the form is submitted, a JSP
Web page should run that randomly picks the first name of a boy and the first
name of a girl and then outputs the random first names coupled with the entered
last name. For example, if the last name entered is “Savitch” and if the JSP program
randomly selects “Emma” as the girl name and “Homer” as the boy name, then the
output would be
VideoNote
Solution to
Programming
Project 19.11
If your baby is a boy, consider the name Homer Savitch.
If your baby is a girl, consider the name Emma Savitch.
The boy and girl names should be randomly selected from the files boynames.txt
and girlnames.txt that are included in the source code on the website for this topic.
These files contain the 1,000 most popular boy and girl names in the United States
for the year 2003 as compiled by the Social Security Administration.
12. Recreate Programming Project 19.9 as JSP pages instead of as a stand-alone
application. One JSP page should create the database, tables, and populate the
tables with data. Create an HTML form that allows the user to enter the name of
an author. The form should invoke another JSP page that displays all titles written
by the specified author that are stored in the database.
13. This program simulates what might happen if two people who share the same bank
account happen to make a simultaneous deposit or withdrawal and the bank does
not account for race conditions by recreating the situation described in Displays 19.4
and 19.5 with a simple BankAccount class. The BankAccount class should store
an account balance and have methods to retrieve the balance, make a deposit, and
make a withdrawal. Do not worry about negative balances.
Next, create an array of thousands of threads where each thread has a reference
to the same BankAccount object. In the run() method, even numbered threads
deposit one dollar and odd numbered threads withdraw one dollar. If you create
an even number of threads, then after all threads are done the account balance
should be zero. See if you can find a number of threads so that you consistently end
up with a balance that is not zero. If you want to increase the likelihood of a race
condition, then make each thread sleep a short random number of milliseconds in
the run() method.
Add the synchronized keyword to fix the problem and ensure a balance of zero
after all the threads are done.
Search WWH ::




Custom Search