Internet Marketing Business Forum


Internet Marketing Business Forum » MAIN FORUMS » Copywriting & Content Creation » How do I write a method that displays the content of the queue?
Copywriting & Content Creation Copywriting is the most valuable skill you can learn. Learn how to influence people with words and make them click your order button. Ask for and do sales copy critiques. Everything about writing articles, getting content for your websites or distributing it.

Reply
 
LinkBack Thread Tools Display Modes
Old 07-29-2009, 06:31 PM   #1 (permalink)
 rocafella5007's Avatar
 
Status: Junior Member
Join Date: Jul 2009
Posts: 1
rocafella5007 is on a distinguished road
Default How do I write a method that displays the content of the queue?

// Queue.java
// demonstrates queue
// to run this program: C>java QueueApp

class Queue
{
private int maxSize;
private long[] queArray;
private int front;
private int rear;
private int nItems;
//--------------------------------------------------------------
public Queue(int s) // constructor
{
maxSize = s;
queArray = new long[maxSize];
front = 0;
rear = -1;
nItems = 0;
}
//--------------------------------------------------------------
public void insert(long j) // put item at rear of queue
{
if(rear == maxSize-1) // deal with wraparound
rear = -1;
queArray[++rear] = j; // increment rear and insert
nItems++; // one more item
}
//--------------------------------------------------------------
public long remove() // take item from front of queue
{
long temp = queArray[front++]; // get value and incr front
if(front == maxSize) // deal with wraparound
front = 0;
nItems--; // one less item
return temp;
}
//--------------------------------------------------------------
public long peekFront() // peek at front of queue
{
return queArray[front];
}
//--------------------------------------------------------------
public boolean isEmpty() // true if queue is empty
{
return (nItems==0);
}
//--------------------------------------------------------------
public boolean isFull() // true if queue is full
{
return (nItems==maxSize);
}
//--------------------------------------------------------------
public int size() // number of items in queue
{
return nItems;
}
//--------------------------------------------------------------
} // end class Queue
import java.util.Scanner;
class QueueApp
{
public static void main(String[] args)
{
Queue theQueue = new Queue(5); // queue holds 5 items

Scanner scannerObject = new Scanner(System.in);

theQueue.insert(scannerObject.nextInt());

theQueue.insert(10); // insert 4 items
theQueue.insert(20);
theQueue.insert(30);
theQueue.insert(40);

theQueue.remove(); // remove 3 items
theQueue.remove(); // (10, 20, 30)
theQueue.remove();

theQueue.insert(50); // insert 4 more items
theQueue.insert(60); // (wraps around)
theQueue.insert(70);
theQueue.insert(80);

while( !theQueue.isEmpty() ) // remove and display
{ // all items
long n = theQueue.remove(); // (40, 50, 60, 70, 80)
System.out.print(n);
System.out.print(" ");
}
System.out.println("");
} // end main()
} // end class QueueApp
rocafella5007 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Old 07-29-2009, 07:35 PM   #2 (permalink)
 Nameless's Avatar
 
Status: Junior Member
Join Date: Jul 2009
Posts: 1
Nameless is on a distinguished road
Default

The easiest way would be to add a method in your queue class that would look something like:
public long getNum(int index)
{
return queArray[index];
}
then your output code would look like
for (int i = 0; i < theQueue.size();i++)
{
System.out.println( theQueue.getNum(i) );
}
Nameless is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Old 07-29-2009, 07:42 PM   #3 (permalink)
 
Status: Junior Member
Join Date: Jul 2009
Posts: 2
femtorgon2 is on a distinguished road
Default

I must admit, I'm not all that comfortable with your Queue implementation here. Particularly the wraparound behavoir. When a node is inserted and wraps past first, first is unchanged. Also, nItems is incremented, even though one of the items has been overwritten. Neither of these seems to agree with the way data is represented.

Anyway, I've written three toString methods, choose whatever is the most appropriate (of course, none of these will change any data associated with the queue):

public String toString()
{
String outStr = queArray[front]+" ";
int iter = front;
while (iter!=rear)
{
iter++;
if(iter == maxSize)
iter = 0;
outStr = outStr + queArray[iter]+" ";
}
return outStr;
}

This is the most logical sort of toString for a Queue, it starts at front, and builds a string of the values until it reaches rear. Because of the problems mentioned above, this may not make the most sense for you.

public String toStringStraight()
{
String outStr = "";
int iter = 0;
while (iter!=maxSize)
{
outStr = outStr + queArray[iter]+" ";
iter++;
}
return outStr;
}

This is a standard array toString. It just builds it's output string of the array from index 0 to max-1.

public String toStringMod()
{
String outStr = "";
int iter = front;
int count = nItems;
while (count!=0)
{
outStr = outStr + queArray[iter]+" ";
iter++;
if(iter == maxSize)
iter = 0;
count--;
}
return outStr;
}

This one is designed to make the most sense with your implementation. It loops through the array, starting with front, until it has added nItems numbers to the output String. This is the same output you will get from removing everything from the queue as you have done at the end of QueueApp.
If you're sure you like the way your Queue works, then this would probably be the one to use.
femtorgon2 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Reply
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT -4. The time now is 01:07 AM.
Powered by vBulletin® Version 3.7.0 Beta 6
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.


Nav Item BG