Task 1. Upgrade the TextAnalyzer
Modify the first task of the home assignment as follows. Add a method without parameters called wordSequences
. The method has to return info about word sequences in an appropriate data structure. For example, if the text to analyze is "A bird in the hand is worth two in the bush", then the program output should be:
{a={bird=1}, bird={in=1}, in={the=2}, the={hand=1, bush=1}, hand={is=1}, is={worth=1}, worth={two=1}, two={in=1}, hand={is=1}, bush={}}
Hint: an appropriate data structure for the task is most probably Map<String, Map<String, Integer>>
.
Task 2. Birthday party
A student wants to celebrate his birthday and organizes a memorable birthday party. The student has asked each of his friends to bring another friend along to the party. As a result, many people have come to the party: birthday boy's friends, the friends of the friends and so on. In the following file, there are two names on each line: inviter's name and his friend's name (each name consists of one word, the names are separated by a space).
A sample file with guests:
Martin Kelly Herbert Jack Kelly Paul Jack Mark Paul Jane
Write a program into a class called BirthdayParty
. The main method of the client class takes in two parameters from the command line: the name of a file and the name of a guest. The program has to find and print out the name of the initial guest (birthday's boy's friend). You can assume that the file is always written using the UTF-8 encoding.
An example of the program execution:
> java BirthdayParty guest.txt Jane Martin
The output of the program is Martin because Jane is invited by Paul, Paul is invited by Kelly, and Kelly is invited by Martin. Martin is at the top of the inviters list for Jane.
Hint: first of all, read the data into a map. Think about how to organize the map. Is the first name of the line a key or a value?