Java Reference
In-Depth Information
5. Create a generic class with a type parameter that simulates drawing an item at
random out of a box. This class could be used for simulating a random drawing.
For example, the box might contain Strings representing names written on a slip
of paper, or the box might contain Integers representing a random drawing for a
lottery based on numeric lottery picks. Create an add method that allows the user
of the class to add an object of the specified type along with an isEmpty method
that determines whether or not the box is empty. Finally, your class should have
a drawItem method that randomly selects an object from the box and returns it.
If the user attempts to drawn an item out of an empty box, return null . Write a
main method that tests your class.
6. Implement a priority queue capable of holding objects of an arbitrary type, T , by
defining a PriorityQueue class that implements the queue with an ArrayList .
A priority queue is a type of list where every item added to the queue also has an
associated priority. Define priority in your application so that those items with the
largest numerical value have the highest priority. Your class should support the
following methods:
Add(item, priority) —Adds a new item to the queue with the associated
priority.
Remove() —Returns the item with the highest priority and removes it from the
queue. If the user attempts to remove from an empty queue, return null .
For example, if q is a priority queue defined to take Strings
q.add("X", 10);
q.add("Y", 1);
q.add("Z", 3);
System.out.println(q.remove()); // Returns X
System.out.println(q.remove()); // Returns Z
System.out.println(q.remove()); // Returns Y
Test your queue on data with priorities in various orders (e.g., ascending, descend-
ing, mixed). You can implement the priority queue by performing a linear search
through the ArrayList . In future courses, you may study a data structure called a
heap that is a more efficient way to implement a priority queue.
7. Programming Project 6.13 implemented a simple trivia game using an array of
Trivia objects. Redo this project but use an ArrayList of Trivia objects instead
of an array. The run-time behavior should remain identical to before.
8. In Programming Project 11.9, you were asked to implement a recursive algorithm
to find all files that matched a target file name. Redo this Programming Project
where the recursive method returns an ArrayList of String objects. Each string
should store the pathname to the matching file. Return null if no matching files
are found.
VideoNote
Solution to
Programming
Project 14.7
Search WWH ::




Custom Search