SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Generics And The Arraylist Class
Exercise:
Programming Projects
Question:5 | ISBN:9780132830317 | Edition: 5

Question

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.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

PROGRAM CODE:

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class RandomBox<T> {
    private List<T> items;
    private Random random;

    public RandomBox() {
        items = new ArrayList<>();
        random = new Random();
    }

    public void add(T item) {
        items.add(item);
    }

    public boolean isEmpty() {
        return items.isEmpty();
    }

    public T drawItem() {
        if (isEmpty()) {
            return null;
        }
        int index = random.nextInt(items.size());
        return items.remove(index);
    }

    public static void main(String[] args) {
        RandomBox<String> nameBox = new RandomBox<>();
        nameBox.add("John");
        nameBox.add("Emma");
        nameBox.add("Michael");
        nameBox.add("Sophia");

        System.out.println("Drawing names from the box:");
        while (!nameBox.isEmpty()) {
            String name = nameBox.drawItem();
            System.out.println("Drawn name: " + name);
        }

        RandomBox<Integer> numberBox = new RandomBox<>();
        numberBox.add(10);
        numberBox.add(20);
        numberBox.add(30);
        numberBox.add(40);

        System.out.println("\nDrawing numbers from the box:");
        while (!numberBox.isEmpty()) {
            Integer number = numberBox.drawItem();
            System.out.println("Drawn number: " + number);
        }
    }
}

OUTPUT OF THE PROGRAM CODE:

Drawn name: Sophia
Drawn name: Emma

Drawing numbers from the box:
Drawn number: 40
Drawn number: 30
Drawn number: 20
Drawn number: 10

 

0 0

Discussions

Post the discussion to improve the above solution.