mardi 3 mars 2020

How do I randomly select a generic type without repetitions

I have a coding assignment which I'm about 90% of the way through. The task is to randomly assign seats to a randomly selected person. I am having troubles with my drawItem() method in the Generic class, as I am supposed to eliminate it so the same number or person name doesn't repeat when being drawn.

The assignment description says: 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)...

Any help with the eliminating the object is greatly appreciated!! I have been stuck on this part of the assignment for too long now.

Here is my GenericDriver class:


    public static void main(String[] args) {

        Generic<String> myStringBox = new Generic<String>();

        // use the .add() function to add slips of papers with names on
        // them to fill the box
        myStringBox.add("Person 1");
        myStringBox.add("Person 2");
        myStringBox.add("Person 3");
        myStringBox.add("Person 4");
        myStringBox.add("Person 5");
        System.out.println("... names are being added!");

        Generic<Integer> myIntegerStack = new Generic<Integer>();
        // use the .add() function to add the integer slips of paper to the box
        myIntegerStack.add(1);
        myIntegerStack.add(2);
        myIntegerStack.add(3);
        myIntegerStack.add(4);
        myIntegerStack.add(5);
        System.out.println("... numbers are being added!");

        System.out.println("..... Generating which seat a person will occupy .....\n");

        System.out.println("First draw: " + myStringBox.drawItem()
            + " will occupy seat number: " + myIntegerStack.drawItem());

        System.out.println("Second draw: " + myStringBox.drawItem()
            + " will occupy seat number: " + myIntegerStack.drawItem());

        System.out.println("Third draw: " + myStringBox.drawItem()
            + " will occupy seat number: " + myIntegerStack.drawItem());

        System.out.println("Fourth draw: " + myStringBox.drawItem()
            + " will occupy seat number: " + myIntegerStack.drawItem());

        System.out.println("Fifth draw: " + myStringBox.drawItem()
            + " will occupy seat number: " + myIntegerStack.drawItem());

    }

} 

Here is my Generic class:

import java.util.Arrays;
import java.io.*;
import java.util.*;
import java.util.Collections;
@SuppressWarnings("unchecked")

public class Generic<T> {

    // creating an array
    T[] item;
    // count of the object's size
    int size;

    //generic constructor that will initilize the items in the box
    Generic (){
        item = (T[]) new Object[5];
    }

    public void add(T t) {
        item[size] = t;
        size++;
    }

    Random randomDraw = new Random();

    public T drawItem() {
        return item[randomDraw.nextInt(item.length)];
    }

    public String toString(){
        return Arrays.toString(item);
    } 

Here is a sample output to visualize what I mean:

..... Generating which seat a person will occupy .....

First draw: Person 4 will occupy seat number: 1
Second draw: Person 5 will occupy seat number: 1
Third draw: Person 2 will occupy seat number: 1
Fourth draw: Person 3 will occupy seat number: 3
Fifth draw: Person 1 will occupy seat number: 5



Aucun commentaire:

Enregistrer un commentaire