SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Collections Maps And Iterators
Exercise:
Programming Projects
Question:7 | ISBN:9780132830317 | Edition: 5

Question

In social networking websites, people link to their friends to form a social network. Write a program that uses HashMaps to store the data for such a network. Your program should read from a file that specifies the network connections for different usernames. The file should have the following format to specify a link:

source_usernamefriend_username

There should be an entry for each link, one per line. Here is a sample file for five usernames:

iba java_guru

iba crisha

iba ducky

crisha java_guru

crisha iba

ducky java_guru

ducky iba

java_guru iba

java_guru crisha

java_guru ducky

wittless java_guru

In this network, everyone links to java_guru as a friend. iba is friends with java_guru , crisha , and ducky . Note that links are not bidirectional; wittless links with java_guru but java_guru does not link with wittless.

First, create a User class that has an instance variable to store the user’s name and another instance variable that is of type HashSet<User>. The HashSet<User> variable should contain references to the User objects that the current user links to. For example, for the user iba there would be three entries, for java_guru, crisha, and ducky. Second, create a HashMap<String, User> instance variable in your main class that is used to map from a username to the corresponding User object. Your program should ]do the following:

• Upon startup, read the data file and populate the HashMap and HashSet data structures according to the links specified in the file.

• Allow the user to enter a name.

• If the name exists in the map, then output all usernames that are one link away from the user entered.

• If the name exists in the map, then output all usernames that are two links away from the user entered. To accomplish this in a general way, you might consider writing a recursive subroutine.

Do not forget that your User class must override the hashCode and equals methods.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Sorry the answer is not available at the moment…

If you are able to find the answer, please make sure to post it here. So that your Juniors have smile on their lips and feel happy.

Spread the 'tradition of sharing'.