Hello guys...
I have a strange problem on my calculation for a First in First Out Algorithm.
The problem is, when i have to calculate 4 processes and above, the waiting time on my processes are wrong, but when i have to calculate only up to 3 processes it is correct.
Provided the 1 process has an arrival time of 0 and the follwing processes the arrival time is in a ascending order or at least equal with the previous process.
I commented my waiting time calculation part in my codes.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class fcfs extends JFrame implements ActionListener
{
JButton cmdButton[] = new JButton[3];
JTextField txtField[],txtField2[];
JLabel lblPromt[],lblPromt2,lblPromt3,lblPromt4;
JPanel frmPanel,frmPanel2;
Container con;
int k,p;
String buttonCaption[] = {"APPLY","RESET","EXIT"};
String labelCaption[] = {"Process"," Arrival Time ","Burst Time ","Waiting Time"};
public fcfs()
{
super("FCFS Scheduling Algorithm");
con = getContentPane();
k= Integer.parseInt(JOptionPane.showInputDialog("Enter Number Of Process"));
lblPromt2 = new JLabel("Process");
lblPromt3 = new JLabel("Arrival Time");
lblPromt4 = new JLabel("Burst Time");
lblPromt = new JLabel[k];
txtField = new JTextField[k];
txtField2 = new JTextField[k];
for(int i=0;i<k;i++)
{
lblPromt[i] = new JLabel("Process "+(i+1));
txtField[i] = new JTextField(10);
txtField2[i] = new JTextField(10);
}
for(int i=0;i<3;i++)
{
cmdButton[i] = new JButton(buttonCaption[i]);
}
con.setLayout(new GridLayout(k+2,3));
con.add(lblPromt2);
con.add(lblPromt3);
con.add(lblPromt4);
int l=0;
for(int i=0;i<k;i++)
{
con.add(lblPromt[l]);
con.add(txtField[l]);
con.add(txtField2[l]);
l++;
}
l=0;
for(int i=0;i<3;i++)
{
con.add(cmdButton[l]);
cmdButton[l].addActionListener(this);
l++;
}
}
public void actionPerformed(ActionEvent ae)
{
int WT[] = new int[k];
float sum=0;
float avg;
JPanel main = new JPanel();
main.setLayout(new BorderLayout());
frmPanel = new JPanel();
frmPanel2 = new JPanel();
frmPanel.setLayout(new GridLayout(k+1,5));
frmPanel2.setLayout(new FlowLayout());
if(ae.getSource() == cmdButton[2])
{
System.exit(0);
}
else if(ae.getSource() == cmdButton[0])
{
Integer.parseInt(txtField2[0].getText());
for(int i=0;i<k;i++)
{
if(i==0 || i < 0)
{
WT[i] = 0;
}
else
{
{
WT[i] = Integer.parseInt(txtField2[i].getText())-1;
WT[i] += Integer.parseInt(txtField2[i].getText())-Integer.parseInt(txtField[i].getText()); //beginning at process 4 calculation becomes wrong
}
}
sum +=WT[i];
}
for (int i=0;i<4;i++ )
{
frmPanel.add(new JLabel(labelCaption[i]));
}
for (int i=0;i<k;i++)
{
frmPanel.setBackground(Color.YELLOW);
frmPanel.add(new JLabel("Process"+(i+1)));
frmPanel.add(new JLabel(" "+Integer.parseInt(txtField[i].getText())));
frmPanel.add(new JLabel(""+Integer.parseInt(txtField2[i].getText())));
frmPanel.add(new JLabel(""+WT[i]));
}
avg = sum/k;
String avgWaitingTime = "Average Waiting Time is "+ avg;
frmPanel2.add(new JLabel(avgWaitingTime));
main.add(frmPanel,BorderLayout.NORTH);
main.add(frmPanel2,BorderLayout.SOUTH);
JOptionPane.showMessageDialog(null,main,"RESULT",JOptionPane.PLAIN_MESSAGE);
}
else if(ae.getSource() == cmdButton[1])
{
setVisible(false);
fcfs window = new fcfs();
window.setSize(400,300);
window.setLocation(570,300);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public static void main(String[] args)
{
fcfs window = new fcfs();
window.setSize(400,300);
window.setLocation(570,300);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Thank you for taking time for it... I really dont know anymore what to do.