Question

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:

  • an add( ) method that allows the user to add one object of the specified type
  • an isEmpty( ) method (returns true if the Jar is empty, otherwise returns false)
  • a drawItem( ) method that randomly selects an object from the Jar and returns it. Return null if the Jar is empty. Do not delete the selected item. (see Note below)
  • a toString( ) method (returns a String containing the Jar’s contents)

Your Jar class with need an array of size 10 to hold the objects in the Jar, and a count variable to maintain a count of how many objects are actually in the Jar.

In the driver file that tests your class create 2 Jars, one with the names of 6 of your friends, the other with numbers between 2 and 8 inclusive representing the number of hours you will spend partying tonight with 3 of your friends.

Use the add( ) method to populate the 2 Jars, and the drawItem( ) method for each Jar to determine    i) which 3 friends you will invite out to party with   and   ii) how many hours of partying you and your friends will do.

Test the isEmpty and toString methods as well for the 2 Jars.

Note:

public int nextInt(int bound) in java.util.Random returns a random int value between 0 (included) and the specified value, bound, (excluded).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Jar.java
import java.util.Random;

public class Jar<T> {
  
   private T items[];
   private int count;
  
   // constructor to create an empty jar of maximum 10 items
   public Jar()
   {
       items = (T[]) new Object[10];
       count = 0;
   }

   // method to add an item in the jar
   public void add(T value)
   {
       // check if jar has room, then add
       if(count < items.length)
       {
           items[count] = value;
           count++;
       }
   }
  
   // method that returns if the jar is empty or not
   public boolean isEmpty()
   {
       return(count == 0);
   }
  
   // method to return a randomly selected item from the jar (doesn't remove the selected item)
   public T drawItem()
   {
       if(isEmpty())
           return null;
      
       Random ran = new Random();
       int idx = ran.nextInt(count);
       return items[idx];
   }
  
   // returns string representation of the jar items
   public String toString()
   {
      
       if(isEmpty())
           return "Empty Jar";
          
       String jarStr = "";
       for(int i=0;i<count;i++)
           jarStr += items[i].toString()+"\n";
       return jarStr;
   }
}
//end of Jar.java

//JarDriver.java

public class JarDriver {
      
   public static void main(String[] args) {
      
       // create 2 jars
       Jar<String> friendsJar = new Jar<String>();
       Jar<Integer> hoursJar = new Jar<Integer>();
       // add elements to both jar
       friendsJar.add("John");
       friendsJar.add("Kelly");
       friendsJar.add("Michelle");
       friendsJar.add("Sheldon");
       friendsJar.add("Mike");
       friendsJar.add("Shaun");
      
       hoursJar.add(2);
       hoursJar.add(4);
      
       // draw 3 items from friendsJar
       System.out.println("The 3 friends invited to party : ");
       System.out.println(friendsJar.drawItem());
       System.out.println(friendsJar.drawItem());
       System.out.println(friendsJar.drawItem());
       // draw one item from hoursJar
       System.out.println("Number of hours of partying : "+hoursJar.drawItem());
      
       // display the items in friendsJar and hoursJar
       System.out.println("\nFriends : \n"+friendsJar);
       System.out.println("Hours : \n"+hoursJar);
      

   }

}
//end of JarDriver.java

Output:

The 3 friends invited to party Shaun John Mike Number of hours of partying 2 Friends John Kelly Michelle Sheldon Mike Shaun H

Add a comment
Know the answer?
Add Answer to:
The purpose of this problem is to practice using a generic Jar class. write in drjava...
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 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...

  • 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....

  • how do I change my code to generic form *********************************************************************** public class UnboundedStackQueue { //question#3 }...

    how do I change my code to generic form *********************************************************************** public class UnboundedStackQueue { //question#3 } class Stack { Node head; int size; Stack() //default constructor { this.head=null; this.size=0; } //Input = data //Output = void (just adds value to list) // method pushes elements on stack // time: O(1) // space: O(1) public void push(int data) { Node node=new Node(data); node.next=head; head=node; size++; } //Input = none //Output = top of stack // method pops value from top of...

  • 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...

  • 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,...

  • In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list...

    In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list of objects. Also, this lab specification tells you only what to do, you now have more responsibility to design how to do it. Problem description You are given a text file called 'Students.txt' that contains information on many students. Your program reads the file, creating many Student objects, all of which will be stored into an array list of Student objects, in the Students...

  • Please write in Java Recall that the ADT list class methods are;  void List() ...

    Please write in Java Recall that the ADT list class methods are;  void List()  bool isEmpty()  int size()  void add(int item, int pos)//inserts item at specified position (first postion is 1)  void remove(int index)//removes item from specified position  void removeAll()  int indexOf(int item)//returns the index of item  int itemAt(int index)//returns the item in position specified by index Implementation of LIST ADT operations and details are hidden. Only the methods listed above are...

  • Create an application that provides a solution for problem 20.8 In addition to requirements specified in...

    Create an application that provides a solution for problem 20.8 In addition to requirements specified in the description. The class must satisfy the following Default Constructor Two argument constructor for data used to initialize "first" and "second" "toString" method for displaying the "first" and "second" data elements Creates a "Comparator" object (to be used by the sort method; i.e. create an instance of a class that implements the interface [must override "compare" and "equals" methods]) The application must create an...

  • Please the answer have to be in JAVA programing language Problem 1: Write a class that...

    Please the answer have to be in JAVA programing language Problem 1: Write a class that implements a generic binary search tree including the methods add, contains and printInorder.   (We will do this as a group) Problem 2: Add a toString method to the class. This method returns a string of the form “[e1, e2, …, en]” containing all of the elements in the tree in order. Problem 3: Add a method to the class that returns the number of...

  • QUESTION 5 (15 Marks) Inheritance and Polymorphism Design a Ship class that has the following members:...

    QUESTION 5 (15 Marks) Inheritance and Polymorphism Design a Ship class that has the following members: • A private String data field named name for the name of the ship. • A private String data field named yearBuilt for the year that the ship was built. • A constructor that creates a ship with the specified name and the specified year that the ship was built. • Appropriate getter and setter methods. A toString method that overrides the toString method...

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