Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

CODE IN JAVA (Generic Dictionary) Create a generic class with two parameters called Dictionary . The first type corresponds to something called key and the

CODE IN JAVA

(Generic Dictionary) Create a generic class with two parameters called Dictionary. The first type corresponds to something called key and the

second type corresponds to something called value. This dictionary can store key- value pairs.

In a dictionary, keys are always unique, a certain key exists only once. On the other hand, a value is always associated with a key, but they values are not necessarily unique. For example, we can create a dictionary where keys are strings and values are integers. This dictionary represents frequencies (in other words values) of words (in other words keys) in a text file. {hello => 3, world => 4, the => 10, application => 4} The Dictionary class contains the following methods: void put(T key, U value) that associates the value of type U with the key of type T. If the given key exists in the dictionary, update its value to the new entry. boolean exists(T key) that checks whether the given key exists. U get(T key) that returns the value associated with the provided key. If the key doesnt exist, throw a custom exception called KeyNotDefinedException. int size() that returns the number of key-value pairs in the dictionary. U remove(T key) that removes the key-value pair from the dictionary (not just one of them but both). If the doesnt exist in the dictionary, throw KeyNotDefinedException. T[] keys() that returns an array with only keys currently in the dictionary.

U[] values() that returns an array with only values currently in the dictionary After creating the Dictionary class, write a testing class and include the following lines. The result should be as following: Dictionary foo = new Dictionary<>(); foo.put("id", "J90"); foo.put("name", "Foo"); foo.put("age", 90); foo.put("alive", true); System.out.println(foo.get("id")); // J90 System.out.println(foo.get("name")); // Foo System.out.println(foo.get("age")); // 90 System.out.println(foo.get("alive")); // true foo.put("name", "Bar"); System.out.println(foo.get("name")); // Bar System.out.println(foo.remove("alive")); // true System.out.println(Arrays.toString(foo.keys())); // [id, name, age] System.out.println(Arrays.toString(foo.values())); // [J90, Bar, 90] System.out.println(foo.get("alive"));

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions