Java Reference
In-Depth Information
This indicates that Emily is the most popular name with 25,494 registered
namings, 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 will return 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 vari-
ables, 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 ignor-
ing 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 com-
mon name, add the entire Name object to the list. Your program should then iter-
ate 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 suit-
ors, 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. Simi-
larly, 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 5 suitors then the elimination process would
proceed as follows:
12345
initial list of suitors, start counting from 1
1245
suitor 3 eliminated, continue counting from 4, bounce from end
back to 4
125
suitor 4 eliminated, continue counting back from 2, bounce from
front back to 2
15
suitor 2 eliminated, continue counting forward from 5
1
suitor 5 eliminated, 1 is the lucky winner
Search WWH ::




Custom Search