hi everybody! i have this code for queues but it doesn't execute due to final Queue s = new Queue(); what should i do with this? pls help me :(
// Testing the Queue class of the java.util package
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class QueueTest extends JFrame
{
public QueueTest()
{
super( "Queue Trial ^_^" );
Container c = getContentPane();
final JLabel status = new JLabel();
__
c.setLayout( new FlowLayout() );
c.add( new JLabel( "Enter a string" ) );
final JTextField input = new JTextField( 10 );
c.add( input );
JButton enqueueBtn = new JButton( "Enqueue" );
enqueueBtn.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
status.setText( "Enqueued: " + s.enqueue( input.getText() ) );
}
} );
c.add( enqueueBtn );
JButton dequeueBtn = new JButton( "Dequeue" );
dequeueBtn.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
try
{
status.setText( "Dequeued: " + s.dequeue() );
}
catch ( EmptyStackException exception )
{
status.setText( exception.toString() );
}
}
} );
c.add( dequeueBtn );
JButton peekBtn = new JButton( "Peek" );
peekBtn.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
try
{
status.setText( "Top: " + s.peek() );
}
catch ( EmptyStackException exception )
{
status.setText( exception.toString() );
}
}
} );
c.add( peekBtn );
JButton emptyBtn = new JButton( "Is Empty?" );
emptyBtn.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
status.setText( s.isEmpty() ? "Queue is empty" : "Stack is not empty" );
}
} );
c.add( emptyBtn );
JButton searchBtn = new JButton( "Search" );
searchBtn.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
String searchKey = input.getText();
int result = s.search(searchKey);
if ( result == -1 )
status.setText( searchKey + " not found" );
else
status.setText( searchKey + " found at element " + result );
}
} );
c.add( searchBtn );
JButton displayBtn = new JButton( "Display" );
displayBtn.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
Enumeration en = s.elements();
StringBuffer buf = new StringBuffer();
while ( en.hasMoreElements() )
buf.append( en.nextElement() ).append( " " );
JOptionPane.showMessageDialog( null, buf.toString(), "Display", JOptionPane.PLAIN_MESSAGE );
}
} );
c.add( displayBtn );
c.add( status );
setSize( 675, 100 );
show();
}
public static void main( String args[] )
{
QueueTest app = new QueueTest();
app.addWindowListener( new WindowAdapter()
{
@Override
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
} );
}
}