Question

Java programming: please help with the following assignment: Thank you. Create a class called GiftExchange that...

Java programming: please help with the following assignment: Thank you.

Create a class called GiftExchange that simulates drawing a gift at random out of a box.

The class is a generic class with a parameter of type T that represents a gift and where T can be a type of any class. The class must include the following

  1. An ArrayList instance variable that holds all the gifts,The ArrayList is referred to as theBox.
  2. A default constructors that creates the box.
  3. An add method that adds a gift to the box.
  4. A drawGift method that
    1. Ensure the box is not empty, if it is empty returns null.
    2. If not empty
      1. Selects a gift at random from the Box.
      2. Removes that gift from the Box
      3. Returns the selected gift.
  5. A toString method that returns a nicely formatted String representing all the object in the ArrayList. Assume any object stored in the ArrayList have a toString method.
  6. You may add any other method(s) you think you need however no additional functionality is required.

No Javadoc or import statements required

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.ArrayList;
import java.util.Random;

class GiftExchange<T>{
    ArrayList<T> gifts;
    GiftExchange(){
        gifts = new ArrayList<>();
    }
    public void add(T gift){
        gifts.add(gift);
    }
    public T drawGift(){
        if(gifts.isEmpty()){
            return null;
        }
        Random r = new Random();
        int ind = r.nextInt(gifts.size());
        T ans = gifts.remove(ind);
        return ans;
    }

    @Override
    public String toString(){
        String ans = "Gifts : ";
        for(T t : gifts){
            ans = ans+t+" ";
        }
        return ans;
    }
}

class Main{
    public static void main(String[] args) {
        GiftExchange<Integer> cards = new GiftExchange<>();
        cards.add(50);
        cards.add(49);
        cards.add(60);
        System.out.println(cards);
        System.out.println("Gift drawn : "+cards.drawGift());
    }
}

OUTPUT :

Run Main C:\Program Files \Java\jdk1.8.0_1311binljava . Gifts 50 49 60 Gift drawn : 60 Process finished with exit code 0

Add a comment
Know the answer?
Add Answer to:
Java programming: please help with the following assignment: Thank you. Create a class called GiftExchange that...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Programing: Please help with the following assignment. Thank you. Use the GiftExchange class you created...

    Java Programing: Please help with the following assignment. Thank you. Use the GiftExchange class you created in the previous question. Complete the code below for the SecretSanta class . Assume the Gift class is available and the constructor and toString method are working properly Create a GiftExchange object. Add each of the Gift object to the GiftExchange object. Use the GiftExchange object to print all the Gifts in that object. Ask the GiftExchange object to provide you with a random...

  • Java programming The purpose of this problem is to practice using a generic Urn class. NOTE:...

    Java programming The purpose of this problem is to practice using a generic Urn class. NOTE: Refer to the code for the ArrayStack class from Chapter 12. Use that code as a starting point to create the Urn class, modifying it to remove the methods in the ArrayStack class (push, pop, etc) then add the methods described below. Also change the variable names to reflect the new class. For example the array name should NOT be stack, instead it should...

  • Generics Objectives: OOWorking with a Generic Class 1. Create a class called Node that is Generic...

    java Generics Objectives: OOWorking with a Generic Class 1. Create a class called Node that is Generic. The class has one class attribute that is an element. It will need 2 Constructors, a setter and getter for the class attribute, and a toString method. 2. Write a Lab10Driver that will: Instantiate a Node Integer object with no value. I. Il. Ill. IV. a. Then call the set method to set the value to 5. Instantiate a Node String object with...

  • The purpose of this problem is to practice using a generic Jar class. write in drjava...

    The purpose of this problem is to practice using a generic Jar class. write in drjava Create a generic class, called Jar, with a type parameter that simulates drawing an item at random out of a Jar. For example the Jar might contain Strings representing names written on a slip of paper, or the Jar 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:...

  • In Java Create a testing class that does the following to the given codes below: To...

    In Java Create a testing class that does the following to the given codes below: To demonstrate polymorphism do the following: Create an arraylist to hold 4 base class objects Populate the arraylist with one object of each data type Code a loop that will process each element of the arraylist Call the first ‘common functionality’ method Call the second ‘common functionality’ method Call the third ‘common functionality’ method Verify that each of these method calls produces unique results Call...

  • cs55(java) please. Write a class called Book that contains instance data for the title, author, publisher,...

    cs55(java) please. Write a class called Book that contains instance data for the title, author, publisher, price, and copyright date. Define the Book constructor to accept and initialize this data. Include setter and getter methods for all instance data. Include a toString method that returns a nicely formatted, multi-line description of the book. Write another class called Bookshelf, which has name and array of Book objects. Bookself capacity is maximum of five books. Includes method for Bookself that adds, removes,...

  • Amusement Park Programming Project Project Outcomes Use the Java selection constructs (if and if else). Use...

    Amusement Park Programming Project Project Outcomes Use the Java selection constructs (if and if else). Use the Java iteration constructs (while, do, for). Use Boolean variables and expressions to control iterations. Use arrays or ArrayList for storing objects. Proper design techniques. Project Requirements Your job is to implement a simple amusement park information system that keeps track of admission tickets and merchandise in the gift shop. The information system consists of three classes including a class to model tickets, a...

  • Assignment: A set is a collection of items without repetitions. The goal of this assignment is...

    Assignment: A set is a collection of items without repetitions. The goal of this assignment is to implement a set as a data structure. Do the following. 1. (30 points) Starting with the template attached, implement a generic class Set(T) that maintains a set of items of generic type T using the class ArrayList(T) in the Java API. Your Set class must provide the following instance methods: add: that adds a new item. (Ignore if already in the set.) remove:...

  • Need help to create general class Classes Data Element - Ticket Create an abstract class called...

    Need help to create general class Classes Data Element - Ticket Create an abstract class called Ticket with: two abstract methods called calculateTicketPrice which returns a double and getld0 which returns an int instance variables (one of them is an object of the enumerated type Format) which are common to all the subclasses of Ticket toString method, getters and setters and at least 2 constructors, one of the constructors must be the default (no-arg) constructor. . Data Element - subclasses...

  • This project will use The Vigenere Cipher to encrypt passwords. Simple letter substitution ciphers are ones...

    This project will use The Vigenere Cipher to encrypt passwords. Simple letter substitution ciphers are ones in which one letter is substituted for another. Although their output looks impossible to read, they are easy to break because the relative frequencies of English letters are known. The Vigenere cipher improves upon this. They require a key word and the plain text to be encrypted. Create a Java application that uses: decision constructs looping constructs basic operations on an ArrayList of objects...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT