Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement an update() method which accepts an item and updates its place in the queue (i.e. update its priority). Lower priority items should be at

Implement an update() method which accepts an item and "updates" its place in the queue (i.e. update its priority). Lower priority items should be at the front of the queue (since well want to pick the shortest lines, not the longest lines in our grocery store).

https://www.chegg.com/homework-help/questions-and-answers/import-javautilqueue-import-javautilnosuchelementexception-class-simple-queue-based-simple-q34996504?trackid=AkMAJs02

import java.util.NoSuchElementException; import java.util.Date; //for testing

//priority queue where the minimum item has the highest priority class PriorityQueue> extends SimpleQueue { //updates an item that's already in the queue //NOTE: This should update the exact item in memory, //not just any "equal" item (in other words, you //should use == here and not .equals())

public void update(T item) { //O(n) //throws NoSuchElementException if item is not //in the queue } //You may need to override some other methods from SimpleQueue! //Restriction 1: all methods from SimpleQueue should still work //(as in, if you add(), the value should be added, if you call //size() it should return the correct value, etc.). However, //remove/poll will remove the _minimum_ value from the queue; //element/peek will return the _minimum_ value from the queue. //Restriction 2: element() and peek() must still both be O(1) //------------------------------------------------------------- // Main Method For Your Testing -- Edit all you want //------------------------------------------------------------- public static void main(String[] args){ PriorityQueue values = new PriorityQueue<>(); Date[] dates = new Date[5]; for (int i=5; i>=1; i--){ dates[i-1] = new Date(86400000*i); values.add(dates[i-1]); } for(Date d : values) { System.out.println(d); }

dates[3].setTime(0); values.update(dates[3]); System.out.println(); for(Date d : values) { System.out.println(d); } if(values.peek().equals(dates[3])) { System.out.println(" Yay 1"); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MFDBS 89 2nd Symposium On Mathematical Fundamentals Of Database Systems Visegrad Hungary June 26 30 1989 Proceedings

Authors: Janos Demetrovics ,Bernhard Thalheim

1989th Edition

3540512519, 978-3540512516

More Books

Students also viewed these Databases questions

Question

why we face Listening Challenges?

Answered: 1 week ago

Question

what is Listening in Context?

Answered: 1 week ago