Monday 21 August 2017

Transpose Matrix Java program

import java.util.*;
class trans
{
public static void main(String arg[])
{
int a[][]=new int[4][4];
int i,j,temp;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the elements of array");
for(i=0;i<4;i++)
 {
  for(j=0;j<4;j++)
   {
    a[i][j]=sc.nextInt();
   }
 }
for(i=0;i<4;i++)
 {
  for(j=0;j<4;j++)
   {
   temp=a[i][j];
   a[i][j]=a[j][i];
   a[j][i]=temp;
   }
 }
System.out.println("The TRANSPOSE OF MATRIX IS:");
for(i=0;i<4;i++)
   {
  for(j=0;j<4;j++)
   {
   System.out.print(a[i][j]+"\t");
   }
   System.out.println();
   } 
}


OUTPUT:

Enter the elements of array
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
The TRANSPOSE OF MATRIX IS:
1    2    3    4   
5    6    7    8   
9    10    11    12   
13    14    15    16   

Monday 14 August 2017

Student Management System SRS diagram (SOOAD)


Student Management System State Chart diagram (SOOAD)


Student Management System Communication diagram (SOOAD)



Student Management System Deployment diagram (SOOAD)


Student Management System Sequence diagram (SOOAD)



Student Management System CRC Card diagram (SOOAD)



Student Management System DFD diagram (SOOAD)









Student Management System Class diagram (SOOAD)


Student Management System Use Case diagram (SOOAD)


(sender side) Java Program to implement Sliding Window Protocol while sending a message travelling from sender to receiver (CN)

/*Aim: A Java Program implementing Sliding Window Protocol while sending a message travelling from sender to receiver. */
/*For complete explanation visit: Prof. Brinda's Blog*/
import java.io.DataInputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

public class slidsender
{
public static void main(String a[])throws Exception
{

ServerSocket ser=new ServerSocket(7870);
Socket s=ser.accept();
DataInputStream in=new DataInputStream(System.in);
DataInputStream in1=new DataInputStream(s.getInputStream());
String sbuff[]=new String[8];
PrintStream p;
int sptr=0,sws=8,nf,ano,i;
String ch;
do
{
p=new PrintStream(s.getOutputStream());
System.out.print("Enter the no. of frames : ");
nf=Integer.parseInt(in.readLine());
p.println(nf);
if(nf<=sws-1)
{

System.out.println("Enter "+nf+" Messages to be send\n");
for(i=1;i<=nf;i++)
{
sbuff[sptr]=in.readLine();
p.println(sbuff[sptr]);
sptr=++sptr%8;
}
sws-=nf;
System.out.print("Acknowledgment received");
ano=Integer.parseInt(in1.readLine());
System.out.println(" for "+ano+" frames");
sws+=nf;
}
else
{
System.out.println("The no. of frames exceeds window size");
break;
}
System.out.print("\nDo you wants to send some more frames : ");
ch=in.readLine(); p.println(ch);
}
while(ch.equals("yes"));
s.close();
}
}


OUTPUT:

//SENDER OUTPUT
Enter the no. of frames : 4
Enter 4 Messages to be send

hiii
how r u
i am fine
how is evryone
Acknowledgment received for 4 frames

Do you wants to send some more frames : no

(receiver side) Java Program to implementing Sliding Window Protocol while sending a message travelling from sender to receiver (CN)

/*Aim: A Java Program implementing Sliding Window Protocol while sending a message travelling from sender to receiver. */
/*For complete explanation visit: Prof. Brinda's Blog */
import java.io.DataInputStream; import java.io.PrintStream; import java.net.Socket; class slidreceiver { public static void main(String a[])throws Exception { Socket s=new Socket("localhost",7870); DataInputStream in=new DataInputStream(s.getInputStream()); PrintStream p=new PrintStream(s.getOutputStream()); int i=0,rptr=-1,nf,rws=8; String rbuf[]=new String[8]; String ch; System.out.println(); do { nf=Integer.parseInt(in.readLine()); if(nf<=rws-1) { for(i=1;i<=nf;i++) { rptr=++rptr%8; rbuf[rptr]=in.readLine(); System.out.println("The received Frame " +rptr+" is : "+rbuf[rptr]); } rws-=nf; System.out.println("\nAcknowledgment sent\n"); p.println(rptr+1); rws+=nf; } else break; ch=in.readLine(); } while(ch.equals("yes")); } } OUTPUT: //RECEIVER OUTPUT The received Frame 0 is : hiii The received Frame 1 is : how r u The received Frame 2 is : i am fine The received Frame 3 is : how is evryone

(sender side) Java Program to implement Stop and Wait Protocol while sending a message travelling from sender to receiver (CN)

/*Aim: A Java Program implementing Stop and Wait Protocol while sending a message travelling from sender to receiver. */
/*For complete explanation visit: Prof. Brinda's Blog*/
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.Socket; public class Sender{ Socket sender; ObjectOutputStream out; ObjectInputStream in; String packet,ack,str, msg; int n,i=0,sequence=0; Sender(){} public void run(){ try { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Waiting for Connection...."); sender = new Socket("localhost",2009); sequence=0; out=new ObjectOutputStream(sender.getOutputStream()); out.flush(); in=new ObjectInputStream(sender.getInputStream()); str=(String)in.readObject(); System.out.println("reciver > "+str); System.out.println("Enter the data to send...."); packet=br.readLine(); n=packet.length(); do{ try{ if(i<n){ msg=String.valueOf(sequence); msg=msg.concat(packet.substring(i,i+1)); } else if(i==n){ msg="end";out.writeObject(msg);break; } out.writeObject(msg); sequence=(sequence==0)?1:0; out.flush(); System.out.println("data sent>"+msg); ack=(String)in.readObject(); System.out.println("waiting for ack.....\n\n"); if(ack.equals(String.valueOf(sequence))){ i++; System.out.println("receiver > "+" packet recieved\n\n"); } else{ System.out.println("Time out resending data....\n\n"); sequence=(sequence==0)?1:0; } } catch(Exception e){} } while(i<n+1); System.out.println("All data sent. exiting."); } catch(Exception e){} finally{ try{ in.close(); out.close(); sender.close(); } catch(Exception e){} } } public static void main(String args[]){ Sender s=new Sender(); s.run(); } } OUTPUT: Waiting for Connection.... reciver > connected . Enter the data to send.... myname data sent>0m waiting for ack..... receiver > packet recieved data sent>1y waiting for ack..... receiver > packet recieved data sent>0n waiting for ack..... receiver > packet recieved data sent>1a waiting for ack..... Time out resending data.... data sent>1a waiting for ack..... receiver > packet recieved data sent>0m waiting for ack..... receiver > packet recieved data sent>1e waiting for ack..... receiver > packet recieved All data sent. exiting.

(receiver side) Java Program to implement Stop and Wait Protocol while sending a message travelling from sender to receiver (CN)

/*Aim: A Java Program implementing Stop and Wait Protocol while sending a message travelling from sender to receiver. */
/*For complete explanation visit: Prof. Brinda's Blog*/
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.ServerSocket; import java.net.Socket; public class Reciever{ ServerSocket reciever; Socket connection=null; ObjectOutputStream out; ObjectInputStream in; String packet,ack,data=""; int i=0,sequence=0; Reciever() { } public void run(){ try{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); reciever = new ServerSocket(2009,10); System.out.println("waiting for connection..."); connection=reciever.accept(); sequence=0; System.out.println("Connection established :"); out=new ObjectOutputStream(connection.getOutputStream()); out.flush(); in=new ObjectInputStream(connection.getInputStream()); out.writeObject("connected ."); do{ try{ packet=(String)in.readObject(); if(Integer.valueOf(packet.substring(0,1))==sequence){ data+=packet.substring(1); sequence=(sequence==0)?1:0; System.out.println("\n\nreceiver >"+packet); } else { System.out.println("\n\nreceiver >"+packet +" duplicate data"); } if(i<3){ out.writeObject(String.valueOf(sequence));i++; } else{ out.writeObject(String.valueOf((sequence+1)%2)); i=0; } } catch(Exception e){} }while(!packet.equals("end")); System.out.println("Data recived="+data); out.writeObject("connection ended ."); } catch(Exception e){} finally{ try{ in.close(); out.close(); reciever.close(); } catch(Exception e){} } } public static void main(String args[]){ Reciever s=new Reciever(); while(true){ s.run(); } } }

Error Correction using Hamming Code (CN)

//Aim: Write a program for Error Correction using Hamming Code.
/*For complete explanation visit: Prof. Brinda's Blog*/

import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.*;
class HammingCode
{
   
    public static void main(String args[])
    {

    Scanner scr=new Scanner(System.in);
    System.out.println("This is hamming code error detection and correction using EVEN parity");
    System.out.println();
    System.out.println("Enter 4 data bits.D4 D3 D2 D1");
    int n=4;

    int d[]=new int[4]; //..........Array to accept data bits
    for(int i=n-1;i>=0;--i)
    {
    System.out.println("Enter the value of D"+(i+1));
    d[i]=scr.nextInt();
    }

    /*.............. Formula for calculating 2^k>=n+k+1 ...............*/

    int k=0;

    while(Math.pow(2,k)<(n+k+1))
    {
    ++k;
    }
    System.out.println();
    System.out.println(k+" parity bits are required for the transmission of data bits.");


    int p[]=new int[k]; //..........Array to store parity bits
    int h[]=new int[n+k+1];//.........Array to hold the hamming code.(n+k+1) as we start from pos 1.

    /********** Initializing array h[] to -1 ************/
    for(int i=0;i<7;++i)
    h[i]=-1;

    int count=0;
    int c=2;
    while(count<4)
    {
    ++c;
    if(c==4)
    continue;

    h[c]=d[count];
    ++count;
    }
    int p1[]={h[1],h[3],h[5],h[7]};
    int p2[]={h[2],h[3],h[6],h[7]};
    int p3[]={h[4],h[5],h[6],h[7]};
    int parity[]=new int[3];


    /************Setting the value of parity bit*************/
    parity[0]=set_parity_bit(p1);
    parity[1]=set_parity_bit(p2);
    parity[2]=set_parity_bit(p3);
   

    /************Inserting the parity bits in the hamming code**********/
    h[1]=parity[0];
    h[2]=parity[1];
    h[4]=parity[2];
   

    System.out.println("\nSENDER:");
    System.out.print("\nThe data bits entered are: ");
    for(int i=3;i>=0;--i)
    System.out.print(d[i]+" ");

    System.out.println("\nThe Parity bits are: ");
    for(int i=2;i>=0;--i)
    System.out.println("Value of P"+(i+1)+" is "+parity[i]+" ");

    System.out.print("\nThe Hamming code is as follows :-\nD4 D3 D2 P3 D1 P2 P1 \n");
    for(int i=(n+k);i>0;--i)
    System.out.print(h[i]+" ");

    System.out.println();
    System.out.println("\nEnter the hamming code with error at any position of your choice.");


    for(int i=7;i>0;--i)
    h[i]=scr.nextInt();

    int p4[]={h[1],h[3],h[5],h[7]};
    int p5[]={h[2],h[3],h[6],h[7]};
    int p6[]={h[4],h[5],h[6],h[7]};

    parity[0]=set_parity_bit(p4);
    parity[1]=set_parity_bit(p5);
    parity[2]=set_parity_bit(p6);

    int position=(int)(parity[2]*Math.pow(2,2)+parity[1]*Math.pow(2,1)+parity[0]*Math.pow(2,0));
    System.out.println("\nRECEIVER:");
    System.out.println("Error is detected at position "+position+" at the receiving end.");
    System.out.println("Correcting the error.... ");

    if(h[position]==1)
    h[position]=0;
    else
    h[position]=1;

    System.out.print("The correct code is ");
    for(int i=7;i>0;--i)
    System.out.print(h[i]+" ");
    }

   
    static int set_parity_bit(int a[])
    {
    int count=0;
    int l=a.length;

    for(int i=0;i<l;++i)
    if(a[i]==1)
    ++count;

    if((count%2)==0)
    return 0;
    else
    return 1;
    }

    }


OUTPUT

This is hamming code error detection and correction using EVEN parity

Enter 4 data bits.D4 D3 D2 D1
Enter the value of D4
1
Enter the value of D3
0
Enter the value of D2
1
Enter the value of D1
1

3 parity bits are required for the transmission of data bits.

SENDER:

The data bits entered are: 1 0 1 1
The Parity bits are:
Value of P3 is 0
Value of P2 is 0
Value of P1 is 1

The Hamming code is as follows :-
D4 D3 D2 P3 D1 P2 P1
1 0 1 0 1 0 1

Enter the hamming code with error at any position of your choice.
1
0
1
1
1
0
1

RECEIVER:
Error is detected at position 4 at the receiving end.
Correcting the error....
The correct code is 1 0 1 0 1 0 1

Error Detection (crc.java) (CN)

//Aim: Write a program for Error Detection usinng CRC.
/*For complete explanation visit: Prof. Brinda's Blog*/
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class crc { public static void main(String args[]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int[] data; int[] div; int[] divisor; int[] rem; int[] crc; int data_bits, divisor_bits, tot_length; System.out.println("Enter number of data bits : "); data_bits=Integer.parseInt(br.readLine()); data=new int[data_bits]; System.out.println("Enter data bits : "); for(int i=0; i<data_bits; i++) data[i]=Integer.parseInt(br.readLine()); System.out.println("Enter number of bits in divisor : "); divisor_bits=Integer.parseInt(br.readLine()); divisor=new int[divisor_bits]; System.out.println("Enter Divisor bits : "); for(int i=0; i<divisor_bits; i++) divisor[i]=Integer.parseInt(br.readLine()); // Display the Data and Divisor Bits System.out.print("\n Data bits are : "); for(int i=0; i< data_bits; i++) System.out.print(data[i]); System.out.print("\n Divisor bits are : "); for(int i=0; i< divisor_bits; i++) System.out.print(divisor[i]); tot_length=data_bits+divisor_bits-1; // Declaration div=new int[tot_length]; rem=new int[tot_length]; crc=new int[tot_length]; /*------------------ CRC GENERATION-----------------------*/ for(int i=0;i<data.length;i++) div[i]=data[i]; System.out.print("\n Dividend (after appending 0's) are : "); for(int i=0; i< div.length; i++) System.out.print(div[i]); System.out.println(); for(int j=0; j<div.length; j++) { rem[j] = div[j]; } rem=divide(div, divisor, rem); for(int i=0;i<div.length;i++) //append dividend and ramainder { crc[i]=(div[i]^rem[i]); } System.out.println(); System.out.println("CRC code : "); for(int i=0;i<crc.length;i++) System.out.print(crc[i]); /*-------------------ERROR DETECTION---------------------*/ System.out.println(); System.out.println("Enter CRC code of "+tot_length+" bits : "); for(int i=0; i<crc.length; i++) crc[i]=Integer.parseInt(br.readLine()); for(int j=0; j<crc.length; j++) { rem[j] = crc[j]; } rem=divide(crc, divisor, rem); for(int i=0; i< rem.length; i++) { if(rem[i]!=0) { System.out.println("Error"); break; } if(i==rem.length-1) System.out.println("No Error"); } } static int[] divide(int div[],int divisor[], int rem[]) { int cur=0; while(true) { for(int i=0;i<divisor.length;i++) rem[cur+i]=(rem[cur+i]^divisor[i]); while(rem[cur]==0 && cur!=rem.length-1) cur++; if((rem.length-cur)<divisor.length) break; } return rem; } } Output CRC WITH ERROR Enter number of data bits : 5 Enter data bits : 1 1 0 0 1 Enter number of bits in divisor : 3 Enter Divisor bits : 1 1 0 Data bits are : 11001 Divisor bits are : 110 Dividend (after appending 0's) are : 1100100 CRC code : 1100110 Enter CRC code of 7 bits : 1 0 0 0 1 1 0 Error CRC WITHOUT ERROR Enter number of data bits : 5 Enter data bits : 1 1 0 0 1 Enter number of bits in divisor : 3 Enter Divisor bits : 1 1 0 Data bits are : 11001 Divisor bits are : 110 Dividend (after appending 0's) are : 1100100 CRC code : 1100110 Enter CRC code of 7 bits : 1 1 0 0 1 1 0 No Error

Sort element of array in descending order Assembly Language Program using tasm (MP)

data segment
arr dw 5434h,6727h,4345h,1234h,3333h
data ends
code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
mov ch,04h
back:
lea si,arr
mov cl,04h
up:
mov ax,[si]
cmp ax,[si+2]
jnc down
mov bx,[si+2]
mov [si+2],ax
mov [si],bx
down:
inc si
inc si
dec cl
jnz up
dec ch
jnz back
int 03h
code ends
end start

Sort element of array in ascending order Assembly Language Program using tasm (MP)

data segment
arr dw 5434h,6727h,4345h,1234h,3333h
data ends
code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
mov ch,04h
back:
lea si,arr
mov cl,04h
up:
mov ax,[si]
cmp ax,[si+2]
jc down
mov bx,[si+2]
mov [si+2],ax
mov [si],bx
down:
inc si
inc si
dec cl
jnz up
dec ch
jnz back
int 03h
code ends
end start

Find the Smallest element from an array Assembly Language Program using tasm (MP)

data segment
arr dw 1111h,2222h,3333h,4444h,5555h
data ends
code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
mov cx,04h
lea si,arr
mov ax,[si]
up:
cmp ax,[si+2]
jc down
mov ax,[si+2]
down:
inc si
inc si
dec cx
jnz up
int 03h
code ends
end start

Find the largest element from an array Assembly Language Program using tasm (MP)

data segment
arr dw 2678h,2222h,3789h,0aaah,1111h,1234h,3234h,7568h,0a12h,8798h
data ends
code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
lea si,arr
mov cx,09h
mov ax,[si]                  ; loading 1st element of array in ax
up:
cmp ax,[si+2]
jnc down                     ; if ax is larger element in array
mov ax,[si+2]             ;swapping to store large element in array
down:
inc si
inc si
dec cx
jnz up
int 03h
code ends
end start

Implement 16bit addition Assembly Language Program using tasm (MP)

data segment          
n1 dw 1111h          
n2 dw 2222h           
data ends               
code segment         
assume cs:code,ds:data  
start:                
mov ax,data          
mov ds,ax          
mov ax,n1            
add ax,n2
int 03h              
code ends             
end start

implement division of 16bit Assembly Language Program using tasm (MP)

data segment
n1 dw 3333h
n2 dw 2222h
data ends
code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
mov ax,n1
div n2
int 03h
code ends
end start


Implement 32 subtraction Assembly Language Program using tasm (MP)

data segment
n1 dd 22222222h
n2 dd 11111111h
data ends
code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
mov ax,word ptr n1
mov bx,word ptr n2
sub ax,bx
mov cx,word ptr n1+2
mov dx,word ptr n2+2
sbb cx,dx
int 03h
code ends
end start

Implement 32bit addition Assembly Language Program using tasm (MP)

data segment
n1 dd 11111111h
n2 dd 22222222h
data ends
code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
mov ax,word ptr n1
mov bx,word ptr n2
add ax,bx
mov cx,word ptr n1+2
mov dx,word ptr n2+2
adc cx,dx
int 03h
code ends
end start

Wednesday 9 August 2017

Implement 16bit multiplication ALP using tasm (MP)

data segment
n1 dw 3333h
n2 dw 2222h
data ends
code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
mov ax,n1
mul n2
int 03h
code ends
end start

Implement 16bit subtraction Assembly Language Program using tasm (MP)

data segment
n1 dw 3333h
n2 dw 2222h
data ends               
code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
mov ax,n1
sub ax,n2
int 03h
code ends
end start      


Monday 7 August 2017

sjf.java (OS)

import java.util.Scanner;

class sjf
{
public static void main(String ... args)
{
int n,curTime=0,sumTAT=0,sumWT=0,tmp;
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of process : ");
n = sc.nextInt();
n=5;
int id[]=new int[n];
int BT[]=new int[n];
int WT[]=new int[n];
int TAT[]=new int[n];
for(int i=0; i<n; i++)
{
id[i]=(i+1);
System.out.println("Enter BT of #"+(i+1)+"No.: ");
BT[i]=sc.nextInt();
}
for(int i=0; i<(n-1); i++)
{
for(int j=0; j<(n-i-1); j++)
{
if(BT[j]>BT[j+1])
{
tmp=BT[j];
BT[j]=BT[j+1];
BT[j+1]=tmp;
tmp=id[j];
id[j]=id[j+1];
id[j+1]=tmp;
}
}
}
for(int i=0; i<n; i++)
{
WT[i]= curTime;
curTime+=BT[i];
TAT[i]= curTime;

sumWT+=WT[i];
sumTAT+=TAT[i];
}
System.out.println("AT\tBT\tWT\tTAT\n---------------------------------------");
for(int i=0; i<n; i++)
System.out.println(id[i]+"\t"+BT[i]+"\t"+WT[i]+"\t"+TAT[i]);
System.out.println("AvgWT = " + ((float)  sumWT/n) +"\nAvgTAT = " +  ((float) sumTAT/n));
}
}

Output:

Enter number of process :
5
Enter BT of #1No.:
2
Enter BT of #2No.:
8
Enter BT of #3No.:
7
Enter BT of #4No.:
4
Enter BT of #5No.:
3
AT      BT      WT      TAT
---------------------------------------
1       2       0       2
5       3       2       5
4       4       5       9
3       7       9       16
2       8       16      24
AvgWT = 6.4
AvgTAT = 11.2

fcfs_disk.java (OS)

#include<stdio.h>
#include<math.h>
int main()
{
    int n,tr[10],head,total,i,j;
    printf("entre the number of request: ");
    scanf("%d",&n);
   
    printf("entre the track number: ");
    for(i=0;i<n;i++)
    {
    scanf("%d",&tr[i]);
    }
    //printf("%d",tr[i]);
    printf("entre the initial head position: ");
    scanf("%d",&head);
    for(j=0;j<n;j++)
    {
        total = total + abs(tr[j]-head);
        head=tr[j];
        }
        printf("Total head movement: %d ",total);
       
   
    return 0;
}

/*
Output:

entre the number of request: 5
entre the track number: 20
30
40
50
60
entre the initial head position: 20
Total head movement: 40
*/

bankers_algo.java (OS)

/*

   Experiment: Banker's Algorithm
   Name: Chandresh Prasad

*/

import java.util.*;

class bankers_algo
{
    static int max [][], alloc[][],need[][],available[];
    static int n,m;
    static boolean finish[];
   
    private static void calcNeed()
    {
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
                need[i][j]=max[i][j]-alloc[i][j];
    }
   
    private static boolean isDone()
    {
        for(int i=0; i<n; i++)
            if(finish[i]==false)
                return false;
        return true;
    }
   
    private static boolean check(int i)
    {
        int j;
        for(j=0; j<m; j++)
            if(available[j]<need[i][j])
                return false;
        return true;
    }

   
    public static void main(String ... args)
    {
        Scanner sc=new Scanner(System.in);
       
        //taking inputs :
        System.out.print("Enter number of processes : ");
        n=sc.nextInt();
       
        System.out.print("Enter number of Resources : ");
        m=sc.nextInt();
       
        //allocating memory for required arrays
        max= new int[n][m];
        alloc = new int[n][m];
        need = new int[n][m];
        available = new int[m];
        finish = new boolean[n];
       
        System.out.println("Enter Max matrix");
        for(int i=0; i<n; i++)
            for(int j=0;j<m; j++)
                max[i][j]=sc.nextInt();
       
        System.out.println("Enter Alloctaion matrix");
        for(int i=0; i<n; i++)
            for(int j=0;j<m; j++)
                alloc[i][j]=sc.nextInt();
       
        System.out.println("Enter Available matrix");
        for(int i=0; i<m; i++)
            available[i]=sc.nextInt();
       
        //step1 : calculating need matrix
        calcNeed();
       
        //step2 : finding safe states
        int i=0;
        while(!isDone())
        {   
            //condition to select process for safe state
            if(!finish[i] && check(i))
            {
                System.out.println("safe state : " + i);
               
                finish[i]=true;
               
                //adding alloc with available
                for(int j=0; j<m; j++)
                    available[j]+=alloc[i][j];
                //start checking for safe state from start
                i=0;
            }
            else
                i++;
        }
       
        //printing all matrices ----
        System.out.print("\nTotal resources :  ");
        for(i=0; i<m; i++)
            System.out.print(available[i]+" ");
        System.out.println("");
       
        System.out.println("Max\tAlloc\tNeed");
        for(i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
                System.out.print(max[i][j]+" ");
            System.out.print("\t");
           
            for(int j=0; j<m; j++)
                System.out.print(alloc[i][j]+" ");
            System.out.print("\t");
           
            for(int j=0; j<m; j++)
                System.out.print(need[i][j]+" ");
            System.out.print("\n");
        }
    }
}

fifo.java (OS)

import java.io.*;
class Stack
{
private int array[], frameSize, top, hit, fault;

Stack(int n)
{
frameSize = n;
array=new int[frameSize];
hit=0;
fault=0;
top=0;
for(int i=0;i<frameSize;i++)
array[i]=-1;
}
void print()
{
for(int i:array)
System.out.print(i+" ");
System.out.println();
}
boolean isHit(int n)
{
for(int i:array)
if(i==n)
{
hit++;
return true;
}
return false;
}
void insert(int n)
{
print();
if(!isHit(n))
{
array[top]=n;
top=(top+1)%frameSize;
fault++;
}
}
int getHit()
{
return hit;
}
int getFault()
{
return fault;
}
}
class FIFO
{
public static void main(String args[])
{
Stack s=new Stack(3);
int n=20;
int inp[]={7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1};
for(int i=0;i<n;i++)
s.insert(inp[i]);
System.out.println("HIT : "+s.getHit()+" FAULT : "+s.getFault());
}
}

Output:

-1 -1 -1
7 -1 -1
7 0 -1
7 0 1
2 0 1
2 0 1
2 3 1
2 3 0
4 3 0
4 2 0
4 2 3
0 2 3
0 2 3
0 2 3
0 1 3
0 1 2
0 1 2
0 1 2
7 1 2
7 0 2
HIT : 5 FAULT : 15

fcfs.java (OS)

import java.util.Scanner;

class fcfs
{
public static void main(String ... args)
{
int n,curTime=0,sumTAT=0,sumWT=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of process : ");
n = sc.nextInt();
int AT[]=new int[n];
int BT[]=new int[n];
int WT[]=new int[n];
int TAT[]=new int[n];
for(int i=0; i<n; i++)
{
System.out.println("Enter AT of #"+(i+1)+"No.: ");
AT[i]=sc.nextInt();
System.out.println("Enter BT of #"+(i+1)+"No.: ");
BT[i]=sc.nextInt();
}
for(int i=0; i<n; i++)
{
WT[i]=curTime-AT[i];
curTime+=BT[i];
TAT[i]=curTime-AT[i];
sumWT+=WT[i];
sumTAT+=TAT[i];
}
System.out.println("AT\tBT\tWT\tTAT\n-------------------------------");
for(int i=0; i<n; i++)
System.out.println(AT[i]+"\t"+BT[i]+"\t"+WT[i]+"\t"+TAT[i]);
System.out.println("AvgWT = " + ((float)  sumWT/n) +"\nAvgTAT = " +  ((float) sumTAT/n));
}
}

Output:

E:\os write ups>javac fcfs.java

E:\os write ups>java fcfs

Enter number of process :
5
Enter AT of #1No.:
5
Enter BT of #1No.:
6
Enter AT of #2No.:
2
Enter BT of #2No.:
1
Enter AT of #3No.:
7
Enter BT of #3No.:
8
Enter AT of #4No.:
5
Enter BT of #4No.:
4
Enter AT of #5No.:
6
Enter BT of #5No.:
2
AT      BT      WT      TAT
-------------------------------
5       6       -5      1
2       1       4       5
7       8       0       8
5       4       10      14
6       2       13      15
AvgWT = 4.4
AvgTAT = 8.6