Tutorial on Generics (Generic Methods and Generic Classes) Q1 (Generic Classes): Write a generic class 'SpecialHashMap'

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Tutorial on Generics (Generic Methods and Generic Classes) Q1 (Generic Classes): Write a generic class 'SpecialHashMap'

Post by answerhappygod »

Tutorial On Generics Generic Methods And Generic Classes Q1 Generic Classes Write A Generic Class Specialhashmap 1
Tutorial On Generics Generic Methods And Generic Classes Q1 Generic Classes Write A Generic Class Specialhashmap 1 (58.65 KiB) Viewed 80 times
Tutorial On Generics Generic Methods And Generic Classes Q1 Generic Classes Write A Generic Class Specialhashmap 2
Tutorial On Generics Generic Methods And Generic Classes Q1 Generic Classes Write A Generic Class Specialhashmap 2 (48.37 KiB) Viewed 80 times
Tutorial on Generics (Generic Methods and Generic Classes) Q1 (Generic Classes): Write a generic class 'SpecialHashMap' which has the following private attributes: ent of type HashMap whose keys and values are generic (can be of any type) with the keys being comparable'. maxNoElem of type int 1.1 Write a no-argument constructor which creates a HashMap, assigns the HashMap to ent and initializes maxNoElem to 10. 1.2 Write a one-argument constructor which takes an argument of type int to set maxNoElem, creates a HashMap, and assigns the HashMap to ent and sets maxNoElem to the given value. 1.3 Write the getters of ent and maxNoElem. 1.4 Write a setter for maxNoElem which sets the attribute maxNoElem to the given value if and only if the current size of the HashMap (ent) is less than or equal to the given value. Otherwise, it prints the message "Cannot set the maximum number of elements to the given value because the current size of the HashMap is larger than the given value!". 1.5 Write a method: public boolean add(K key. V value) which adds an entry, whose key and value are given to the HashMap ent if and only if the size of ent is less than maxNoElem. If the entry is successfully added, the method return true, otherwise, it return false. If the entry is added, maxNoElem is incremented by 1. 1.6 Write a method: public boolean remove(K key) which removes an entry, whose key is given, from the HashMap ent if it exist. If the entry is successfully removed, the method return true, otherwise, it returns false. If the entry is removed, maxNoElem is decremented by 1. 1.7 Write a method: public HashMap getElemKeyLE(K someKey) which returns a HashMap of the elements in ent whose key is smaller than or equal to someKey.

1.8 Write a method: public HashMap getElemKeyGE(K someKey) which returns a HashMap of the elements in ent whose key is larger than or equal to someKey. 1.9 Override toString() to return: "The number of elements is: 40, The elements are: (key=value, ....." The above string assumes that maxNoElem is 40. Q2 (Generic Methods): Write the following generic methods: 2.1 Takes an array of elements of some type and returns the smallest element in it (You may upper bound the type if you need to). 2.2 Takes a List of elements of some type and two indices. It swaps the elements in the two indices (You may upper bound the type if you need to). Q3: Write a test class that: 3.1 Adds elements of different types in objects of SpecialHashMap, either of types that are in the Java API, or of types you have defined, e.g. 'Person', 'Gas', etc. It then calls the methods "getElemKeyLE' and 'getElemKeyGE' and prints the elements of the returned HashMap objects. 3.2 Creates different arrays of different types and calls the method written in 2.1 to return the smallest elements to print them. 3.2 Creates different List objects of different types and calls the method written in 2.2 to alter the lists by swapping elements. It then prints the List objects.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply