Priority Queue and Application
You are to write a program to that finds lowest N closing Dow Jones Industrial averages and their dates in order by closing average. The program should prompt for:
● N: the number of lowest closing averages requested by the user to be output.
● The name of a file containing the Dow Jones Industrial average (djia) data which is assumed to be in the following format. Each line contains 3 items separated by whitespace:
○ date
○ opening average
○ closing average
● For example:
7­Mar­2006 11294.94 11279.65 16­Mar­2006 11210.97 11253.24
15­Mar­2006 11149.76 11209.77
14­Mar­2006 11076.02 11151.34
You write all code involved by yourself.

1. Class MinPQ
public class MinPQ<E extends Comparable<E>>
The MinPQ<E> class must implement these methods:
● MinPQ() ­ constructor
● E deleteMin() ­ removes and returns the minimum item
● E min() ­ return the minimum item (but don't delete it)
● void insert(E item) ­ add item to this min priority queue
● boolean isEmpty() ­ is the priority queue empty?
● int size() ­ return the number of items in the priority queue
You must implement MinPQ using a Java LinkedList as the data storage. You must also keep the list sorted. You do so by placing the element at the right position at every insertion. You may NOT sort the whole list at NO time (by Collections.sort() or any other way). Doing so will result in significant reduction in points.
By keeping the list sorted, the complexity of the methods would become:
● deleteMin() ­ complexity: O(1)
● min() ­ complexity: O(1)
● insert ­ complexity: O(n) (where n is the size of the priorty queue at the time of the insertion)
● size() and isEmpty() ­ complexity: O(1)

2. Class Djia
This class should have three members to hold one date's djia data: the date, opening, and closing average. For date, use the Java GregorianCalendar class (defined in the java.util package). The class would then look like:
public class Djia {   // instance variables
private GregorianCalendar date;
private double opening;
private double closing;
This class should also have the following public members:
● public Djia(String dt, double opening, double closing) ­ constructor
● public String date()
● public double opening()
● public double closing()
● public int compareTo(Djia djiaItem)
● public String toString()
The constructor's first parameter, String dt, should expect the form "dd­mm­yyyy".
The compareTo() method should compare the closing average of 'this' and of djiaItem and return ­1 if this.closing is < djiaItem.closing; return 0 if they are equal; and return 1 if this.closing > djiaItem.closing.

3. Application Program
The application class should be named DjiaApp. The application must use your MinPQ and
Djia classes. In particular, the application should have this declaration:
MinPQ<Djia> pq = new MinPQ<Djia>();
Data Files
Two data files are provided
● tinyDjia.txt (20 djia items)
● djia.txt (19,449 djia items)
JUnit Test
A JUnit class (in is provided to unit test your MinPQ before you use it in your DjiaApp application.
Sample Run
Here is a sample output of the program for input file tinyDjia.txt:
This program finds the N lowest closing averages in a file of Dow Jones Industrial Average closing records. N: 5
File: tinyDjia.txt

The 5 lowest closing averages are:
       Date    Opening    Closing
6­Mar­2006   11022.47   10958.59
9­Mar­2006   11005.66   10972.28
7­Mar­2006   10957.31   10980.69
28­Feb­2006   11096.75   10993.41
8­Mar­2006   10977.08   11005.74

● Be sure to test your MinPQ class (with the provided JUnit test).
● Test your DjiaApp class! Use tinyDjia.txt input file. If that gives the expected result, you could try the bigger djia.txt file to see if your program is acceptable fast enough.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import java.util.ArrayList;
import java.util.NoSuchElementException;

public class MinPQ<E extends Comparable<E>> {
    private ArrayList<E> list;
    * constructor
    public MinPQ() {
       list = new ArrayList<>();
    * delete min value
    * @return
    *          min value if list is not empty
    public E deleteMin(){
       if (list.isEmpty()) {
            throw new NoSuchElementException();
       return list.remove(0);
    * get min value
    * @return null if list is empty
    *          min value if list is not empty      
    public E min(){
       if (list.isEmpty()) {...

By purchasing this solution you'll be able to access the following files:

for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats