Round Robin

Published on February 2017 | Categories: Documents | Downloads: 30 | Comments: 0 | Views: 280
of 2
Download PDF   Embed   Report

Comments

Content

import java.awt.*; import javax.swing.*; import java.awt.event.*; class round extends JFrame implements ActionListener { JButton jb[]=new JButton[3]; JTextField jt1[],jt2[]; JLabel j1[],j11,j12,j13; JPanel jp,jp1; Container con; int k,p,q; String str[]={"SUBMIT","RESET","EXIT"}; String str1[]={"Process"," AT","WT","TAT"}; public round() { super("round scheduling algorithm"); con=getContentPane(); k=Integer.parseInt(JOptionPane.showInputDialog("E nter no. of processes:")); q=Integer.parseInt(JOptionPane.showInputDialog("E nter quantum time:")); j11=new JLabel("Process"); j12=new JLabel("arrival time"); j13=new JLabel("Service Time"); j1=new JLabel[k]; jt1=new JTextField[k]; jt2=new JTextField[k]; for(int i=0;i<k;i++) { j1[i]=new JLabel("Process"+(i+1)); jt1[i]=new JTextField(10); jt2[i]=new JTextField(10); } for(int i=0;i<3;i++) { jb[i]=new JButton(str[i]); } con.setLayout(new GridLayout(k+2,3)); con.add(j11); con.add(j12); con.add(j13); int l=0; for(int i=0;i<k;i++) { con.add(j1[l]); con.add(jt1[l]); con.add(jt2[l]); l++; } l=0; for(int i=0;i<3;i++) { con.add(jb[l]); jb[l].addActionListener(this); l++; } }

public static void main(String[] args) { round win=new round(); win.setSize(400,300); win.setVisible(true); win.setDefaultCloseOperation(JFrame.EXIT_ON_CL OSE); } public void actionPerformed(ActionEvent ae) { int a[]=new int[k]; int b[]=new int[k]; int p[]=new int[k]; int fb[]=new int[10]; int wait[]=new int[10]; int i,j,temp1,temp2,temp3; int wtc=0,lc=0,pn=0; int bb[]=new int[10]; int wt[]=new int[k]; int tat[]=new int[k]; float twt=0,ttat=0,avg; int fp[]=new int[30]; int ft[]=new int[30]; JPanel main=new JPanel(); main.setLayout(new BorderLayout()); jp=new JPanel(); jp1=new JPanel(); jp.setLayout(new GridLayout(k+1,7)); jp1.setLayout(new FlowLayout()); if(ae.getSource()==jb[2]) { System.exit(0); } else if(ae.getSource()==jb[0]) { for(i=0;i<k;i++) { a[i]=Integer.parseInt(jt1[i].getText()); b[i]=Integer.parseInt(jt2[i].getText()); p[i]=i+1; } for(i=0;i<k;i++) { for(j=i+1;j<k;j++) { if(a[i]>a[j]) { temp1=a[i]; a[i]=a[j]; a[j]=temp1; temp2=b[i]; b[i]=b[j]; b[j]=temp2; temp3=p[i]; p[i]=p[j]; p[j]=temp3; } }

} for(i=0;i<k;i++) { lc=b[i]+lc; fb[i]=b[i]; if(b[i]%q==0) bb[i]=(int)(b[i]/q)-1; else bb[i]=(int)(b[i]/q); } lc=lc/q+1; for(i=0;i<lc;i++) { for(j=0;j<k;j++) { if(b[j]>0) { wait[j]=wtc; if(b[j]>=q) { b[j]=b[j]-q; wtc+=q; } else { wtc+=b[j]; b[j]=b[j]-p[j]; } ft[pn]=wtc; fp[pn++]=p[j]; } } } for(i=0;i<k;i++) { wt[i]=(wait[i]-a[i])-(bb[i]*q); twt=twt+wt[i]; tat[i]=wt[i]+fb[i]; ttat=ttat+tat[i]; } for(i=0;i<4;i++) { jp.add(new JLabel(str1[i])); } for( i=0;i<k;i++) { jp.add(new JLabel("Process"+(p[i]))); jp.add(new JLabel(" "+a[i])); jp.add(new JLabel(" "+wt[i])); jp.add(new JLabel(" "+tat[i])); } avg=twt/k; String str2="Average Waiting time: "+avg; jp1.add(new JLabel(str2)); avg=ttat/k; str2="Average turn around time: "+avg; jp1.add(new JLabel(str2));

String str3=""; String str4="0 "; for(i=0;i<pn;i++) { str3+="P"+fp[i]+"|"; str4+=""+ft[i]+" "; } jp1.add(new JLabel(str3)); jp1.add(new JLabel(str4)); main.add(jp,BorderLayout.NORTH); main.add(jp1,BorderLayout.SOUTH); JOptionPane.showMessageDialog(null,main,"output", JOptionPane.PLAIN_MESSAGE); } else if(ae.getSource()==jb[1]) { setVisible(false); round win =new round(); win.setSize(400,300); win.setVisible(true); win.setDefaultCloseOperation(JFrame.EXIT_ON_CL OSE); } } }

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close