Write a program that first creates a dictionary object student into with the fol
ID: 3732124 • Letter: W
Question
Write a program that first creates a dictionary object student into with the following information (ID used as the key; Name used as the value): Student ID Student Name 001 002 003 Alice Bob Charlie Then, perform the following tasks: (1) Add the key-value pairs (004, David) and (001, Alex) into the student_into. (2) Use the del statement to remove the element with key '005. Use in or not in operator to avoid KeyError exception as necessary. (3) Use get method to retrieve and print the value associated with key '001 (4) Use pop method to retrieve and print the value associated with key '003'. Note that this will pop out the key-value pair of key 003 (5) Use items method to return and print all keys and corresponding values. value associated with key 001 is Alex value associated with key 003 is Charlie 001 Alex 002 Bob 004 DavidExplanation / Answer
Dictionary.java:
import java.util.Hashtable;
import java.util.Set;
public class Dictionary {
/**
* @param args
*/
@SuppressWarnings("serial")
public static void main(String[] args) {
// TODO Auto-generated method stub
// Created student_into with the initial Student Id and Student Names
Hashtable<String, String> student_into =
new Hashtable<String, String>(){{put("001","Alice");put("002","Bob");put("003","Charie");}};
// Adding the Value pairs David and Alex
student_into.put("004","David");
student_into.put("001","Alex");
// Deleting the key 005
student_into.remove("005");
//Printing the key 001
System.out.println("Value assosicated with key 001 is "+student_into.get("001"));
//Printing the key 003
System.out.println("Value assosicated with key 003 is "+student_into.remove("003"));
//Printing all the entries of studet_into
Set<String> keys = student_into.keySet();
for(String key: keys){
System.out.println(key+" "+student_into.get(key));
}
}
}
Output:
Value assosicated with key 001 is Alex
Value assosicated with key 003 is Charie
004 David
002 Bob
001 Alex
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.