Source Code : By Java
//main_file.java
import javax.swing.JFrame;
public class main_file {
public static void main(String args[])
{
shortest_job_first f = new shortest_job_first();
f.setVisible(true);
f.setBounds(100, 50, 700, 550);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
//shortest_job_first.java
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.StringTokenizer;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class shortest_job_first extends JFrame {
JPanel p1,p2,p3,p11,p12,p13,p14,p15,p16,p17;
JButton b1,b2;
JTextField process,Arrival_time,brust_time;
JLabel l1,l2,l3,top;
public shortest_job_first() {
p1= new JPanel();
p1.setBackground(Color.WHITE);
add(p1);
p1.setLayout(new GridLayout(2,1));
p2= new JPanel();
p2.setBackground(Color.white);
p1.add(p2);
p3= new JPanel();
p3.setBackground(Color.white);
p1.add(p3);
p2.setLayout(new GridLayout(7,1));///divide first panel into seven and then enter one text field after another
p11= new JPanel();
p11.setBackground(Color.white);
p2.add(p11);
p12= new JPanel();
p12.setBackground(Color.white);
p2.add(p12);
p13= new JPanel();
p13.setBackground(Color.white);
p2.add(p13);
p14= new JPanel();
p14.setBackground(Color.white);
p2.add(p14);
p15= new JPanel();
p15.setBackground(Color.white);
p2.add(p15);
p16= new JPanel();
p16.setBackground(Color.white);
p2.add(p16);
p17= new JPanel();
p17.setBackground(Color.white);
p2.add(p17);
l1= new JLabel("Process");
l1.setPreferredSize(new Dimension(100,30));
l1.setFont(new Font("Times new Roman", Font.PLAIN, 18));
p13.add(l1);
process= new JTextField();
process.setPreferredSize(new Dimension(400,30));
process.setFont(new Font("Times new Roman", Font.BOLD, 18));
p13.add(process);
l2= new JLabel("Arrival Time");
l2.setPreferredSize(new Dimension(100,30));
l2.setFont(new Font("Times new Roman", Font.PLAIN, 18));
p14.add(l2);
Arrival_time= new JTextField();
Arrival_time.setPreferredSize(new Dimension(400,30));
Arrival_time.setFont(new Font("Times new Roman", Font.BOLD, 18));
p14.add(Arrival_time);
l3= new JLabel("Brust Time");
l3.setPreferredSize(new Dimension(100,30));
l3.setFont(new Font("Times new Roman", Font.PLAIN, 18));
p15.add(l3);
brust_time= new JTextField();
brust_time.setPreferredSize(new Dimension(400,30));
brust_time.setFont(new Font("Times new Roman", Font.BOLD, 18));
p15.add(brust_time);
b1= new JButton("SJF");//Shortest job First button
b1.setPreferredSize(new Dimension(100,30));
b1.setBackground(Color.white);
b1.setFont(new Font("Times new Roman", Font.BOLD, 18));
//Action of b1(sjf button)
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try{
String s1 = process.getText();
String s2 = Arrival_time.getText();
String s3 = brust_time.getText();
int pro = Integer.parseInt(s1);
int min_data=99999;
StringTokenizer token = new StringTokenizer(s2, ",");
StringTokenizer token1 = new StringTokenizer(s3, ",");
String[] proces = new String[1000];
int arrival[] = new int[1000];
int brust[] = new int[1000];
boolean complete[] = new boolean[pro + 1];
int i = 0;
while (token.hasMoreElements()) {
String tokenizer = token.nextToken();
int data = Integer.parseInt(tokenizer);
arrival[i] = data;
i++;
}
i = 0;
while (token1.hasMoreElements()) {
String tokenizer = token1.nextToken();
int data = Integer.parseInt(tokenizer);
brust[i] = data;
i++;
}
for (i = 0; i < pro; i++) {
proces[i] = "p" + (i + 1);
}
int j;
int temp;
String sp;
shorting(pro, brust, arrival, proces, complete);
int total_brust_time = 0;
for (i = 0; i < pro; i++) {
total_brust_time += brust[i];
if (brust[i] == 0) {
complete[i] = true;
}
}
int thistime = 0;
String queue[] = new String[total_brust_time + 1000];
int k = 0;
int tag = 0;
int m = 0;
for (i = 0; ; i++) {
tag = 0;
for (j = 0; j < pro; j++) {
if (complete[j] == true) {
m = 0;
} else {
m = 1;
break;
}
}
if (m == 0){
break;
}
for (j = 0; j < pro; j++) {
if (arrival[j] <= thistime && complete[j] == false) {
tag = 1;
queue[k] = proces[j];
k++;
thistime++;
brust[j]--;
if (brust[j] == 0) {
complete[j] = true;
}
break;
}
}
if (tag == 0) {
queue[k] = "st";
k++;
thistime++;
}
shorting(pro, brust, arrival, proces, complete);
}
//This part printing process into a panel or GANTT CHART
Graphics g = p3.getGraphics();
int x1 = 40, x2 = 30, y1 = 40, y2 = 40;
int drawstrx1 = (x1 + 17);
int drawstry1 = 55;
int drawfirstx1 = x1 - 3;
int drawfirsty1 = 85;
g.setColor( Color.MAGENTA);
g.drawString("GANTT CHART", 40, 10);
int x=0;
for (i = 0; i < thistime; i++) {
x=1;
g.setColor(Color.black);
g.drawRect(x1, x2, y1, y2);
g.setColor(Color.blue);
g.drawString("" + queue[i], drawstrx1, drawstry1);
g.setColor(Color.black);
g.drawString("" + i, drawfirstx1, drawfirsty1);
x1 = x1 + y1;
drawfirstx1 = x1 - 3;
drawstrx1 = (x1+17);
Thread.sleep(1000);
}
if(x==1)
g.drawString("" + i, drawfirstx1, drawfirsty1);
}catch(Exception e){
}
}
// method for sorting (bubble sort)
private void shorting(int pro, int[] brust, int[] arrival,
String[] proces, boolean[] complete) {
int i, j, temp;
String sp;
boolean m;
for (i = 0; i < pro - 1; i++) {
for (j = i + 1; j < pro; j++) {
if (brust[i] > brust[j]) {
temp = brust[i];
brust[i] = brust[j];
brust[j] = temp;
temp = arrival[i];
arrival[i] = arrival[j];
arrival[j] = temp;
sp = proces[i];
proces[i] = proces[j];
proces[j] = sp;
m = complete[i];
complete[i] = complete[j];
complete[j] = m;
}
}
}
}
});
p16.add(b1);
b2= new JButton("RESET");//Reset button
b2.setPreferredSize(new Dimension(100,30));
b2.setBackground(Color.white);
b2.setFont(new Font("Times new Roman", Font.BOLD, 18));
//action of reset button
b2.addActionListener( new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try{
process.setText(null);
Arrival_time.setText(null);
brust_time.setText(null);
p3.repaint();
}catch(Exception ex){
}
}
});
p16.add(b2);
top= new JLabel("Shortest Job First (SJF) Scheduling Algorithm");
top.setForeground(Color.black);
top.setFont(new Font("Times new Roman", Font.BOLD, 24));
p12.add(top);
}
}