Question

Create a generic class with a type parameter that simulates drawing an item at random out...

  1. Create a generic class with a type parameter that simulates drawing an item at random out of a box. For example, the box might contain Strings representing names written on slips of paper, or the box might contain Integers representing the possible numbers for a lottery draw. Include the following methods:

    add() – that allows the user to add an object of the specified type to the box

    isEmpty() – to determine if the box is empty

    drawItem() – to randomly select an object from the box and return it, as well as eliminating it from the box (if the box is empty return null)

    toString() – to output the current contents of the box

In the driver, create two boxes, one with the names of 5 people and one with numbers between 1 and 5 that represent seating at a table. Use the drawItem() method to determine which seat a person will occupy.

Homework Answers

Answer #1
Thanks for the question.

Here is the completed code for this problem. Comments are included so that you understand whats going on. Let me know if you have any doubts or if you need anything to change.

Thank You !!

Please do up Vote : )

========================================================================================

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

//Create a generic class with a type parameter
public class Items<T> {

    private ArrayList<T> items;


    public Items() {
        items = new ArrayList<T>();
    }

    //add() – that allows the user to add an object of the specified type to the box
    public void add(T anItem) {
        items.add(anItem);
    }

    //to randomly select an object from the box and return it,
    // as well as eliminating it from the box
    public T drawItem() {

        if (items.size() != 0) {
            Random random = new Random();
            int randomIndex = random.nextInt(items.size());
            T drawnItem = items.get(randomIndex);
            items.remove(randomIndex);
            return drawnItem;
        } else {
            // (if the box is empty return null)
            return null;
        }
    }

    public boolean isEmpty() {
        return items.size() == 0;
    }

    //toString() – to output the current contents of the box
    @Override
    public String toString() {

        String description = "Items:\n";
        for (T item : items) {
            description += item.toString() + "\n";
        }
        return description;
    }
}

========================================================================================

public class ItemDemo {

    public static void main(String[] args) {

        Items<String> boxOne = new Items<String>();
        boxOne.add("Jason");
        boxOne.add("Jacob");
        boxOne.add("Jewel");
        boxOne.add("Jane");
        boxOne.add("Jackie");

        Items<Integer> boxTwo = new Items<Integer>();
        for (int i = 1; i <= 5; i++) boxTwo.add(i);

        while (!boxOne.isEmpty()) {

            //Use the drawItem() method to determine which seat a person will occupy.
            String person = boxOne.drawItem();
            int seat = boxTwo.drawItem();

            System.out.println(person + " will take seat " + seat);

        }
    }
}

========================================================================================

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
TrackMinMax For this lab, you will create a generic version of the IntTrackMinMax class you wrote...
TrackMinMax For this lab, you will create a generic version of the IntTrackMinMax class you wrote in a previous lab, called TrackMinMax. The API is: Function Signature Description constructor TrackMinMax() constructor check void check(T i) compares i to the current minimum and maximum values and updates them accordingly getMin T getMin() returns the minimum value provided to check() so far getMax T getMax() returns the maximum value provided to check() so far toString String toString() returns the string "[min,max]" As...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to the method header that will be a boolean variable: public boolean add(T newEntry, boolean sorted) The modification to the add method will makeit possible toadd new entriesto the beginning of the list, as it does now, but also to add new entries in sorted order. The sorted parameter if set to false will result in the existing functionality being executed (it will add the...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields,...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields, as well as the methods for the Inventory class (object). Be sure your Inventory class defines the private data fields, at least one constructor, accessor and mutator methods, method overloading (to handle the data coming into the Inventory class as either a String and/or int/float), as well as all of the methods (methods to calculate) to manipulate the Inventory class (object). The data fields...
this is the book name. Data Structures and Abstractions with Java 1) Description: The sample programs...
this is the book name. Data Structures and Abstractions with Java 1) Description: The sample programs in Chapter 1 of your textbook are not complete. They are used for illustration purpose only. The implementation of Listing 1-1 on page 39 is explained in Chapter 2. And, in order to see the result of using it, we will need the following set of files: i. BagInteface.java – the specification only. ii. ArrayBag.java – the implementation of BagInerface.java. iii. ArrayBagDemo.java – a...
In java just need work in the billards class it is in bold everything else is...
In java just need work in the billards class it is in bold everything else is done just the sections marked TODO Suppose that you have several numbered billiard balls on a pool table. The smallest possible number on the ball is “1”. At each step, you remove a billiard ball from the table. If the ball removed is numbered n, you replace it with n balls randomly numbered less than n. For example, if you remove the “5” ball,...
Objective: Write a Java program that will use a JComboBox from which the user will select...
Objective: Write a Java program that will use a JComboBox from which the user will select to convert a temperature from either Celsius to Fahrenheit, or Fahrenheit to Celsius. The user will enter a temperature in a text field from which the conversion calculation will be made. The converted temperature will be displayed in an uneditable text field with an appropriate label. Specifications Structure your file name and class name on the following pattern: The first three letters of your...
Program Behavior Each time your program is run, it will prompt the user to enter the...
Program Behavior Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results. Here is a sample run of the program. User input is shown in red. Let's analyze some text! Enter file name: sample.txt Number of lines: 21 Number of words: 184 Number of long words: 49 Number of sentences: 14 Number of...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT