Monday, 25 December 2017

This example create a menubar and toolbar both populated with Action objects.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class ActionExample extends JPanel {

  public JMenuBar menuBar;
  public JToolBar toolBar;

  public ActionExample() {
    super(true);

    // Create a menu bar and give it a bevel border.
    menuBar = new JMenuBar();
    menuBar.setBorder(new BevelBorder(BevelBorder.RAISED));

    // Create a menu and add it to the menu bar.
    JMenu menu = new JMenu("Menu");
    menuBar.add(menu);

    // Create a toolbar and give it an etched border.
    toolBar = new JToolBar();
    toolBar.setBorder(new EtchedBorder());

    // Instantiate a sample action with the NAME property of
    // "Download" and the appropriate SMALL_ICON property.
    SampleAction exampleAction = new SampleAction("Download",
                                     new ImageIcon("action.gif"));

    // Finally, add the sample action to the menu and the toolbar.
    // These methods are no longer preferred:
    // menu.add(exampleAction);
    // toolBar.add(exampleAction);
    // Instead, you should create actual menu items and buttons:
    JMenuItem exampleItem = new JMenuItem(exampleAction);
    JButton exampleButton = new JButton(exampleAction);
    menu.add(exampleItem);
    toolBar.add(exampleButton);
  }

  class SampleAction extends AbstractAction {
    // This is our sample action. It must have an actionPerformed() method,
    // which is called when the action should be invoked.
    public SampleAction(String text, Icon icon) {
      super(text,icon);
    }

    public void actionPerformed(ActionEvent e) {
      System.out.println("Action [" + e.getActionCommand() + "] performed!");
    }
  }

  public static void main(String s[]) {
    ActionExample example = new ActionExample();
    JFrame frame = new JFrame("Action Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setJMenuBar(example.menuBar);
    frame.getContentPane().add(example.toolBar, BorderLayout.NORTH);
    frame.setSize(200,200);
    frame.setVisible(true);
  }
}

A simple Action that copies text from a PageFrame object.

import java.awt.event.ActionEvent;
import javax.swing.*;

public class CutAction extends AbstractAction {
  SiteManager manager;

  public CutAction(SiteManager sm) {
    super("", new ImageIcon("cut.gif"));
    manager = sm;
  }

  public void actionPerformed(ActionEvent ae) {
    JInternalFrame currentFrame = manager.getCurrentFrame();
    if (currentFrame == null) { return; }
    // cannot cut or paste sites
    if (currentFrame instanceof SiteFrame) { return; }
    ((PageFrame)currentFrame).cutText();
  }
}

A simple Action that copies text from a PageFrame object.

import java.awt.event.ActionEvent;
import javax.swing.*;

public class CopyAction extends AbstractAction {
  SiteManager manager;

  public CopyAction(SiteManager sm) {
    super("", new ImageIcon("copy.gif"));
    manager = sm;
  }

  public void actionPerformed(ActionEvent ae) {
    JInternalFrame currentFrame = manager.getCurrentFrame();
    if (currentFrame == null) { return; }
    // can't cut or paste sites
    if (currentFrame instanceof SiteFrame) { return; }
    ((PageFrame)currentFrame).copyText();
  }
}

A simple event handler to display cut/copy/paste events.

import java.awt.event.*;

public class CCPHandler implements ActionListener {

  public final static String CUT   = "cut";
  public final static String COPY  = "copy";
  public final static String PASTE = "paste";

  public void actionPerformed(ActionEvent e) {
    String command = e.getActionCommand();
    if (command == CUT) { // We can do this since we're comparing constants.
      System.out.println("Got Cut event");
    }
    else if (command == COPY) {
      System.out.println("Got Copy event");
    }
    else if (command == PASTE) {
      System.out.println("Got Paste event");
    }
  }
}

Sunday, 5 November 2017

Network Time Protocol (NTP)


Java program to shutdown windows7

import java.io.*;
class Main
{
public static void main(string[args]) throws IOExcption
{
char ch ;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
string s;
System.out.println ("Do you want to shutdown your computer now (y/n) \n ") ;
s=br.readLine( ch ) ;
if ( ch == 'y' || ch == 'Y' )
system( "C: \\ WINDOWS\\ System32 \\ shutdown /s" ) ;
system( "shutdown" );
return 0 ;
}
}

Java Program to find class & Subnet mask based on IP address (CN)

import java.util.*;
class FindClass{
public static void main(String args[]){
System.out.println("Enter the IP address for recongization of the class");
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
String s1[]=s.split("\\.");
int a[]=new int[s1.length];
for(int i=0;i<s1.length;i++)
a[i]=Integer.parseInt(s1[i]);

int first=a[0];
String mask="";

if(s1.length==4 && first!=0){
if(first<=127){
System.out.println("Class A");
mask = "255.0.0.0";
System.out.println("The Subnet mask is: "+mask);
}
else if(first>=128 && first <=191){
System.out.println("Class B");
mask = "255.255.0.0";
System.out.println("The Subnet mask is: "+mask);
}
else if(first>=192 && first <=223){
System.out.println("Class C");
mask = "255.255.255.0";
System.out.println("The Subnet mask is: "+mask);
}
else if(first>=224 && first <=239){
System.out.println("Class D");
mask = "255.0.0.0";
System.out.println("The Subnet mask is: "+mask);
}
else if(first>=240 && first <=254){
System.out.println("Class E");
mask = "255.0.0.0";
System.out.println("The Subnet mask is: "+mask);
}
}
else
System.out.println("Invalid");
}

}

Output:

Enter the IP address for recongization of the class
192.168.0.1
Class C
The Subnet mask is: 255.255.255.0

Enter the IP address for recongization of the class
127.0.0.1
Class A
The Subnet mask is: 255.0.0.0

Sunday, 29 October 2017

Implementation of data hazards in 8086 using java program (MP)

import java.util.*;
class datahazard
{
static void accept()
{
Scanner scr=new Scanner(System.in);
System.out.print("\nFirst Instruction:\nOpcode ");
s[0][0]=scr.nextLine();
System.out.print("\nDomain: ");
s[0][2]=scr.nextLine();
System.out.print("\nRange: ");
s[0][1]=scr.nextLine();
System.out.print("\nSecond Instruction:\nOpcode ");
s[1][0]=scr.nextLine();
System.out.print("\nDomain: ");
s[1][2]=scr.nextLine();
System.out.print("\nRange: ");
s[1][1]=scr.nextLine();
}
static int check()
{
if(s[0][1].equals(s[1][2]))
return 1;
else
if(s[0][2].equals(s[1][1]))
return 2;
else
if(s[0][1].equals(s[1][1]))
return 3;
else
return 0;
}
static String s[][]=new String[2][3];
public static void main(String args[])
{
Scanner scr=new Scanner(System.in);
int ch=0;
do
{
int c=0;
accept();

System.out.println(s[0][0]+" "+s[0][1]+" "+s[0][2]);
System.out.println(s[1][0]+" "+s[1][1]+" "+s[1][2]);
c=check();
if(c==1)
System.out.println("RAW hazard exists");
else
if(c==2)
System.out.println("WAR hazard exists");
else
if(c==3)
System.out.println("WAW hazard exists");
else
System.out.println("There are No Data Dependency Hazards");
System.out.println("Do you wish to continue?(1/0)");
ch=scr.nextInt();
}
while(ch==1);
}
}

Output:

D:\Extras>javac datahazard.java

D:\Extras>java datahazard

First Instruction:
Opcode mul

Domain: r2

Range: r1

Second Instruction:
Opcode add

Domain: r4

Range: r1
mul r1 r2
add r1 r4
WAW hazard exists
Do you wish to continue?(1/0)
1

First Instruction:
Opcode mov

Domain: r2

Range: m2

Second Instruction:
Opcode mov

Domain: r1

Range: m1
mov m2 r2
mov m1 r1
There are No Data Dependency Hazards
Do you wish to continue?(1/0)
0

D:\Extras>

Implementation of BIOS interrupt in 8086 using ALP (MP)

data segment
CR equ 0dh          ;start of the cursor
LF equ 0ah           ;cursor will go to next line
lgn_msg db CR,LF,'Enter a Number between 0-9',CR,LF,'$'
data ends
code segment
assume cs:code, ds:data
start:
mov ax,data
mov ds,ax
mov dx,offset lgn_msg
mov ah,09h          ;for displaying message on screen
int 21h
mov ah,01h          ;for accepting user input from keyboard
int 21h
cmp al,39h           ;to check whether number in range of 0 to 9
ja exit
sub al,30h            ;to convert hexadecimal to decimal
mov ah,00h
mov cx,ax
mov ax,0001h
mov bx,0001h
up: mul bx
inc bx
dec cx
jnz up
exit:
int 03h
code ends
end start

TRANSFER DATA FROM DATA SEGMENT TO EXTRA SEGMENT USING ALP 2ND METHOD (MP)

data segment
arr1 dw 1111h,2222h,3333h,4444h
data ends

extra segment
arr2 dw 05 dup(?)
extra ends
  
code segment
assume cs:code, ds:data, es:extra
start:
mov ax,data
mov ds,ax

mov ax,extra
mov es,ax

lea si,arr1
lea di,arr2
mov cx,04

up:
lodsw
stosw
loop up

int 03h
code ends

end start

TRANSFER DATA FROM DATA SEGMENT TO EXTRA SEGMENT USING ALP (MP)

data segment
arr1 dw 1111h,2222h,3333h,4444h
data ends

extra segment
arr2 dw 05 dup(?)
extra ends

code segment
assume cs:code,ds:data,es:extra
start:
mov ax,data
mov ds,ax
mov ax,extra
mov es,ax
lea si,arr1
lea di,arr2
mov cx,04

up:
mov ax,[si]
mov es:[di],ax
inc si
inc si
inc di
inc di
loop up

int 03h
code ends

end start

Saturday, 28 October 2017

Hospital Management System - Project Proposal

Hospital Management System - Project Proposal Complete Document

Goto: Scribd


Hospital Management System - System Requirement Specification (SRS)

Hospital Management System - System Requirement Specification (SRS) Complete Document

Goto: Scribd


Student Management System - System Requirement Specification (SRS)

Student Management System - System Requirement Specification (SRS) Complete Document
Goto: Scribd


sstf.java (OS)

import java.util.Scanner;

public class sstf
{
public static void main(String[] args)
{
int b[]=new int[20];
int a[]=new int[20],d,n,i,j,temp,s,k=0,x=0,t=0;
Scanner sc= new Scanner(System.in);
System.out.println("Enter head pointer position:");
a[0]= sc.nextInt();
System.out.println("\nEnter number of processes:");
n= sc.nextInt();
System.out.println("\nEnter processes in request order");
for(i=1;i<=n;i++)
{
a[i]=sc.nextInt();
}
b[k++]=a[0];
for(i=0;i<n;i++)
{
s=1000;
for(j=i+1;j<=n;j++)
{
if(a[i]>a[j])
d=a[i]-a[j];
else
d=a[j]-a[i];
if(d<s)
{
s=d;
x=j;
}
}
t+=s;
temp=a[i+1];
a[i+1]=a[x];
a[x]=temp;
b[k++]=a[i+1];
}
System.out.println("\nProcessing order:");
for(i=1;i<=n;i++)
System.out.print("\t"+b[i]);
System.out.println("\nTotal Head Movement:"+t);
}
}

Output:

Enter head pointer position:
50

Enter number of processes:
5

Enter processes in request order
75
82
61
10
26

Processing order:
        61      75      82      26      10
Total Head Movement:104

Saturday, 2 September 2017

Difference between Abstraction and Encapsulation in Java - OOP



Difference between Abstraction vs Encapsulation:


1) The most important difference between Abstraction and Encapsulation is that Abstraction solves the problem at design level while Encapsulation solves it implementation level.

2) Abstraction is about hiding unwanted details while giving out most essential details, while Encapsulation means hiding the code and data into a single unit e.g. class or method to protect inner working of an object from outside world. In other words, Abstraction means extracting common details or generalizing things.

3) Abstraction lets you focus on what the object does instead of how it does, while Encapsulation means hiding the internal details of how an object works. When you keep internal working details private, you can change it later with a better method. 

4) Abstraction focus on outer lookout e.g. moving of vehicle while Encapsulation focuses on internal working or inner lookout e.g. how exactly the vehicle moves.

5) In Java, Abstraction is supported using interface and abstract class while Encapsulation is supported using access modifiers e.g. publicprivate and protected.

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");
        }
    }
}