Java Reference
In-Depth Information
space followed by the number of registered births in the year using that name. For
example, the
girlnames.txt
file begins with
Emily 25494
Emma 22532
This indicates that Emily is the most popular name with 25,494 registered nam-
ings, Emma is the second most popular with 22,532, and so on.
Write a program that determines how many names are on both the boys' and
the girls' list. Use the following algorithm:
•
Read each girl name as a String, ignoring the number of namings, and add it to
a
HashSet
object.
•
Read each boy name as a String, ignoring the number of namings, and add it to
the same
HashSet
object. If the name is already in the
HashSet
, then the
add
method returns
false
. If you count the number of
false
returns, then this
gives you the number of common namings.
•
Add each common name to an
ArrayList
and output all of the common names
from this list before the program exits.
5. Repeat the previous problem except create your own class,
Name
, that is added to a
HashMap
instead of a
HashSet
. The
Name
class should have three private variables,
a String to store the name, an integer to store the number of namings for girls,
and an integer to store the number of namings for boys. Use the first name as the
key to the
HashMap
. The value to store is the
Name
object. Instead of ignoring the
number of namings, as in the previous project, store the number in the
Name
class.
Make the
ArrayList
a list of
Name
objects; each time you find a common name,
add the entire
Name
object to the list. Your program should then iterate through
the
ArrayList
and output each common name, along with the number of boy and
girl namings.
6. In an ancient land, the beautiful princess Eve had many suitors. She decided on
the following procedure to determine which suitor she would marry. First, all of
the suitors would be lined up one after the other and assigned numbers. The first
suitor would be number 1, the second number 2, and so on up to the last suitor,
number
n
. Starting at the first suitor, she would then count three suitors down
the line (because of the three letters in her name) and the third suitor would be
eliminated from winning her hand and removed from the line. Eve would then
continue, counting three more suitors, and eliminating every third suitor. When
she reached the end of the line, she would reverse direction and work her way back
to the beginning. Similarly, on reaching the first person in line, she would reverse
direction and make her way to the end of the line.
For example, if there were five suitors, then the elimination process would
proceed as follows:
VideoNote
Solution to
Programming
Project 16.5
12345
Initial list of suitors; start counting from 1.
1245
Suitor 3 eliminated; continue counting from 4 and bounce from end
back to 4.