Lab 14.1 Suppose you write a program to keep track of the friends of all the stu
ID: 3606543 • Letter: L
Question
Lab 14.1
Suppose you write a program to keep track of the friends of all the students in your class using a HashMap. What would you use for the keys? What would you use for the values?
Type your answers in the space provided below.
Lab 14.2
Write a program named Friends that stores the following information in a HashMap:
Sue is friends with Bob, Jose, Alex, and Cathy
Cathy is friends with Bob and Alex
Bob is friends with Alex, Jose, and Jerry
After storing the information, keep prompting the user to enter a name until they enter "quit".
If the name that is a key in the HashMap, print out the name and the list of friends.
Otherwise print a message indicating that the name is not in the HashMap.
Sample Run (user input in color):
Enter a name ('quit' to end): Bob
Here are the friends of Bob:
[Alex, Jose, Jerry]
Enter a name ('quit' to end): Cathy
Here are the friends of Cathy:
[Alex, Bob]
Enter a name ('quit' to end): Sue
Here are the friends of Sue:
[Alex, Bob, Jose, Cathy]
Enter a name ('quit' to end): Pete
Awww, Pete has no friends :(
Enter a name ('quit' to end): Quit
OK, bye for now!
Explanation / Answer
Lab 14.1
Suppose you write a program to keep track of the friends of all the students in your class using a HashMap. What would you use for the keys? What would you use for the values?
Ans:
key of HashMap will be name of the Student. It will of Strign type
values will be of type List of friends. It will be ArrayList of String.
2)
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class Friends {
public static void main(String[] args) {
HashMap<String, ArrayList<String>> friends = new HashMap<>();
ArrayList<String> list1 = new ArrayList<>();
list1.add("Bob");
list1.add("Jose");
list1.add("Alex");
list1.add("Cathy");
ArrayList<String> list2 = new ArrayList<>();
list2.add("Bob");
list2.add("Alex");
ArrayList<String> list3 = new ArrayList<>();
list3.add("Alex");
list3.add("Jose");
list3.add("Jerry");
friends.put("Sue", list1);
friends.put("Cathy", list2);
friends.put("Bob", list3);
Scanner sc = new Scanner(System.in);
while(true) {
System.out.print("Enter a name ('quit' to end): ");
String name = sc.next();
if("quit".equalsIgnoreCase(name))
break;
if(friends.containsKey(name)) {
System.out.println("Here are the friends of "+name+":");
System.out.println(friends.get(name));
}else{
System.out.println("Awww, "+name+" has no friends :(");
}
}
sc.close();
System.out.println("OK, bye for now!");
}
}
/*
Sample run:
Enter a name ('quit' to end): Sue
Here are the friends of Sue:
[Bob, Jose, Alex, Cathy]
Enter a name ('quit' to end): Bob
Here are the friends of Bob:
[Alex, Jose, Jerry]
Enter a name ('quit' to end): Rte
Awww, Rte has no friends :(
Enter a name ('quit' to end): Quit
OK, bye for now!
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.