Question
Create a generic class, called Urn, with a type parameter that simulates drawing an item at random out of a Urn. For example the Urn
Create a generic class, called Urn, with a type parameter that simulates drawing an item at random out of a Urn. For example the Urn might contain Strings representing names written on a slip of paper, or the Urn might contain integers representing a random drawing for a lottery. Include the following methods in your generic class, along with any other methods you’d like: •an add( ) method that allows the user to add one object of the specified type. Attempting to add too many items to the Urn will throw a FullUrnException •an isEmpty( ) method (returns true if the Urn is empty, otherwise returns false) •a drawItem( ) method that randomly selects an object from the Urn and returns it. Return null if the Urn is empty. Delete the selected item. (see Note below). Attempting to draw from an empty Urn will throw an EmptyUrnException •a toString( ) method (returns a String containing the Urn’s contents). If the urn is empty the toString should return “the urn is empty” Your Urn class with need an array of size 10 to hold the objects in the Urn, and a count variable to maintain a count of how many objects are actually in the Urn. In the driver file that tests your class create 3 Urns, one to test the various methods in the Urn class, one with the names of 8 of your friends, the third with numbers between 1 and 5 inclusive representing the number of hours you will spend playing computer games tonight with 2 of your friends. For the test Urn, •create a for loop to attempt to add 11 integers to the urn. Your code should catch the exception and write out an appropriate message •call the toString to display the contents of the urn •create a for loop to attempt to draw 11 items out of the urn. Your code should demonstrate: oeach item drawn is random oeach item drawn is different oattempting to draw the 11th item should throw an exception. Your code should catch the exception and write out an appropriate message •call the toString to display the contents of the now empty urn For the remaining 2 urns, use the add( ) method to populate the 2 Urns, and the drawItem( ) method for each Urn to determine i) which 2 friends you will invite to play computer games with and ii) how many hours of gaming you and your friends will do. Note: public int nextInt(int bound) in java.util.Random returns a random int value between 0 (included) and the specified value, bound, (excluded).
Step by Step Solution
3.46 Rating (153 Votes )
There are 3 Steps involved in it
Step: 1
FullUrnExceptionjava public class FullUrnException extends Exception public FullUrnException superUr...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started