Skip links
Drag

Java代写|Data Structure Exam 3

这是一个美国的数据结构Java代写案例

Task 1 – Generics and Exceptions

You will be writing your code in ComparatorQueue.java. You will creating a new generic class called Queue that will take 2 arguments for its constructor. Queue will have 2 fields: A generic List<E> object called contents and a Comparator<E> object called comp. These two fields will be set by its constructor. All imports have already been done for you.

You may find Collections.sort() useful for this Task.

Note: If you do not name these fields exactly, you will fail the autograder.

Task 1.1

In the Queue class, you will use the design recipe to write a method called add that returns void and takes an argument of type E and adds it to the Queue. Add should preserve sorted order of elements such that the smallest element (as defined by the comparator) occurs at earlier indices than larger elements.

Task 1.2

In the Queue class, you will use the design recipe to write a method called contains that takes and argument of type E and returns a boolean indicating whether or not that element is contained in Queue. You will return true if the element is contained in Queue, false otherwise.

Task 1.3

In the Queue class, you will use the design recipe to write a method called remove that returns boolean and takes an argument of type E and removes one instance of it from Queue. If the argument does not exist in Queue, then you will throw a NoSuchElementException with no message. If the argument does exist, then you will return true if the remove was successful and false if the remove was unsuccessful. Remove should preserve sorted order of elements such that the smallest element (as defined by the comparator) occurs at earlier indices than larger elements.

Task 1.4

In the Queue class, you will use the design recipe to write a method called poll that takes no arguments and returns and removes the top element (element at index 0) of Queue. If there are no elements in Queue you will return null. Poll should preserve sorted order of elements such that the smallest element (as defined by the comparator) occurs at earlier indices than larger elements.