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.

WT[i] = Integer.parseInt(txtField2[i].getText())-1;
WT[i] += Integer.parseInt(txtField2[i].getText())-Integer.parseInt(txtField[i].getText());
ie
WT[i] = 2* Integer.parseInt(txtField2[i].getText()) -Integer.parseInt(txtField[i].getText()) -1;

That looks odd to me, but anyway, have you printed ALL the values returned by all these parseInts to see if that are what you expect? Also, there's nothing in that part of the code that should change on the 4th value, so it seems more likely the problem starts elsewhere.

Hi, yes i also cant see too why it should change at the 4th value. I will try to check if the errror could be somewhere else.
Actually i checked my parseInts but i couldnt see something odd.

OMG...!!! Somehow my whole calculation seems wrong after i tested it again.
Man, it seems like another night without sleep again :(

OK. I was having a little trouble trying to guess what kind of waiting time calc involved doubling the value from one field before subtracting the second - but that's my problem, not yours!

again change JTextField to JFormattedTextField with Date/Time formatter, from there are comings more than 90pct of your *** nigth(s)

or put that to JTable with Date.class for TableColumn

@mKorbel

I never used JFormattedTextField with Date/Time formatter. The other thing is, i dont understad how the Date/Time formatter can help me.

The Wating Time is calculated from the Burst Time of the priviews processes.

not it isn't calculated anything (difference between Time value), nor with correct way, I'm outta from this thread

To be fair to mKorbel, the formatted text field suggestion would help to ensure that the input you are getting from the user is at least in a valid format. If you have confirmed that your parse of the input data is giving the expected value then that's not the real problem. However, if you were building this for real end-users it would be better to use formatted input fields.
Personally, I still suspect that formula ;-)

I will work a little on my formula. Maybe i am lucky...

Anyways, thank you... :)

This article has been dead for over six months. Start a new discussion instead.