Thursday, March 26, 2009

Program




import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import java.sql.*;

public class TestQueueIntSimple extends JPanel implements ActionListener{

// initialize the class variables,access specifier are using private
private JLabel labelt;
private JFrame frame;
private JPanel panel;
//private JLabel labelmid;
private JLabel putvalueLable;
private JLabel takenvalueLable;
private JLabel queuedepthLable;
private JLabel errormsgLable;


private JTextField putvalue;
private JTextField takenvalue;

private JTextField queuedepth;
public JTextField errormsg;


private JButton button;
private JButton button2;
private JButton button3;

private JTextArea tarea;
private JScrollPane scrollpane;
QueueIntSimple qs = new QueueIntSimple();

public TestQueueIntSimple(){

frame= new JFrame(" -- Queue Implemantion -- ");
frame.getContentPane().setLayout(null);

putvalueLable = new JLabel("Enter value to put");
putvalue = new JTextField(15);
takenvalueLable = new JLabel("Value taken");
takenvalue = new JTextField(15);

queuedepthLable = new JLabel("Queue depth");
queuedepth = new JTextField(15);

labelt = new JLabel("Queue Implemantion");
labelt.setFont(new Font("Serif", Font.BOLD, 25));
errormsgLable = new JLabel("Error messages");
errormsg = new JTextField(15);




button = new JButton("PUT");
button2 = new JButton("TAKE");
button3 = new JButton("DISPLAY");
tarea = new JTextArea();

labelt.setBounds(250,20,350,70);

putvalueLable.setBounds(50,70,150,70);
takenvalueLable.setBounds(50,120,150,70);

putvalue.setBounds(220,90,60,27);
takenvalue.setBounds(220,140,60,27);

queuedepthLable.setBounds(50,170,150,70);
queuedepth.setBounds(220,190,60,27);

errormsgLable.setBounds(50,220,150,70);

errormsg.setBounds(220,240,180,27);




button.setBounds(100,280,80,27);
button2.setBounds(200,280,80,27);
button3.setBounds(300,280,120,27);
tarea.setBounds(220,320,180,180);

frame.getContentPane().add(labelt);
frame.getContentPane().add(putvalueLable);
frame.getContentPane().add(putvalue);
frame.getContentPane().add(takenvalueLable);
frame.getContentPane().add(takenvalue);

frame.getContentPane().add(queuedepthLable);
frame.getContentPane().add(queuedepth);
frame.getContentPane().add(errormsgLable);
frame.getContentPane().add(errormsg);



frame.getContentPane().add(button);
frame.getContentPane().add(button2);
frame.getContentPane().add(button3);
frame.getContentPane().add(tarea);

button.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);

frame.setSize(550,550);
frame.setVisible(true);



}


public void actionPerformed(ActionEvent event){
if(event.getSource() == button)
{
int value = Integer.parseInt(putvalue.getText());
if(qs.queueDepth() == 5){
errormsg.setText("Queue is full");
}
qs.putValue(value);
queuedepth.setText(qs.queueDepth()+"");
}else if(event.getSource() == button2){
if(qs.queueDepth() == 0){
errormsg.setText("Queue is empty");
}
takenvalue.setText(qs.takenValue()+"");
queuedepth.setText(qs.queueDepth()+"");
System.out.println("vvvvv"+qs.queueDepth());
}else if(event.getSource() == button3){
tarea.setText(qs.toString());
}


}


public static void main(String arg[]){
TestQueueIntSimple tqs = new TestQueueIntSimple();
}
}




----------------------------------

public class QueueIntSimple {
private int queue[]; //array in int
private int capacity; //capacity of the queue
private int count; //indicates number of elements currently stored in the queue
// elements[0] is front, elements[count-1] is back

public QueueIntSimple() {
queue = new int[5];
capacity =5;
}


public int getSize() { return count; }
public boolean isEmpty() { return count==0; }
public boolean isFull() { return count == capacity; }


public void putValue(int value) {
if(isFull()) {
System.out.println("Queue is full");
return;
}
System.out.println("Inserting " + value + " at Queue[" + count + "]");
queue[count++]=value;
}


public int poll() {
if(isEmpty()) {
System.out.println("Poll:Queue is empty");

}
return queue[0];
}


public int takenValue() {
int item = poll();
count--;
// shift elements up
for(int i=0; i queue[i] = queue[i + 1];
}
return item;
}

public int queueDepth() {
int depth = 0;
for(int i=0; i<5; i++) {
if(0< queue[i]){
depth++;
}
}
return depth;
}


public String toString() {
String s = "";
for(int i = 0; i if (queue[i] > 0) {
s =s+"Queue["+i+"] : "+queue[i]+"\n";
}
}
return s;
}

public static void main(String ar[]){
QueueIntSimple qs = new QueueIntSimple();
qs.putValue(4);
qs.putValue(2);
qs.putValue(1);

System.out.println(qs.toString());

qs.takenValue();

System.out.println(qs.toString());

}
}

No comments:

Post a Comment