Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement the queue class and Iterator for the queue public class Process{ private String _name; private final int _totalInstructions; private int _finishedInstructions; private JProgressBar _bar;

Implement the queue class and Iterator for the queue

public class Process{

private String _name; private final int _totalInstructions; private int _finishedInstructions; private JProgressBar _bar; Process _next, _prev;

/** * Instantiates a new process. * * @param name the name of the process * @param totalIns the total instructions of the process */ public Process(String name, int totalIns){ _name = name; _totalInstructions = totalIns; createProgressBar();}

/** Gets the name. * @return the name */ public String getName(){return _name;} /** Gets the progress bar. * @return the progress bar */ public JProgressBar getBar(){return _bar;} /** Gets the total amount of instructions in the process. * @return the total instructions */ public int getTotal(){return _totalInstructions;} /** * Gets the amount of finished instructions. * @return the finished instructions */ public int getFinished(){return _finishedInstructions;} /** * Checks if the process is dcompleted. * @return true, if is done */ public boolean isDone(){ return _finishedInstructions == _totalInstructions;} /** Perform a single instruction of the process. */ public void performInstruction(){ if (!isDone()) _finishedInstructions++;}

private void createProgressBar(){ _bar = new JProgressBar(0, _totalInstructions); UIManager.put("ProgressBar.selectionForeground", Color.WHITE); _bar.setString(_bar.getValue()+"/"+_bar.getMaximum()); _bar.setStringPainted(true);} protected void updateProgressBar(boolean active){ _bar.setValue(_finishedInstructions); //set color if (isDone() || !active) _bar.setForeground(new Color(102,153,204)); else _bar.setForeground(new Color(102,204,204)); //set label if (isDone()) _bar.setString("Finished @ "+Driver._pulseCt); else _bar.setString(_finishedInstructions+"/"+_totalInstructions); _bar.repaint();} /** * Returns a clone of this process that is identical in every way * except that it has null links. * * @return the process * @see java.lang.Object#clone() */ @Override public Process clone(){ Process result = new Process(_name,_totalInstructions); result._bar.setValue(_finishedInstructions); return result; } /** * Checks for equality of this process with the parameter process. * It will check everything except for the links. * * @param other the object against which to test for equality * @return true, if successful * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals(Object other){ if (!(other instanceof Process) || other == null) return false; Process p = (Process) other; return p._totalInstructions == _totalInstructions && p._finishedInstructions == _finishedInstructions && p._name.equals(_name); } /** * The Class Queue. */ public static class Queue extends AbstractQueue{ private int _manyItems, _version; private Process _dummy;

/** Instantiates a new queue object. * It will only contain a dummy process with null name and 0 instructions, * linked circularly to itself. This dummy process should never be * passed outside of this class. * @postcondition queue is empty except for dummy process */ public Queue(){ }

/** Adds a new process to the end of the queue. * * @param p process to add to the end of this queue * @throws NullPointerException if the process to add is null * @throws IllegalArgumentException if the process is already in another queue * * @return true always * @see java.util.Queue#offer(java.lang.Object) */ @Override public boolean offer(Process p) { return true; } /** Add all processes from parameter queue into the back of this queue. * The parameter queue should be empty after this method, * except if the parameter is the same as this, in which case, nothing happens. * @param pq the queue from which to take all processes, must not be null */ public void takeAll(Queue pq) { }

/** Returns the next process to be polled from this queue. * * @return the next process to be polled by this queue, or null if empty * @see java.util.Queue#peek() */ @Override public Process peek(){ return null; } /** Removes and returns the process at the start of this queue, null if empty. * This method should never return the dummy process! * * @returns the process at the start of this queue, or null if empty * @see java.util.Queue#poll() */ @Override public Process poll() { Process result = null; return result; }

/** Returns the number of non-dummy processes in this queue. * * @return the number of non-dummy processes * @see java.util.AbstractCollection#size() */ @Override public int size() { return 0; } /** Returns a new copy of this queue. The copy should be unaffected * by subsequent changes made to this queue, and vice versa. The * processes added to the copy should be clones. * * @return a clone of this queue * @see java.lang.Object#clone() */ @Override public Queue clone(){ assert _wellFormed() : "invariant failed at start of clone()";

Queue copy = new Queue(); try{ copy = (Queue) super.clone();} catch(CloneNotSupportedException e){ // should not happen } // TODO assert _wellFormed() : "invariant failed at end of clone()"; assert copy._wellFormed() : "invariant of result failed at end of clone()"; return copy; }

/** Returns a new (remove-less) iterator over this queue. * @see java.util.AbstractCollection#iterator() */ @Override public Iterator iterator(){ assert _wellFormed() : "invariant failed at start of iterator()"; return new MyIterator();} private class MyIterator implements Iterator{ private Process _cursor; private int _myVersion; /** Instantiates a new iterator */ public MyIterator(){ } /** Returns whether there are more processes to be returned. * @throws ConcurrentModificationException if versions don't match * @return true if there exists a next element */ public boolean hasNext() { return false; }

/** Returns the next process in this queue. This method should * *not* call poll, or change the state of the queue in any way. * * @throws ConcurrentModificationException if versions don't match * @return the next process in the queue */ public Process next() { return null; } }

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_2

Step: 3

blur-text-image_step3

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

More Books

Students also viewed these Databases questions

Question

Explain how to build high-performance service delivery teams.

Answered: 1 week ago