Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Queue.java: import stdlib.*; import java.util.Iterator; import java.util.NoSuchElementException; public class Queue implements Iterable { private int N; // number of elements on queue private Node first;

image text in transcribed

Queue.java:

import stdlib.*; import java.util.Iterator; import java.util.NoSuchElementException;

public class Queue implements Iterable { private int N; // number of elements on queue private Node first; // beginning of queue private Node last; // end of queue

// helper linked list class private static class Node { public Node() { } public T item; public Node next; }

/** * Create an empty queue. */ public Queue() { first = null; last = null; N = 0; }

/** * Is the queue empty? */ public boolean isEmpty() { return first == null; }

/** * Return the number of items in the queue. */ public int size() { return N; }

/** * Return the item least recently added to the queue. * @throws java.util.NoSuchElementException if queue is empty. */ public T peek() { if (isEmpty()) throw new NoSuchElementException("Queue underflow"); return first.item; }

/** * Add the item to the queue. */ public void enqueue(T item) { Node oldlast = last; last = new Node(); last.item = item; last.next = null; if (isEmpty()) first = last; else oldlast.next = last; N++; }

/** * Remove and return the item on the queue least recently added. * @throws java.util.NoSuchElementException if queue is empty. */ public T dequeue() { if (isEmpty()) throw new NoSuchElementException("Queue underflow"); T item = first.item; first = first.next; N--; if (isEmpty()) last = null; return item; }

/** * Return string representation. */ public String toString() { StringBuilder s = new StringBuilder(); for (T item : this) s.append(item + " "); return s.toString(); }

// check internal invariants private static boolean check(Queue that) { int N = that.N; Queue.Node first = that.first; Queue.Node last = that.last; if (N == 0) { if (first != null) return false; if (last != null) return false; } else if (N == 1) { if (first == null || last == null) return false; if (first != last) return false; if (first.next != null) return false; } else { if (first == last) return false; if (first.next == null) return false; if (last.next != null) return false;

// check internal consistency of instance variable N int numberOfNodes = 0; for (Queue.Node x = first; x != null; x = x.next) { numberOfNodes++; } if (numberOfNodes != N) return false;

// check internal consistency of instance variable last Queue.Node lastNode = first; while (lastNode.next != null) { lastNode = lastNode.next; } if (last != lastNode) return false; }

return true; }

/** * Return an iterator that iterates over the items on the queue in FIFO order. */ public Iterator iterator() { return new ListIterator(); }

// an iterator, doesn't implement remove() since it's optional private class ListIterator implements Iterator { private Node current = first;

public boolean hasNext() { return current != null; } public void remove() { throw new UnsupportedOperationException(); }

public T next() { if (!hasNext()) throw new NoSuchElementException(); T item = current.item; current = current.next; return item; } }

public void toGraphviz(String filename) { GraphvizBuilder gb = new GraphvizBuilder (); toGraphviz (gb, null, first); gb.toFile (filename, "rankdir=\"LR\""); } private void toGraphviz (GraphvizBuilder gb, Node prev, Node n) { if (n == null) { gb.addNullEdge (prev); return; } gb.addLabeledNode (n, n.item.toString ()); if (prev != null) gb.addEdge (prev, n); toGraphviz (gb, n, n.next); }

/** * A test client. */ public static void main(String[] args) { StdIn.fromString ("to be or not to - be - - that - - - is"); Queue q = new Queue(); int count = 0; q.toGraphviz ("queue" + count + ".png"); count++; while (!StdIn.isEmpty()) { String item = StdIn.readString(); if (!item.equals("-")) q.enqueue(item); else if (!q.isEmpty()) StdOut.print(q.dequeue() + " "); q.toGraphviz ("queue" + count + ".png"); count++; } StdOut.println("(" + q.size() + " left on queue)"); } }

TestFinalAnswer.java:

package finalanswer;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Random;

import stdlib.StdIn;

import stdlib.StdOut;

public class TestFinalAnswer {

public static > boolean isSorted(Item[] a) {

for (int i = 0; i

if (a[i].compareTo(a[i+1]) > 0) return false;

}

return true;

}

public static void main(String[] args) {

StdOut.println("*** " + FinalAnswer.yourName() + " ***");

StdOut.println();

Integer[] array = new Integer[12];

Random r = new Random();

for (int i = 0; i

array[i] = r.nextInt(1000);

}

StdOut.println("Array before sorting: " + Arrays.toString(array));

FinalAnswer.minpqSort(array);

StdOut.println("Array after sorting: " + Arrays.toString(array));

StdOut.println("Array is sorted: " + isSorted(array));

StdOut.println();

Queue queue = new Queue();

queue.enqueue("first");

queue.enqueue("second");

queue.enqueue("third");

queue.enqueue("fourth");

StdOut.println("Queue before reversing: " + queue);

FinalAnswer.reverseQueue(queue);

StdOut.println("Queue after reversing: " + queue);

StdOut.println();

double[] frequencies = {110.0, 110.0*1.224, 110.0*1.224*1.224};

ArrayList risingChords = FinalAnswer.createRisingChordList(0.2, frequencies, 10);

for (Chord risingChord: risingChords) {

StdOut.println("Playing: " + risingChord);

risingChord.play();

}

StdOut.println();

ArrayList ctaTrains = new ArrayList();

StdIn.fromFile("data/CTAdata.txt");

while (!StdIn.isEmpty()) {

int runNumber = StdIn.readInt();

String lineColor = StdIn.readString();

String nextStation = StdIn.readString();

String arrivalTime = StdIn.readString();

ctaTrains.add(new CTATrain(runNumber, lineColor, nextStation, arrivalTime));

}

StdOut.println("--- Trains before sorting ---");

for (CTATrain ctaTrain: ctaTrains) {

StdOut.println(ctaTrain);

}

StdOut.println();

ctaTrains.sort(null);

StdOut.println("--- Trains after sorting by run number ---");

for (CTATrain ctaTrain: ctaTrains) {

StdOut.println(ctaTrain);

}

StdOut.println();

ctaTrains.sort((CTATrain t1, CTATrain t2) -> (t1.getArrivalTime().compareTo(t2.getArrivalTime())));

StdOut.println("--- Trains after sorting by arrival time ---");

for (CTATrain ctaTrain: ctaTrains) {

StdOut.println(ctaTrain);

}

StdOut.println();

StdOut.println("=== " + FinalAnswer.yourName() + " ===");

}

}

Need help with a Java problem: Instructions 1. In Eclipse, create a package called finalanswer. 2. Copy and place into the package the file TestFinalAnswer.java provided below. 3. In the package, create a class called FinalAnswer. All of the static methods you are asked to write will go in this class. FIFO Queue Define in the FinalAnswer class a generic method called reverse eue with the signature public static Queue reversegueue (Queue Item> queue) lt modifies its parameter so that, when it returns, the values in the queue are reversed. For example, if the queue q1 contains the integer values 13, 15, 9, 10, and 25 (in that order), the following call: q2 eue (q1) reverse will result in q2 having the values 25, 10, 9, 15, 13 (in that order). Use the queue class provided below Testing your code The Test FinalAnswer program provided below will test every one of your methods and classes. It produces (a lot) of output and it will be obvious whether your code works

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 1 Lnai 9284

Authors: Annalisa Appice ,Pedro Pereira Rodrigues ,Vitor Santos Costa ,Carlos Soares ,Joao Gama ,Alipio Jorge

1st Edition

3319235273, 978-3319235271

More Books

Students also viewed these Databases questions

Question

DEFINE selection and DISCUSS its strategic importance.

Answered: 1 week ago