Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Text Book: \"Java How to Program”, by Deitel & Deitel, 10th Ed. ISBN: 9780133807

ID: 3786673 • Letter: T

Question

Text Book: "Java How to Program”, by Deitel & Deitel, 10th Ed. ISBN: 9780133807806

Create an application that uses a "PriorityQueue" to perform the following

Uses the constructor that receives a "Comparator" object as an argument

Stores 5 "Time1" objects using the "Time1" class shown in Fig. 8.1 on page 317. The class must be modified to implement the "Comparator" interface

Displays the "Universal Time" in priority order

Note: To determine the ordering when implementing the "Comparator" interface, convert the time into seconds (i.e., hours * 3600 + minutes * 60 + seconds), smaller values infer higher priority)

Explanation / Answer

For this, As your Time1 class members are private, you need to provide the setters and getters method.

++++++++++++++++++++++++++++++++File Time1.java +++++++++++++++++++++++++++++++
public class Time1 {
   private int hour; // 0 - 23
   private int minute; // 0 - 59
   private int second; // 0 - 59

   public int getHour() {
       return hour;
   }

   public void setHour(int hour) {
       this.hour = hour;
   }

   public int getMinute() {
       return minute;
   }

   public void setMinute(int minute) {
       this.minute = minute;
   }

   public int getSecond() {
       return second;
   }

   public void setSecond(int second) {
       this.second = second;
   }

   // set a new time value using universal time; throw an
   // exception if the hour, minute or second is invalid
   public void setTime(int h, int m, int s) {
       // validate hour, minute and second
       if ((h >= 0 && h < 24) && (m >= 0 && m < 60) && (s >= 0 && s < 60)) {
           hour = h;
           minute = m;
           second = s;
       } else
           throw new IllegalArgumentException(
                   "hour, minute and/or second was out of range");
       // end method setTime
   }

   // convert to String in universal-time format (HH:MM:SS)
   public String toUniversalString() {
       return String.format("%02d:%02d:%02d", hour, minute, second);
   } // end method toUniversalString

   // convert to String in standard-time format (H:MM:SS AM or PM)
   public String toString() {
       return String.format("%d:%02d:%02d %s", ((hour == 0 || hour == 12) ? 12
               : hour % 12), minute, second, (hour < 12 ? "AM" : "PM"));
   } // end method toString

} // end class Time1

++++++++++++++++++++++++++++++++ TestPriorityQueue.java +++++++++++++++++++++++++++++++

import java.util.Comparator;
import java.util.PriorityQueue;

public class TestPriorityQueue {

   PriorityQueue<Time1> priorityQueue;

   public TestPriorityQueue(Comparator<Time1> defautComparator) {
       priorityQueue = new PriorityQueue<Time1>(5, defautComparator);
   }

   public static void main(String[] args) {
       Time1Comparator comparator = new Time1Comparator();

       TestPriorityQueue testPriorityQueue = new TestPriorityQueue(comparator);

       Time1 time1 = new Time1();
       time1.setTime(3, 30, 0); // 3:30:00

       Time1 time2 = new Time1();
       time2.setTime(9, 00, 0); // 9:00:00

       Time1 time3 = new Time1();
       time3.setTime(12, 30, 0); // 12:30:00

       Time1 time4 = new Time1();
       time4.setTime(16, 5, 50); // 16:05:50

       Time1 time5 = new Time1();
       time5.setTime(23, 10, 10); // 23:10:10

       // changed the order so that we can verify priority queue behaviour
       testPriorityQueue.priorityQueue.add(time2);
       testPriorityQueue.priorityQueue.add(time5);
       testPriorityQueue.priorityQueue.add(time1);
       testPriorityQueue.priorityQueue.add(time3);
       testPriorityQueue.priorityQueue.add(time4);

       for (Time1 s; (s = testPriorityQueue.priorityQueue.poll()) != null; )
           System.out.println(s);
   }
}

class Time1Comparator implements Comparator<Time1> {
   public int compare(Time1 timeArg1, Time1 timeArg2) {
       long arg1Secs = timeArg1.getHour() * 3600 + timeArg1.getMinute() * 60
               + timeArg1.getSecond();
       long arg2Secs = timeArg2.getHour() * 3600 + timeArg2.getMinute() * 60
               + timeArg2.getSecond();

       if (arg1Secs > arg2Secs)
           return 1;
       else
           return 0;
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote