Question

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.

  1. Use the GiftExchange class you created in the previous question.
  2. Complete the code below for the SecretSanta class .
  3. Assume the Gift class is available and the constructor and toString method are working properly
  4. Create a GiftExchange object.
  5. Add each of the Gift object to the GiftExchange object.
  6. Use the GiftExchange object to print all the Gifts in that object.
  7. Ask the GiftExchange object to provide you with a random gift and print that gift's information.

The starter code for your answer, copy this into your answer and complete.

public static void main(String args[])
{
Gift truck = new Toy("Dump Truck",20.99);
Gift dress = new Clothing("Sun Dress", 42.86);
Gift oven = new Applance("KitchenAid Oven", 1298.89);

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 Gift{
    String name;
    double price;

    public Gift(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String toString(){
        return "["+name+" : "+price+"]";
    }
}

class Toy extends Gift{

    public Toy(String name, double price) {
        super(name, price);
    }

    
}

class Clothing extends Gift{

    public Clothing(String name, double price) {
        super(name, price);
    }
}

class Appliance extends Gift{

    public Appliance(String name, double price) {
        super(name, price);
    }
}
class Main{
    public static void main(String[] args) {
        GiftExchange<Gift> cards = new GiftExchange<Gift>();

        Gift truck = new Toy("Dump Truck",20.99);
        Gift dress = new Clothing("Sun Dress", 42.86);
        Gift oven = new Appliance("KitchenAid Oven", 1298.89);

        cards.add(truck);
        cards.add(dress);
        cards.add(oven);

        System.out.println(cards);
        System.out.println("Gift drawn : "+cards.drawGift());
    }
}

OUTPUT :

Add a comment
Know the answer?
Add Answer to:
Java Programing: Please help with the following assignment. Thank you. Use the GiftExchange class you created...
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 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 An ArrayList instance variable that holds all the gifts,The ArrayList is referred to as theBox. A default constructors that creates the box....

  • JAVA programing Question 1 (Date Class):                                   &nbsp

    JAVA programing Question 1 (Date Class):                                                                                                     5 Points Create a class Date with the day, month and year fields (attributes). Provide constructors that: A constructor that takes three input arguments to initialize the day, month and year fields. Note 1: Make sure that the initialization values for the day, month and year fields are valid where the day must be between 1 and 31 inclusive, the month between 1 and 12 inclusive and the year a positive number. Note 2:...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

  • JAVA This PoD, builds off the Book class that you created on Monday (you may copy...

    JAVA This PoD, builds off the Book class that you created on Monday (you may copy your previously used code). For today’s problem, you will add a new method to the class called lastName() that will print the last name of the author (you can assume there are only two names in the author name – first and last). You can use the String spilt (“\s”) method that will split the String by the space and will return an array...

  • Homework assignment need help 3. Create the constructor, area and toString method for the following class...

    Homework assignment need help 3. Create the constructor, area and toString method for the following class (2 point) public class. Square private double a; Ll...area of square is a*a In the test class replace commented lines with your code. public class. Test Square public static void main (String[] args) // Create an instance of Square I. Print the area using area method // Print the instance using toString method

  • Please help me with this code. Thank you Implement the following Java class: Vehicle Class should...

    Please help me with this code. Thank you Implement the following Java class: Vehicle Class should contain next instance variables: Integer numberOfWheels; Double engineCapacity; Boolean isElectric, String manufacturer; Array of integers productionYears; Supply your class with: Default constructor (which sets all variables to their respective default values) Constructor which accepts all the variables All the appropriate getters and setters (you may skip comments for this methods. Also, make sure that for engineCapacity setter method you check first if the vehicle...

  • JAVA /** * This class stores information about an instructor. */ public class Instructor { private...

    JAVA /** * This class stores information about an instructor. */ public class Instructor { private String lastName, // Last name firstName, // First name officeNumber; // Office number /** * This constructor accepts arguments for the * last name, first name, and office number. */ public Instructor(String lname, String fname, String office) { lastName = lname; firstName = fname; officeNumber = office; } /** * The set method sets each field. */ public void set(String lname, String fname, String...

  • using java File Details – Build a class called FileDetails.java. When you instantiate this class and...

    using java File Details – Build a class called FileDetails.java. When you instantiate this class and give it a filename, it will report back the size of the file, whether the file is Readable and whether the file is Writeable; plus any other file information that you might deem important. This cd goes in main FileDetails fd=newFileDetails(“anyfile.doc”); All other code goes in the constructor. Write a String to a File using PrintStream – This time build a class WriteString. This...

  • In 6A, you created an object class encapsulating a Trivia Game which INHERITS from Game. Now...

    In 6A, you created an object class encapsulating a Trivia Game which INHERITS from Game. Now that you have successfully created Trivia objects, you will continue 6B by creating a linked list of trivia objects. Add the linked list code to the Trivia class. Your linked list code should include the following: a TriviaNode class with the attributes: 1. trivia game - Trivia object 2. next- TriviaNode 3. write the constructor, accessor, mutator and toString methods. A TriviaLinkedList Class which...

  • C++ Programing In this exercise, you will design a class memberType. The class has the following...

    C++ Programing In this exercise, you will design a class memberType. The class has the following data members: memberName. A string that holds the name of a person memberID. A string that holds the member identification number numBooks. An int that holds the number of books bought purchaseAmt. A double that holds the amount spent on books In addition, the class should have the following constructor and other member functions. Constructor. The constructor should accept the person’s name and member...

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