NB! The practical session exercises are always provided enough and to spare with an eye to fast / experienced students have exercises to solve. To this end, do not get upset if you do not get on all the task in class. Take your time and complete the tasks at home!
Task 1. Upgrade the TextAnalyzer
Upgrade 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 friend 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 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?
Task 3. Sample task of the second test.
Solve the task.