Question

JAVA. Create an application that uses a "PriorityQueue" to perform the following Uses the constructor that rece...

JAVA. 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 331. 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)

// Fig 8.1: Time1.java
public class Time1 {
   private int hour;
   private int minute;
   private int second;
  
   public void setTime(int hour, int minute, int second) {
       if (hour < 0 || hour >= 24 || minute < 0 || minute >= 60 ||
           second < 0 || second >= 60 {
           throw new IllegalArgumentException(
               "hour, minute and/or second was out of range");
       }
      
       this.hour = hour;
       this.minute = minute;
       this.second = second;
   }
  
   public String to UniversalString() {
       return String.format("%02d:%2d:%02d", hour, minute, second);
   }
  
   public String toString() {
       return String.format("%d:%02d:%02d %s",
       ((hour == 0 || hour == 12) ? 12 : hour % 12),
       minute, second, (hour < 12 ? "AM" : "PM"));
   }
}

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

ЗУ <terminated> TimeObjDemo [Ja 40 10:10:10 AM 41 class Time1Queue 42 10:30:10 AM 10:30:30 AM PriorityQueue<Time1> dataQueue;

you can see the times are sorted by their values from low to high.. since low has more priority.
======

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

class Time1 {
        private int hour;
        private int minute;
        private int second;

        public Time1(int hour, int minute, int second) {
                if (hour < 0 || hour >= 24 || minute < 0 || minute >= 60 || second < 0 || second >= 60) {
                        throw new IllegalArgumentException("hour, minute and/or second was out of range");
                }

                this.hour = hour;
                this.minute = minute;
                this.second = second;
        }

        public String toUniversalString() {
                return String.format("%02d:%2d:%02d", hour, minute, second);
        }

        public String toString() {
                return String.format("%d:%02d:%02d %s", ((hour == 0 || hour == 12) ? 12 : hour % 12), minute, second,
                                (hour < 12 ? "AM" : "PM"));
        }

        public int getHour() {
                return hour;
        }

        public int getMinute() {
                return minute;
        }

        public int getSecond() {
                return second;
        }
}

class Time1Queue {

        PriorityQueue<Time1> dataQueue;

        public Time1Queue(Comparator<Time1> comparator) {
                dataQueue = new PriorityQueue<>(comparator);
        }

        void addTimeObj(Time1 obj) {
                dataQueue.add(obj);
        }

        Time1 removeTimeObj() {
                return dataQueue.remove();
        }
}

public class TimeObjDemo {

        public static void main(String[] args) {
                Comparator<Time1> cmp = new Comparator<Time1>() {

                        @Override
                        public int compare(Time1 o1, Time1 o2) {
                                long sec1 = o1.getHour() * 3600 + o1.getMinute() * 60 + o1.getSecond();
                                long sec2 = o2.getHour() * 3600 + o2.getMinute() * 60 + o2.getSecond();
                                return (int) (sec1 - sec2);
                        }
                };
                
                Time1Queue queue = new Time1Queue(cmp);
                queue.addTimeObj(new Time1(10, 10, 10));
                queue.addTimeObj(new Time1(20, 10, 10));
                queue.addTimeObj(new Time1(10, 30, 30));
                queue.addTimeObj(new Time1(13, 10, 10));
                queue.addTimeObj(new Time1(10, 30, 10));
                
                for(int i=0; i<5; i++) {
                        System.out.println(queue.removeTimeObj());
                }

        }

}

please upvote. Thanks!

Add a comment
Know the answer?
Add Answer to:
JAVA. Create an application that uses a "PriorityQueue" to perform the following Uses the constructor that rece...
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
  • Lesson is about Input validation, throwing exceptions, overriding toString Here is what I am supposed to...

    Lesson is about Input validation, throwing exceptions, overriding toString Here is what I am supposed to do in the JAVA Language: Model your code from the Time Class Case Study in Chapter 8 of Java How to Program, Late Objects (11th Edition) by Dietel (except the displayTime method and the toUniversalString method). Use a default constructor. The set method will validate that the hourly employee’s rate of pay is not less than $15.00/hour and not greater than $30.00 and validate...

  • Instructions: Refer to the Timel class provided in Figure 8.1 in your Deitel book to complete...

    Instructions: Refer to the Timel class provided in Figure 8.1 in your Deitel book to complete the exercise. Make sure that you follow the Program Style and Readability guidelines found in the Start Here Folder. 1. Write a Point class. The Point class should be written as an abstract data type. 2. Include the following instance variables: a. an integer representing the x coordinate b. an integer representing the y coordinate c. The instance variables in your program should only...

  • Java Programming The program template represents a complete working Java program with one or more key...

    Java Programming The program template represents a complete working Java program with one or more key lines of code replaced with comments. Read the problem description and examine the output, then study the template code. Using the problem-solving tips as a guide, replace the /* */ comments with Java code. Compile and execute the program. Compare your output with the sample output provided. Modify class Time2 to include a tick method that increments the time stored in a Time2 object...

  • Please help with Java programming! This code is to implement a Clock class. Use my beginning...

    Please help with Java programming! This code is to implement a Clock class. Use my beginning code below to test your implementation. public class Clock { // Declare your fields here /** * The constructor builds a Clock object and sets time to 00:00 * and the alarm to 00:00, also. * */ public Clock() { setHr(0); setMin(0); setAlarmHr(0); setAlarmMin(0); } /** * setHr() will validate and set the value of the hr field * for the clock. * *...

  • Need help with this java code supposed to be a military time clock, but I need...

    Need help with this java code supposed to be a military time clock, but I need help creating the driver to test and run the clock. I also need help making the clock dynamic. public class Clock { private int hr; //store hours private int min; //store minutes private int sec; //store seconds public Clock () { setTime (0, 0, 0); } public Clock (int hours, intminutes, int seconds) { setTime (hours, minutes, seconds); } public void setTime (int hours,int...

  • I need some help with some homework questions. How would I implement the to-do's? ----------------------------------------------------------------------------------------------------------------------------------------------------------- AbstractArrayHea

    I need some help with some homework questions. How would I implement the to-do's? ----------------------------------------------------------------------------------------------------------------------------------------------------------- AbstractArrayHeap.Java package structures; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.NoSuchElementException; public abstract class AbstractArrayHeap<P, V> {   protected final ArrayList<Entry<P, V>> heap;   protected final Comparator<P> comparator; protected AbstractArrayHeap(Comparator<P> comparator) {     if (comparator == null) {       throw new NullPointerException();     }     this.comparator = comparator;     heap = new ArrayList<Entry<P, V>>();   } public final AbstractArrayHeap<P, V> add(P priority, V value) {     if (priority == null || value...

  • Write in C++ please In Chapter 10, the class clockType was designed to implement the time...

    Write in C++ please In Chapter 10, the class clockType was designed to implement the time of day in a program. Certain applications, in addition to hours, minutes, and seconds, might require you to store the time zone. Derive the class extClockType from the class clockTypeby adding a member variable to store the time zone. Add the necessary member functions and constructors to make the class functional. Also, write the definitions of the member functions and the constructors. Finally, write...

  • [Java] We have learned the class Clock, which was designed to implement the time of day...

    [Java] We have learned the class Clock, which was designed to implement the time of day in a program. Certain application in addition to hours, minutes, and seconds might require you to store the time zone. Please do the following: Derive the class ExtClock from the class Clock by adding a data member to store the time zone. Add necessary methods and constructors to make the class functional. Also write the definitions of the methods and constructors. Write a test...

  • import java.util.Scanner; import class17.HeapPriorityQueue; import class17.PriorityQueue; /*************** * Homework D * * * Remove any initial...

    import java.util.Scanner; import class17.HeapPriorityQueue; import class17.PriorityQueue; /*************** * Homework D * * * Remove any initial package declaration that might be added to your file in * case you edit it in eclipse. * * The goal of the homework is to create two ArrayList based implementations of * a Priority Queue as explained in Section 9.2 (in 9.2.4 and 9.2.5) of the * textbook. * * These are to be made by completing the classes PQunsorted and PQsorted as...

  • JAVA Use the class, Invoice, provided to create an array of Invoice objects. Class Invoice includes...

    JAVA Use the class, Invoice, provided to create an array of Invoice objects. Class Invoice includes four instance variables; partNumber (type String), partDescription (type String), quantity of the item being purchased (type int0, and pricePerItem (type double). Perform the following queries on the array of Invoice objects and display the results: Use streams to sort the Invoice objects by partDescription, then display the results. Use streams to sort the Invoice objects by pricePerItem, then display the results. Use streams to...

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