Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Easy Question

  Asked By: Pedro    Date: Jun 19    Category: Java    Views: 945

I have been assigned a relatively easy assignment (that's what I've been
told anyway!) that requires me to find the smallest item (not necessairly
just a number!!) in a queue.

I have been given the interface for the queue (not supposed to use the one
given with java), and all other files needed to compile the project apart
from the file that creates the queue and initialises the method to find the
smallest number in it. I have decided to call this file

I am having a problem with the Comparable interface.

Here is the code i've wrote for SmallestInQueue.java so far:

public class SmallestInQueue

// In order to get any marks for your work, this java file must compile!!

public static Comparable findSmallestInQueue(QueueInterface theQueue, int
// pre: theQueue is not empty
// and only contains items that are Comparable
// queueSize is the size of the queue
// post: Returns a reference to the 'smallest' Object in theQueue
Comparable SmallestItem; // Comparable object used to store the smallest
if(queueSize > 1){
// haven't coded this yet, first need to be able to return one thing
Object aObject = theQueue.dequeue();
SmallestItem = (Comparable) aObject;
return SmallestNumber;

public static void main(String[] args)

int aQueueSize = 1; // the size of the queue being created
Comparable RandomItem; // a variable used to store a random number (between
0 and 9) that is created in the following for loop and entered into the
queue as an object. Initially set to zero.
QueueReferenceBased aQueue = new QueueReferenceBased();
System.out.println("The queue consists of the following (top of list being
front of queue):");
for (int i = 0; i < aQueueSize; i++) {
RandomItem = (Comparable) ((int)(Math.random() * 10 )); // 0 to 9
aQueue.enqueue(new Integer(RandomItem));
} // end for
System.out.println("The smallest number in the queue is:");
System.out.println(findSmallestInQueue(aQueue, aQueueSize));

I have wrote code to make the queue, and to test everything before
constructing most of the findSmallestInQueue method i have set the queue
size as 1, and used an if statement to just return the one item in the queue
(of size 1).

Any help would be grateful!!
Especially, if someone could help me sort out what data types are needed
where and how to convert any that need converting!

I'm not asking for anyone to do it all, because that's my job, just some
help! (I don't want to copy anyone as I am having to do this, as well as
most of my programming year, because we copied, and got caught!!)

Thanks, Rohit

The other files needed refer to the interface and constructor classes for
the queue:

***************** (THE INTERFACE)
public interface QueueInterface {

public boolean isEmpty();
// Determines whether a queue is empty.
// Precondition: None.
// Postcondition: Returns true if the queue is empty;
// otherwise returns false.

public void enqueue(Object newItem) throws QueueException;
// Adds an item at the back of a queue.
// Precondition: newItem is the item to be inserted.
// Postcondition: If the operation was successful, newItem
// is at the back of the queue. Some implementations
// may throw QueueException if newItem cannot be added
// to the queue.
public Object dequeue() throws QueueException;
// Retrieves and removes the front of a queue.
// Precondition: None.
// Postcondition: If the queue is not empty, the item
// that was added to the queue earliest is returned and
// the item is removed. If the queue is empty, the
// operation is impossible and QueueException is thrown.

public void dequeueAll();
// Removes all items of a queue.
// Precondition: None.
// Postcondition: The queue is empty.

public Object peek() throws QueueException;
// Retrieves the item at the front of a queue.
// Precondition: None.
// Postcondition: If the queue is not empty, the item
// that was added to the queue earliest is returned.
// If the queue is empty, the operation is impossible
// and QueueException is thrown.
} // end QueueInterface
public class QueueReferenceBased
implements QueueInterface {
private Node lastNode;

public QueueReferenceBased() {
lastNode = null;
} // end default constructor

// queue operations:
public boolean isEmpty() {
return lastNode == null;
} // end isEmpty

public void dequeueAll() {
lastNode = null;
} // end dequeueAll

public void enqueue(Object newItem) {
Node newNode = new Node(newItem);

// insert the new node
if (isEmpty()) {
// insertion into empty queue
else {
// insertion into nonempty queue
} // end if

lastNode = newNode; // new node is at back
} // end enqueue

public Object dequeue() throws QueueException {
if (!isEmpty()) {
// queue is not empty; remove front
Node firstNode = lastNode.getNext();
if (firstNode == lastNode) { // special case?
lastNode = null; // yes, one node in queue
else {
} // end if
return firstNode.getItem();
else {
throw new QueueException("QueueException on dequeue:"
+ "queue empty");
} // end if
} // end dequeue

public Object peek() throws QueueException {
if (!isEmpty()) {
// queue is not empty; retrieve front
Node firstNode = lastNode.getNext();
return firstNode.getItem();
else {
throw new QueueException("QueueException on peek:"
+ "queue empty");
} // end if
} // end peek

} // end QueueReferenceBased
public class Node {
private Object item;
private Node next;

public Node(Object newItem) {
item = newItem;
next = null;
} // end constructor

public Node(Object newItem, Node nextNode) {
item = newItem;
next = nextNode;
} // end constructor

public void setItem(Object newItem) {
item = newItem;
} // end setItem

public Object getItem() {
return item;
} // end getItem

public void setNext(Node nextNode) {
next = nextNode;
} // end setNext

public Node getNext() {
return next;
} // end getNext

} // end class Node

public class QueueException extends RuntimeException {

public QueueException(String s) {
} // end constructor
} // end QueueException



No Answers Found. Be the First, To Post Answer.

Didn't find what you were looking for? Find more on Easy Question Or get search suggestion and latest updates.