i. Create a generic class with a type parameter that simulates drawing an item at random out of a box. This class could be used for simulating a random drawing. For example, the box might contain Strings representing names written on a slip of paper, or the box might contain Integers representing a random drawing for a lottery based on numeric lottery picks. Create an add method that allows the user of the class to add an object of the specified type along with an isEmpty method that determines whether or not the box is empty. Finally, your class should have a drawItem method that randomly selects an object from the box and returns it. If the user attempts to drawn an item out of an empty box, return null. Write a main method that tests your class.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 16PE: The implementation of a queue in an array, as given in this chapter, uses the variable count to...
icon
Related questions
Question

Implement the following tasks by using concepts of object-oriented programming (Java). 
i. Create a generic class with a type parameter that simulates drawing an item at random out of a box. This class could be used for simulating a random drawing. For example, the box might contain Strings representing names written on a slip of paper, or the box might contain Integers representing a random drawing for a lottery based on numeric lottery picks. Create an add method that allows the user of the class to add an object of the specified type along with an isEmpty method that determines whether or not the box is empty. Finally, your class should have a drawItem method that randomly selects an object from the box and returns it. If the user attempts to drawn an item out of an empty box, return null. Write a main method that tests your class.
ii. The following is a short snippet of code that simulates rolling a 6-sided dice 100 times. There is an equal chance of rolling any digit from 1 to 6.
public static void printDiceRolls(Random randGenerator) {
for ( int i = 0; i < 100; i++)
{
System.out.println(randGenerator.nextInt(6) + 1); }
}
public static void main(String[] args)
{
Random randGenerator = new Random(); printDiceRolls(randGenerator);
}
Create your own class, LoadedDice, that is derived from Random. The constructor for LoadedDice needs to only invoke Random ’s constructor. Override the public int nextInt(int num) method so that with a 50% chance, your new method always returns the largest number possible (i.e., num – 1), and with a 50% chance, it returns what Random’s nextInt method would return.
Test your class by replacing the main method with the following: LoadedDice myDice = new LoadedDice();

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Generic Type
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning