Thursday, October 31, 2013

FACADE DESIGN PATTERN IN JAVA

/*
FACADE DESIGN PATTERN IN JAVA
*/
class Client
{
public static void main(String args[]){

OrderFacade orderFacade = new OrderFacade();

orderFacade.placeOrder("OR123456");

System.out.println("Order processing completed");
}

}

 class OrderFacade {
private Payment pymt = new Payment();
private Inventory inventry = new Inventory();

public void placeOrder(String orderId) {

String step1 = inventry.checkInventory(orderId);

String step2 = pymt.deductPayment(orderId);

System.out.println("Following steps completed:" + step1+ " & " + step2);

}

}
 class Payment {
public String deductPayment(String orderID)
{
System.out.println("TOKEN "+orderID+" recieved\nProcessing payment...\n");
return "Payment deducted successfully";

}
}

class Inventory {
public String checkInventory(String OrderId)
{
System.out.println("TOKEN "+OrderId+" recieved\nProcessing Inventory...\n");
return "Inventory checked";
}
}






/*
OUTPUT:
TOKEN OR123456 recieved
Processing Inventory...

TOKEN OR123456 recieved
Processing payment...

Following steps completed:Inventory checked & Payment deducted successfully
Order processing completed
*/

OBSERVER DESIGN PATTERN IN JAVA

/*
OBSERVER DESIGN PATTERN IN JAVA
*/
import java.util.*;
class subject
{

person p1,p2;

subject()
{
p1=new person();
p2=new person();
}
void notify(boolean a)
{
if(a)
{
p2.state=p1.state="changed";

System.out.println(p1);
System.out.println(p2);
}
else
{
System.out.println(p1);
System.out.println(p2);
}

}
public static void main(String[] args)
{
boolean executed=false;
String choice;
subject s=new subject();
Scanner sc=new Scanner(System.in);
while(!executed)
{
System.out.println("SHOULD WE CHANGE STATE ?  Y/N");
choice=sc.nextLine();
if(choice.equals("Y") || choice.equals("y"))
{
s.notify(true);
executed=true;
}
else
s.notify(false);
}
}
}
class person
{//person is the observer;
String state;
person()
{
state="unchanged";
}
public String toString()
{
return state;
}

}

/*
OUTPUT
SHOULD WE CHANGE STATE ?  Y/N
n
unchanged
unchanged
SHOULD WE CHANGE STATE ?  Y/N
n
unchanged
unchanged
SHOULD WE CHANGE STATE ?  Y/N
n
unchanged
unchanged
SHOULD WE CHANGE STATE ?  Y/N
n
unchanged
unchanged
SHOULD WE CHANGE STATE ?  Y/N
y
changed
changed
*/

Wednesday, October 30, 2013

HOW TO RUN 8086 ON WINDOWS

TOPIC : HOW TO RUN 8086 ON WINDOWS

REQUIREMENTS : TASM ,Debugger to assemble link and create object and exe files for your program as well as to debug it. 
here is download link: 
http://dl.dropboxusercontent.com/u/25051673/tasm5.zip
http://dl.dropboxusercontent.com/u/25051673/afdebug.zip

STEPS:
1.Extract these files in this folder
and now open the command prompt and change the directory to this folder.


2.copy the program and paste it in the notepad/Editplus.
save it in the bin folder with extension .asm


3.Now type tasm followed by the program name with .asm extension

tasm filename.asm

press enter this will show your errors. correct all these errors then proceed to the next step.


4.now type tlink followed by the program name with extension .obj


tlink filename.obj

press enter


5.now type the td followed by the program name with extension .exe


td filename.exe

6.then your program appears in different screen.
press F7 key until you reach the starting address of the program.  


To open the dump window press ctrl+g


6.then a dialog box will appear. enter the starting address of the program which is typed in the program. for ex 2000h.
then press enter. and close the dialog box.


8.now continue pressing F7. when the program terminates it shows a dialog box. and the result will be seemed in the dump window

VIDEO LINK: http://www.youtube.com/watch?v=oUmCi2He84o

16 BIT ADDITION WITH CARRY


;16 BIT ADDITION WITH CARRY

.model small
.data
num1 dw 0FFFFH
num2 dw 0DDDDH
sum dw ?
carry db ?
.code
START:
MOV AX,@data
MOV DS,AX
MOV AX,0000H
MOV AX,num1
MOV BX,num2
ADD AX,BX
MOV DL,00H
ADC DL,00H
MOV sum,AX
MOV carry,DL
MOV AX,4C00H
INT 21H
END START

;da:0000 CD 21 FF FF(num1) DD DD(num2) DC DD(sum)
;01(carry)

8-bit addition register adddressing mode with carry


;Aim - 8-bit addition register adddressing mode with carry

.8086
.model small
.data
num1 db 05h
num2 db 0eh
sum db ?
carry db ?
.code

start:
mov ax , @data
mov ds , ax
mov ax , 0000h
mov al, num1
mov bl , num2
add al , bl
mov dl , 00
adc dl , 00
mov carry , dl
mov ax , 4c00h
int 21h
end start

;Result
;DS:0001 05(NUM1)
;DS:0002 0E(NUM2)
;0005 01
;SUM 23
;CARRY 00

Aim - 8-bit addition register adddressing mode without carry

;Aim - 8-bit addition register adddressing mode without carry

.8086
.model small
.code

start:
mov ax , 1200h
mov ds , ax
mov ax , 0000h
mov al , 05h
mov bl , 04h
add al , bl
mov ax , 4c00h
int 21h
end start
; Result
;0000+0012=0012

16-bit Subtraction register adddressing mode with carry

; Aim - 16-bit Subtraction register adddressing mode with carry

 .8086
 .model small
 .data
 num1 dw 0aaaah      ;Load aaaah to Num1
 num2 db 0ffffh                       ;Load ffffh to Num2
 ans dw ?
 carry db ?
 .code
 Start:
 mov ax , @data                       ;Copying the address of the data to AX.
 mov ds , ax
 mov ax , 0000h
 mov ax , num1                        ;Copies Num1 to AX.
 mov bx , num2
 sub cx , dx                          ;Subtracts DX from CX.

 mov ah , 00h
 mov ans,cx

 lahf                                 ;Loads all the flags to AH.
 AND ah,01h

 mov borrow , ah
 mov ax , 4c00h
 int 21h
 end start

 ;Result
 ;DS:0000 0AAAA(NUM1)
 ;DS:0000 0FFFF(NUM2)
 ;ds:00008 0AAAB(ans)
 ;ds:00008 01 (borrow)

8-bit Subtraction register adddressing mode with borrow

;8-bit Subtraction register adddressing mode with borrow

 .8086
 .model small
 .data
 num1 db 0ah
 num2 db 0Fh
 ans db ?
 carry db ?
 .code
 Start:
 mov ax , @data                 ; Copying the address of the data to AX.
 mov ds , ax
 mov ax , 0000h
 mov al , num1                  ; Copies Num1 to AL.
 mov bl , num2
 sub al , bl                    ;Subtracts BL fron AL.

 mov ah , 00h
 mov ans,al

 lahf                           ;Loads all the flags to AH.
 AND ah,01h

 mov carry , ah
 mov ax , 4c00h
 int 21h
 end start

 ;Result
 ;DS:0000 0A(NUM1)
 ;DS:0000 0F(NUM2)
 ;ds:00008 0FA(ans)
 ;ds:00008 01 (ans)

To move a block of 10 databytes from source to destination

; To move a block of 10 databytes from source to destination
.8086
.model small
.data
src1 db 10h, 20h ,30h, 40h, 50h, 60h, 70h, 80h, 90h, 0ah
Des1 db 0ah dup(0)
.code
START:
mov ax , @data
mov ds , ax
mov es , ax
lea si , src1
lea di , des1
mov cx , 000ah
rep movsb
mov ax,4c00h
int 21h
end start
;output
(000a)

To calculate the power of a number

;Aim:- To calculate the power

.8086
.model small
.data
msg1 db 13,10, "enter the number-:$"
msg2 db 13,10, "enter the power-:$"
msg3 db 13,10, "the answer-:$"
.code
START:
MOV AX,@data
MOV DS,AX
LEA DX,msg1 ;loads the offset of MSG1 in dx
MOV AH,09H
INT 21H
MOV AH,01H
INT 21H
MOV BL,AL

LEA DX,msg2 ;loads the offset of MSG2 in dx
MOV AH,09H ;Display s string
INT 21H ;Whose offset is in dx
MOV AH,01H
INT 21H
MOV CL,AL
SUB CL,30H ;Subtract bl from ASCII 30H
SUB BL,30H
MOV AH,00H
MOV AL,01H ;Take input from keyboard


BACK:MUL BL
DEC CL
JNZ BACK
MOV BL,AL

LEA DX,msg3 ;loads the offset of MSG3 in dx
MOV AH,09H
INT 21H
MOV DL,BL
ADD DL,30H
MOV AH,02H
INT 21H
MOV AX,4C00H
INT 21H
END START

;Output
;Enter the number:3
;Enter the power:2
;the answer is-:9

to find no of ones and zeroz

;title:to find no of ones and zeroz
.model small
.data
a db 05H
num db ? ;stores input no
.code
start:
mov ax,@data
mov ds,ax
mov dx,0000H
mov cx,0000H
mov bx,08H ;storing 8 in bx counter
mov al,num
back:ror ax,1 ;rotate al by 1
jnc abc ;jump if carry
inc dx ;increment dx
abc:inc cx
dec bx
jnz back ;jump if zero
mov ax,4c00h
int 21h
end start

;output
;input nos 0000 0101
no of 0s:Cx:6
no of 1s:Dx:2

16-bit multiplication direct addressing mode

;16-bit multiplication direct addressing mode

.model small
.data
num1 dw 1234h
num2 dw 0100h
product dw ?
.code
start:
mov ax,@data
mov ds,ax
mov ax,num1
mov bx,num2
mul bx
;multiplies data in bx with ax
;stores msb in dx and lsb in ax
mov product,ax
mov ax,4c00h
int 21h
end start

;Output
;AX=1234
;BX=0100
;AFTER MULTILICATION
;AX=3400

to study Interrupts

;to study Interrupts
.8086
.model small
.data
msg1 db 13,10 "Enter no. to be displayed.$"
msg2 db 13,10 "display enteries.$"

.code
Start:
Mov ax,@data
mov ds,ax

back:lea msg1,dx
mov Ah,09h
INT 21

MOv Ah,01h
INT 21h

cmp al,30h
jz lebel1

lea msg2,dx
Mov Ah,02h
int 21h
mov dl,al

lebel1:mov ax,4C00h
int 21H
jmp back

End start



;Result
;ds:0008: 10h,20h,30h,40h,50h,60h,70h,80h,90h,0ah(src) 10h,20h,30h,40h,50h,60h,70h,80h,90h,0ah des

8 bit Multiplication of signed bit

;Aim:- 8 bit Multiplication of signed bit
 .8086
 .model small
 .data
 num1 db 0FFh
 num2 db 0FEh
 product db ?
 .code
 Start:
 Mov ax,@data
 mov ds,ax
 mov ax,0000h
 mov al,num1
 mov bl,num2
 imul cl                      ;Multiplies Bl wit Al and the result in Cl Retaining the second bit
 mov product,al               ; Copies Al to product variable
 mov ax,4C00h
 int 21H
 End start
 ;result
 ;DS :0000 1000h(num1) 0ffffh(num2)
 ;DS:0008 0fffh(sum) 01 carry

To input the number and display untill 0 is passed

;Aim:- To input the number and display untill 0 is passed

.8086
.model small
.data
msg1 db 13,10, "enter a character to be displayed:$"
msg2 db 13,10, "displaying entered character:$"
.code
START:
MOV AX,@data
MOV DS,AX
Again:LEA DX,msg1 ;loads the offset of MSG1 in dx
MOV AH,09H ;Display s string
INT 21H ;Whose offset is in dx

MOV AH,01H
INT 21H

CMP AL,30H
jz label1
MOV BL,AL


LEA DX,msg2 ;loads the offset of MSG2 in dx
MOV AH,09H
INT 21H
MOV DL,BL
MOV AH,02H
INT 21H

jmp Again
label1: MOV AX,4C00H
INT 21H
END START

;Output
enter a character to be displayed: d
displaying entered character: d

To count number of zeros and ones

;To count number of zeros and ones
.8086
.model small
.data
num db 05h ;Storing 05h number as our original number
.code
start:
mov ax,@data
mov ds,ax
mov dx,0000h
mov cx,0000h
mov bx,08h ;Storing 08 in bx as a counter
mov al,num
BACK: ROR Al,1 ;Rotate al by 1
JNC TG ;Jump if no carry
INC dx ;Increment counter dx
TG: INC cx
DEC bx
JNZ BACK
mov ax,4c00h
INT 21H
END start

;Output
;Num:05H
;cx:06h
;dx:02h

To find average of 5 numbers

;title:To find average of  5 numbers
;no:6483
.model small
.data
src1 db 02H,04H,06H,02H,02H
sum db 00H
avg db ?
.code
start:
mov ax,@data
mov ds,ax
mov cx,05H
lea si,src1
mov dl,05H
back: mov al,[si]
add sum,al
inc si
dec cx
jnz back
div al
mov ax,4c00h
int 21h
end start
; output
;ax=6

To implement SEMAPHORE in C

 /*
To implement SEMAPHORE in C

*/
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<stdio.h>

int main()
{
        int semid,pid,val,i,withdraw;
        struct sembuf sop;
        FILE *fp;
        int balance;
        semid=semget(0x20,1,IPC_CREAT|0666);
        semctl(semid,0,SETVAL,1);
        sop.sem_num=0;
        sop.sem_op=-1;
        sop.sem_flg=SEM_UNDO;
        pid=fork();
        if(pid==0)
        {
                printf("Child Before Sem Operation\n");
                semop(semid,&sop,1);
                printf("Child is in Critical Section\n");
                fp = fopen("data.txt","r+");
                fscanf(fp,"%d",&balance);
                printf("Enter the Amount to be Withdrawn: ");
                scanf(" %d",&withdraw);
                rewind(fp);
                balance = balance - withdraw;
                fprintf(fp,"%d",balance);
                fclose(fp);
                sleep(2);
                printf("\nBalance = %d\n",balance);
                printf("Child Outside Critical Section\n");
        }
        else
        {
                printf("Parent Before Sem Operation\n");
                semop(semid,&sop,1);
                printf("Parent is in Critical Section\n");
                sleep(2);
                fp = fopen("data.txt","r+");
                fscanf(fp,"%d",&balance);
                printf("\nBalance = %d\n",balance);
                printf("Enter the Amount to be Withdrawn: ");
                scanf(" %d",&withdraw);
                rewind(fp);
                balance = balance - withdraw;
                fprintf(fp,"%d",balance);
                fclose(fp);
                sleep(2);
                printf("\nBalance = %d\n",balance);
                printf("Parent Outside Critical Section\n");
        }
        return 0;
}

/* Output:
Child Before Sem Operation
Child is in Critical Section
Parent Before Sem Operation
Enter the Amount to be Withdrawn: 10

Balance = 850
Child Outside Critical Section
Parent is in Critical Section

Balance = 850
Enter the Amount to be Withdrawn: 50

Balance = 800
Parent Outside Critical Section
*/

To Implement Inter Process Communication using Pipes

/*
To Implement Inter Process Communication using Pipes
*/

#include<stdio.h>
#include<unistd.h>
#include<string.h>

int main()
{
        int fd[2],n,status;
        char buff[100];
        pipe(fd);
        if(pipe (fd)<0)
        {
                printf("Cannot create a pipe.....");
                exit(0);
        }

        else
        {
                switch(fork())
                {
                        case -1:
                                printf("\nFork error...");
                                exit(0);
 
                        case 0:
                                //child process read data from buffer

                                printf("\nRunning child process...");
                                printf("\nBefore read is performed...");
                                read(fd[0],buff,100);
                                printf("\nThe data read from the buffer is:\t %s",buff);
                                break;

                        default:
                                //parent process writes to buffer

                                printf("\nRunning parent process...\n");
                                printf("\nWriting...\n");
                                write(fd[1],"Welcome to HOME....",60);
                                wait(&status);
                                printf("\nWriting is over...\n");
                                break;
                }
        }
    return(0);
}



/*
Output:
Running child process...

Running parent process...

Writing...
Before read is performed...
The data read from the buffer is:        Welcome to HOME....
Writing is over...
*/

IMPLEMENTATATION OF INODE USING C.

/*
TITLE:IMPLEMENTATATION OF INODE USING C.

*/

#include<sys/types.h>
#include<ustat.h>
#include<stdio.h>
#include<sys/dir.h>

main(argc,argv)
int argc;
char *argv[];
{
  FILE *fp;
  struct direct dir;
  fp=fopen(argv[1],"r");
  printf("\n Directory listing of %s\n",argv[1]);
  while(fread(&dir,sizeof(dir),1,fp)!=EOF)
  {
     if(dir.d_ino==0)
         continue;
          printf("inode number:%d of file:%s\n",dir.d_ino,dir.d_name);
  }
   fclose(fp);
   exit(0);
}

To Study the File-Handling

/*
To Study the File-Handling
*/


#include<stdio.h>
#include<fcntl.h>
#include<error.h>
#include<string.h>
#include<sys/wait.h>

int fd;
char name1[100];
void createfile(char *name)
{
printf("\n Enter name of file \n");
scanf("%s",name);
fd=open(name,O_CREAT|O_EXCL,0760);
if(fd==-1)
{
perror("\n ERROR!!!\n");
exit(1);
}

else
{
printf("\nfile created");
close(fd);
}
}


void copy()
{
char ch;
int n;
char name1[100], name2[100];
int fd1, fd2;
printf("\n Enter name of file to copy");
scanf("%s",name1);
fd1=open(name1,O_RDWR|O_APPEND,0760);
printf("\nEnter name of file to be copied into");
scanf("%s",name2);
fd2=open(name2,O_CREAT|O_EXCL|O_RDWR|O_APPEND,0761);
if(fd1==-1)
{
perror("\n ERROR!!!\n");
exit(1);
}
if(fd2==-1)
{
perror("\n ERROR!!!\n");
exit(1);
}


while(read(fd1,&ch,1)>0);
{
write(fd2,&ch,1);
}


printf("\n copied");
close(fd1);
close(fd2);
}
void writeandread()
{
char ch;
int n;
char name2[100], buff[100], buff1[100];
int fd1;
printf("\n Enter name of file to write");
scanf("%s",name1);
fd1=open(name1,O_RDWR|O_APPEND,0760);
/*printf("\nEnter name of file");
scanf("%s",name2);
fd2=open(name2,O_CREAT|O_EXCL|O_RDWR|O_APPEND,0761);
*/
if(fd1==-1)
{
perror("\n ERROR!!!\n");
exit(1);
}

else
{
printf("\nEnter to write");
scanf("%s",buff);
write(fd1,buff,strlen(buff));
printf("\nwrite succesfull");
lseek(fd1,0,SEEK_SET);
read(fd1,buff1,strlen(buff));
printf("\nfile contains: %s\n",buff1);
}
printf("\nexiting");
close(fd1);

}

int main()

{
char name[100];
createfile(name);
        writeandread();
copy();
return 0;
}

/*
OUTPUT:
bash-3.00$ cc file.c
bash-3.00$ ./a.out

Enter name of file a1

file created
 Enter name of file to write a1

Enter to write hi

write succesfull
file contains: hij

exiting
 Enter name of file to copy a1

Enter name of file to be copied into a2

 copiedbash-3.00$ ./a.out
*/

IMPLEMENTATATION OF PARENT CHILD PROCESS USING C.

/*

TITLE: IMPLEMENTATATION OF PARENT CHILD PROCESS USING C.
*/




#include<stdio.h>
#include<sys/wait.h>

int main()
{
      int pid,status,n1,n2,sum;
      printf("\n parent(user) program is in execution");
      pid=fork();
      printf("\n spanning the child process");
      if(pid==0)
      {
           printf("\n Child is executing :\n");
           printf("\n Enter two no.:\n");
           scanf("%d  %d",&n1,&n2);
           sum=n1+n2;
           printf("the sum is: %d\n",sum);
           printf(" pid of child is: %d\n",getpid());
           printf("pid of parent is: %d\n",getppid());
           printf("\n exiting child \n");

      }
    else
     {
       wait(&status);
              printf("\n parent is executing: \n");
           printf("\n pid of parent is :%d\n",getpid());
           printf("\n pid of parent's parent is: %d\n ",getppid());

         
           printf("\n exiting parent \n");
     }
   return 0;
}
/*
 parent(user) program is in execution
 spanning the child process
 Child is executing :

 Enter two no.:
 parent(user) program is in execution
20
14
the sum is: 34
 pid of child is: 4874
pid of parent is: 4873

 exiting child
 spanning the child process
 parent is executing:

 pid of parent is :4873

 pid of parent's parent is: 4857

 exiting parent
*/

TO IMPLEMENT SHORTEST JOB FIRST IN JAVA

/*
TOPIC : TO IMPLEMENT SHORTEST JOB FIRST IN JAVA
*/

import java.util.*;
public class SJF
{
  public static void main(String args[])throws Exception{
Scanner s=new Scanner(System.in);
int n=0;
System.out.println("Enter no of processes :");
n=s.nextInt();
Process[] p=new Process[n];
for(int i=0;i<n;i++)
{
   p[i]=new Process();
}
for(int i=0;i<n;i++)
{
  System.out.println("PROCESS ID :");
p[i].id=s.nextInt();
System.out.println("ARRIVAL TIME :");
p[i].atime=s.nextInt();
System.out.println("Burst TIME :");
p[i].btime=s.nextInt();
}
for(int i=0;i<n-1;i++)
{
   for(int j=0;j<n-1;j++)
{
   if(p[j].btime>p[j+1].btime)
{
  Process t=p[j];
   p[j]=p[j+1];
   p[j+1]=t;
}
}
}
System.out.println("Process id\tArrival time\tBurst time");
System.out.println("...............................");
for(int i=0;i<n;i++)
{
   System.out.println(""+p[i].id+"\t\t"+p[i].atime+"\t\t"+p[i].btime);
}
}
}
class Process {
  int id,btime,atime;
}
/* OUTPUT
Enter no of processes :
2
PROCESS ID :
99
ARRIVAL TIME :
20
Burst TIME :
12
PROCESS ID :
10
ARRIVAL TIME :
21
Burst TIME :
32
Process id      Arrival time    Burst time
...............................
99              20              12
10              21              32
*/

TO IMPLEMENT ROUND ROBIN TIME SLICING IN JAVA

/*
TO IMPLEMENT ROUND ROBIN TIME SLICING IN JAVA
*/

import java.util.*;
public class RR
{
public static void main(String arg[])throws Exception
{
Scanner s=new Scanner(System.in);
int n,quan,tat;
System.out.println("Enter no of processes:");
n=s.nextInt();
System.out.println("enter time quantum:");
quan=s.nextInt();
Process p[]=new Process[n];
for(int i=0;i<n;i++)
{
p[i]=new Process();
}
for(int i=0;i<n;i++)
{
System.out.println("PROCESS ID:");
p[i].id=s.nextInt();
System.out.println("ARRIVAL TIME:");
p[i].atime=s.nextInt();
System.out.println("Burst time:");
p[i].btime=s.nextInt();
}
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-1;j++)
{
if(p[j].atime>p[j+1].atime)
{
Process t=p[j];
p[j]=p[j+1];
p[j+1]=t;
}
}
}
System.out.println("Process id\tArrival time\tBurst time");
System.out.println("...................................");
tat=0;
boolean flag=true;
int pass=0;
while(pass!=2)
{
for(int i=0;i<n;i++)
{
if(flag==true)
{
p[i].rbt=p[i].btime;
p[i].rbt=p[i].rbt-quan;
}
else
{
p[i].rbt=p[i].rbt-quan;
}
if(p[i].rbt>-4)
{
if(p[i].rbt<0)
p[i].rbt=0;
System.out.println(""+p[i].id+"\t\t"+p[i].atime+"\t\t"+p[i].btime+"\t\t"+p[i].rbt);
}
}
flag=false;
pass++;
}
}
}
class Process
{
int id,btime,atime,rbt;
Process()
{
id=0;
atime=0;
btime=0;
rbt=0;
}
}

/* OUTPUT
Enter no of processes:
2
enter time quantum:
12
PROCESS ID:
32
ARRIVAL TIME:
21
Burst time:
125
PROCESS ID:
45
ARRIVAL TIME:
12
Burst time:
12
Process id      Arrival time    Burst time
...................................
45              12              12              0
32              21              125             113
32              21              125             101
*/

TO IMPLEMENT PROCESS PRIORITY IN JAVA

/*

Title   :TO IMPLEMENT PROCESS PRIORITY IN JAVA
*/

import java.util.*;
public class Priority
{
  public static void main(String args[])throws Exception{
Scanner s=new Scanner(System.in);
int n=0;
System.out.println("Enter no of processes :");
n=s.nextInt();
Process[] p=new Process[n];
for(int i=0;i<n;i++)
{
   p[i]=new Process();
}
for(int i=0;i<n;i++)
{
  System.out.println("PROCESS ID :");
p[i].id=s.nextInt();
System.out.println("ARRIVAL TIME :");
p[i].atime=s.nextInt();
System.out.println("Burst TIME :");
p[i].btime=s.nextInt();
System.out.println("Priority :");
p[i].prior=s.nextInt();
}
for(int i=0;i<n-1;i++)
{
   for(int j=0;j<n-1;j++)
{
   if(p[j].prior>p[j+1].prior)
{
  Process t=p[j];
   p[j]=p[j+1];
   p[j+1]=t;
}
}
}
System.out.println("Process id\tArrival time\tBurst time\tPriority");
System.out.println("...............................");
for(int i=0;i<n;i++)
{
   System.out.println(""+p[i].id+"\t\t"+p[i].atime+"\t\t"+p[i].btime+"\t\tp"+p[i].prior);
}
}
}
class Process {
  int id,btime,atime,prior;
}

/*OUTPUT
Enter no of processes :
2
PROCESS ID :
12
ARRIVAL TIME :
14
Burst TIME :
45
Priority :
1
PROCESS ID :
21
ARRIVAL TIME :
23
Burst TIME :
45
Priority :
2
Process id      Arrival time    Burst time      Priority
...............................
12              14              45              p1
21              23              45              p2
*/

TO IMPLEMENT MULTITHREADING AND OBSERVE TIMESLICING

/*

TO IMPLEMENT MULTITHREADING AND OBSERVE TIMESLICING
*/
import java.util.*;
class q extends Thread
{
public void run()
{
try{
for(int i=0;i<12;i++)
{
System.out.println("12"+"x"+i+"="+(i*12));
Thread.sleep(2000);
}
}catch(Exception e){}
}
}
class p extends Thread
{
public void run()
{
try{
for(int i=0;i<12;i++)
{
System.out.println("10"+"x"+i+"="+(i*10));
Thread.sleep(2000);
}
}catch(Exception e){}
}
}
class multi
{
public static void main(String args[]) throws Exception
{
p a=new p();
q b=new q();
a.start();
b.start();
//THIS PROGRAM IS CODED BY TG;
}
}
/*
OUTPUT
 javac multi.java
bash-4.1$ java multi
10x0=0
12x0=0
10x1=10
12x1=12
10x2=20
12x2=24
10x3=30
12x3=36
10x4=40
12x4=48
10x5=50
12x5=60
10x6=60
12x6=72
10x7=70
12x7=84
10x8=80
12x8=96
10x9=90
12x9=108
10x10=100
12x10=120
10x11=110
12x11=132
*/

TO IMPLEMENT LRU IN JAVA

/*
TO IMPLEMENT LRU IN JAVA

*/
import java.util.*;

class Queue
{
int q[]=new int[3];
int f=0;
int count=0;
 void insert(int d)
{

if(f!=3)
{

if(find(d) && count==2)
{System.out.print("\nHIT");f++;}
else
q[f++]=d;
}

else
{
f=0;

       if(find(d) && count==2)
{System.out.print("\nHIT");f++;}
else
q[f++]=d;
}
System.out.print("\nTop:"+f);
}

 boolean find(int d)
  {
if(count==3) count=2;
for(int i=0;i<=count;i++)
{
if(d==q[i] && count>=2)
return true;


}


return  false;
}
 void traverse()
{
        if(count==3) count=2;
for(int i=0;i<=count;i++)
{

System.out.print("\n"+q[i]);
}
if(count<3) count++;
System.out.println();
}

 }


class LRU
{
public static void main(String args[])
{
Scanner src=new Scanner(System.in);

                int a[]={7,0,1,2,0,3,0,4,2,3,0,3,2,1};
Queue q=new Queue();
for(int i=0;i<14;i++)
{
q.insert(a[i]);
q.traverse();
}
}
}


TO IMPLEMENT FIFO-First In First Out

/*
TO IMPLEMENT FIFO-First In First Out
*/
import java.util.*;

class Queue
{
int q[]=new int[3];
int f=0;
int count=0;
 void insert(int d)
{
if(f!=3)
{

if(q[f]==d && count==2)
{System.out.print("\nHIT");}
else
q[f++]=d;
}

else
{
f=0;

       if(q[f]==d && count==2)
{System.out.print("\nHIT");}
else
q[f++]=d;
}
}


 void traverse()
{
for(int i=0;i<=count;i++)
{

System.out.print("\n"+q[i]);
}
if(count<2) count++;
System.out.println();
}

 }


class FIFO
{
public static void main(String args[])
{
Scanner src=new Scanner(System.in);

                int a[]={7,0,1,2,0,3,0,4,2,3,0,3,2,1};
Queue q=new Queue();
for(int i=0;i<14;i++)
{
q.insert(a[i]);
q.traverse();
}
}
}

/*OUTPUT

bash-3.00$ java FIFO

7

7
0

7
0
1

2
0
1

HIT
2
0
1

2
3
1

2
3
0

4
3
0

4
2
0

4
2
3

0
2
3

0
3
3

0
3
2

1
3
2

*/

TO IMPLEMENT First Come First Service

/*
TO IMPLEMENT First Come First Service

*/
import java.util.*;
public class FCFS
{
  public static void main(String args[])throws Exception{
Scanner s=new Scanner(System.in);
int n=0;
System.out.print("Enter no of processes :");
n=s.nextInt();
Process[] p=new Process[n];
for(int i=0;i<n;i++)
{
   p[i]=new Process();
}
for(int i=0;i<n;i++)
{
  System.out.print("PROCESS ID :");
p[i].id=s.nextInt();
System.out.println("ARRIVAL TIME :"+i);
p[i].atime=i;
System.out.print("Burst TIME :");
p[i].btime=s.nextInt();
}
/*for(int i=0;i<n-1;i++)
{
   for(int j=0;j<n-1;j++)
{
   if(p[j].atime>p[j+1].atime)
{
  Process t=p[j];
   p[j]=p[j+1];
   p[j+1]=t;
}
}
}
System.out.println("Process id\tArrival time\tBurst time");
System.out.println("...............................");
for(int i=0;i<n;i++)
{
   System.out.println(""+p[i].id+"\t\t"+p[i].atime+"\t\t"+p[i].btime);
}
*/
System.out.print("\nStarting to execute process");
for(int i=0;i<n;i++)
{
System.out.print("\nProcess "+p[i].id+" under execution");
Thread.sleep((p[i].btime)*1000);
System.out.println("\nProcess "+p[i].id+" executed");
}

}
}
class Process {
  int id,btime,atime;
}
/*OUTPUT  
bash-3.00$ java FCFS
bash-3.00$ java FCFS
Enter no of processes :3
PROCESS ID :1
ARRIVAL TIME :0
Burst TIME :7
PROCESS ID :2
ARRIVAL TIME :1
Burst TIME :1
PROCESS ID :3
ARRIVAL TIME :2
Burst TIME :3

Starting to execute process
Process 1 under execution
Process 1 executed

Process 2 under execution
Process 2 executed

Process 3 under execution
Process 3 executed

*/

SINGLETON PATTERN IN JAVA

/*
TEJAS J GHALSASI
TEIT
SINGLETON PATTERN IN JAVA
*/
import java.util.*;
public class NoticeBoard {
   
    private static NoticeBoard a;
    String message;
    /**
     * Create private constructor
     */
    private NoticeBoard(){
       
    }
    /**
     * Create a static method to get instance.
     */
    public static NoticeBoard getInstance(){
        if(a == null){
            a = new NoticeBoard();
        }
        return a;
    }
   
    public void getMessage(String msg){
        message=msg;
        System.out.println("message updated \n");
    }
   
    public void postMessage(){
       
        System.out.println("Current message is: "+message);
    }
   
    public static void main(String a[]){
    Scanner sc=new Scanner(System.in);
    NoticeBoard nb = NoticeBoard.getInstance();
        NoticeBoard nb1 = NoticeBoard.getInstance();
        System.out.println("enter message");
        String msg=sc.nextLine();
       
nb.getMessage(msg);
System.out.println("message stored in nb");
nb.postMessage();

System.out.println("message stored in nb1");
nb1.postMessage();
     
      System.out.println("enter a new message ");
       msg=sc.nextLine();
     
nb.getMessage(msg);
System.out.println("message stored in nb");
nb.postMessage();

System.out.println("message stored in nb1");
nb1.postMessage();
    }
}
/*
THIS PROGRAM IS CODED BY TG
*/

TO STUDY COMMAND LINE ARGUMENTS

echo "TO STUDY COMMAND LINE ARGUMENTS"

        case $2 in
                +)      echo "Addition Operation"
                        x=$1
                        y=$3
                        echo "$x+$y=`expr $x + $y`" ;;

                -)      echo "Subtraction Operation"
                        x=$1
                        y=$3
                        echo "$x-$y=`expr $x - $y`" ;;

                \*)     echo "Multiplication Operation"
                        x=$1
                        y=$3
                        echo "$x*$y=`expr $x \* $y`" ;;

                /)      echo "Division Operation"
                        x=$1
                        y=$3
                        echo "$x/$y=`expr $x / $y`" ;;

        esac
#OUTPUT:
#chmod +x cmd.sh
#./cmd.sh 2 + 3
#Addition Operation
#2+3=5

PROGRAM TO DEMONSTRATE BACKGROUND PROCESSES

#PROGRAM TO DEMONSTRATE BACKGROUND PROCESSES

echo "background processes"
while :
do
ti=`date`
tput cup 0 65
echo -n $ti

sleep 1
clear
done

Tuesday, October 29, 2013

IMPLEMENTATION OF BRESNHAM LINE DRAWING ALGORITHM IN JAVA

/*
DATE:29TH JULY 2013
TITLE:IMPLEMENTATION OF BRESNHAM LINE DRAWING ALGORITHM IN JAVA
*/

import java.applet.*;
import java.awt.*;
import java.util.*;

public class bresnham extends Applet
{
    double x1,y1,x2,y2;
    double dx,dy,steps,x,y,k;

    Scanner sc=new Scanner(System.in);
    double xc,yc;
   
    public bresnham(){
    System.out.println("ENTER X1 Y1");
    x1=sc.nextDouble();
    y1=sc.nextDouble();
    System.out.println("ENTER X2 Y2");
    x2=sc.nextDouble();
    y2=sc.nextDouble();
    }
  
    public void paint(Graphics g)
    {
    int ch=6;
            dx=Math.abs(x2-x1);
        dy=Math.abs(y2-y1);
        x=x1;
        y=y1;
        double e=(2*dy)-(dx);
        int i=1;
        //THIS PROGRAM IS CODED BY TG6483
        one: for(int j=10;ch==6;j++)
        {
            g.fillOval((int)x,(int)y,5,5);
            while(e>=0)
            {
                y=y+1;
                e=e-(2*dx);
            }
            x=x+1;
            e=e+(2*dy);
            i++;
            if(i<=dx)
            ch=6;
            else
            break one;
        }
    }
}
/*<applet code="bresnham.class" width="1000" height="1000">
 </applet>*/
 /*
 OUTPUT:
 bash-3.00$ appletviewer bresnham.java
ENTER X1 Y1
500
200
ENTER X2 Y2
200
600
*/


Monday, October 28, 2013

WAP to generate Fibonacci series

#WAP to generate Fibonacci series 

echo “Enter the range to be displayed” 

read n 
a=0
b=1
echo fibonacci series 
echo $a 
echo $b i=2 
while test $i -It $n 
do c = ‘expr $a + $b’ 
echo $c 
i=`expr$i+ 1’ 
a = $b 
b = $c 
done 

WAP to counts the number of lines and words present in a given file.

#WAP to counts the number of lines and words present in a given file.  

echo Enter a file name:
read n
echo Number of Lines:wc –l $n
echo Number of Words:wc –w $n
echo Number of Characters:wc –c $n

Largest of three numbers with the total and the average.

#WAP to find Largest of three numbers with the total and the average.

echo "Enter the 1st Number: "
read n1
echo "Enter the 2nd Number: "
read n2
echo "Enter the 3rd Number: "
read n3

total=`expr $n1 + $n2 + $n3`
avg=`expr $total / 3`
if [ $n1 -gt $n2 -a $n1 -gt $n3 ]
then
echo "$n1 is the Largest of three"
elif [ $n2 -gt $n1 -a $n2 -gt $n3 ]
then
echo "$n2 is the Largest of three"
elif [ $n3 -gt $n1 -a $n3 -gt $n2 ]
then
echo "$n3 is the Largest of three"
else
echo "All the three numbers are Equal"
fi
echo "Total: $total"
echo "Average: $avg"

To find a factorial of a given no.

 #To find a factorial of a given no.
fact=1
ie=1
echo -e "Enter a number:\c"
read a
while [ $ie -le $a ]
do
        fact=`expr $fact \* $ie`
        ie=`expr $ie + 1`
done
echo -e "Multilpication of $a number is $fact."
#OUTPUT
#Enter a number:5
#Multilpication of 10 number is 120.

Shell script to find the sum of the first n numbers.

#Write a shell script to find the sum of the first n numbers.


echo "Enter a number: "
read n
i=1
sum=0
while [ $i -le $n ]
do
sum=`expr $sum + $i`
i=`expr $i + 1`
done
echo "The sum of first $n numbers is: $sum"
#Enter a number:
#5
#The sum of first 5 numbers is: 15

PROGRAM TO FIND PRIME NOS IN C++

//PROGRAM TO FIND PRIME NOS IN C++
#include<iostream.h>
void main()
{
int a;
cout<<"Enter a no"<<endl;
cin>>a;

int s=0;
for(int i=1;i<a;i++)
{
if(a%i==0)
{
s=1;
}
else
{
s=0;
}
}
if(s==1)
cout<<"no is prime";
else
cout<<"no is nt prime";
}

PROGRAM TO FIND PRIME NOS IN SHELL SCRIPT

#PROGRAM TO FIND PRIME NOS IN SHELL SCRIPT
echo "Enter a number: "
read num
i=2
f=0
while [ $i -le `expr $num / 2` ]
do
if [ `expr $num % $i` -eq 0 ]
then
f=1
fi
i=`expr $i + 1`
done
if [ $f -eq 1 ]
then
echo "The number is composite"
else
echo "The number is Prime"
fi


#OUTPUT
#Enter a number:
#5
#The number is Prime

TO CALCULATE THE CHARACTERS,WORDS AND LINE IN A SENTENCE

//TO CALCULATE THE CHARACTERS,WORDS AND LINE IN A SENTENCE
#include<iostream.h>
#include<string.h>
void main()
{
char str[100];
char ch;
int i,w=0,l=0,c=0;
cout<<"enter para:=\n";
while(1)
{
i=0;
while((ch=cin.get())!='\n')
{
str[i]=ch;
i++;
}

str[i]='\0';
if(str[0]=='\0')
break;
w++;
i=0;
while(str[i]!='\0')
{
if(str[i]=='\t'||str[i]==' ')
w++;
i++;
}
l++;
c=c+strlen(str);
}
cout<<"\n char cnst="<<c;
cout<<"\n wrd count="<<w;
cout<<"\nline count="<<l;
}

PROGRAM TO DISPLAY TABLE OF NUMBER


#PROGRAM TO DISPLAY TABLE OF NUMBER-WHILE LOOP

echo "enter no for which table is to be printed";
read x
i=1
while [ $i -lt 13 ]
do
echo " $x x $i = ` expr $x \* $i `"
i=`expr $i /* 1`;
done
---------------------------------------------------------------------------------------

# SAME PROGRAM USING FOR LOOP
echo enter number for table
read n
for (( i=1;i<=12;i++ ))
do
echo " $n x $i= `expr $n \* $i` ";
done
echo THANK U CODED BY TG

#OUTPUT:
#enter number for table
#10
#  10 x 1= 10
 #10 x 2= 20
 #10 x 3= 30
 #10 x 4= 40
 #10 x 5= 50
 #10 x 6= 60
 #10 x 7= 70
 #10 x 8= 80
 #10 x 9= 90
 #10 x 10= 100
 #10 x 11= 110
 #10 x 12= 120
#THANK U CODED BY TG#

PROGRAM TO DISPLAY SCREEN HANDLING

#PROGRAM TO DISPLAY SCREEN HANDLING

echo "enter text";
read text
for (( i=10 ;i>0;i++ ))
do
echo "enter ur choice"

echo -e "1>Bold \n 2>underline \n 3>strikethrough \n 4>text color \n 5>baground";
read choice

case $choice in
1)
echo "bold"
echo -e "\033[01m  $text"
echo -e "\033[00m  ";;
2)
echo "underline"
echo -e "\033[04m $text"
echo -e "\033[00m  ";;
3)
echo "strikethruh"
echo -e "\033[09m  $text"
echo -e "\033[00m  ";;
4)
echo "text color 35"
echo -e "\033[35m  $text"
echo -e "\033[00m  ";;
5)
echo "background color 44"
echo -e "\033[044m  $text"
echo -e "\033[00m ";;

esac
done

DISPLAY THE SUM AND REVERSE OF A NUMBER USING WHILE LOOP.

#DISPLAY THE SUM AND REVERSE OF A NUMBER USING WHILE LOOP.

echo "Enter the number: "
read x
rev=0
sum=0
d=0

while [ $x -gt 0 ]
do
 d=`expr $x % 10`
 x=`expr $x / 10`
 sum=`expr $sum + $d`
 rev=`expr $rev \* 10`
 rev=`expr $rev + $d`
done

echo "sum= " $sum
echo "reverse= " $rev

#OUTPUT:
#bash-3.00$ ./EXP7_1.sh
#Enter the number:
#123
#sum=  6
#reverse=  321

TO DEMONSTRATE NESTED IF ELSE USING SHELL SCRIPTING.

#TO DEMONSTRATE NESTED IF ELSE USING SHELL SCRIPTING.
echo "Enter percentage: "
read a

if [ $a -ge 75 ]
then
 echo "First Class Distinction"
elif [ $a -ge 60 ]
then
 echo "First Class"
elif [ $a -ge 40 ]
then
 echo "Second Class"
else
 echo "Fail"
fi


#OUTPUT:
#Enter percentage:
#75
#First Class Distinction

TO FIND IF NOS ARE EQUAL OR NOT USING SHELL SCRIPT

#TOPIC :TO FIND IF NOS ARE EQUAL OR NOT IN SHELL SCRIPT
echo "enter a:"
read a
echo "enter b:"
read b
if [ $a -eq $b ]
then
echo " numbers are not equal"
else
echo "nos are equal"
fi

TO PERFORM ARITHMETIC OPERATIONS USING SHELL SCRIPTING

#TO PERFORM ARITHMETIC OPERATIONS USING SHELL SCRIPTING IN LINUX

echo "Enter the number 1: "
read x
echo "Enter the number 2: "
read y

exit=0
while [ $exit -eq 0 ]
do

echo -e "\n1.Addition \n2.Subtraction \n3.Multiplication \n4.Division \n5.Mod \nEnter your choice:  "
read ch
case $ch in
 1)add=`expr $x + $y`
   echo "Addition= " $add;;
 2)sub=`expr $x - $y`
   echo "Subtraction= " $sub;;
 3)mul=`expr $x \* $y`
   echo "Multiplication= " $mul;;
 4)div=`expr $x / $y`
   echo "Division= " $div;;
 5)mod=`expr $x % $y`
   echo "Mod= " $mod;;
   esac

echo -e "\nDo you want to continue(0:'YES'/1:'NO'):  "
read exit

done


DIJKSTRA'S ALGORITHM IN JAVA

/*
TOPIC: dijkstra's algorithm in java
*/
import java.util.*;

public class dij
{
        public static void main(String args[])
        {
                int d[][]={{0,9,3,0,0,0},
                           {2,0,6,5,3,0},
                           {3,6,0,0,1,0},
                           {0,5,0,0,2,2},
                           {0,3,1,1,0,4},
                           {0,0,0,2,4,0}};
                int vi[][]=new int[6][6];

                Vector v=new Vector();

                int s=0,de=5,c=0,cost=-1,co=0,temp=0;

                while(de!=c)
                {
                        for(int i=-1;i<=5;i++)
                        {
                                while(cost<=0)
                                {cost=d[++i][c]; temp=i;}

                                if(d[de][c]>0 && vi[de][c]==0)
                                { cost=d[de][c];  vi[de][c]=1; temp=de;   break;}

                                else
                                {
                                if(d[i][c]!=0 && d[i][c]<cost && vi[i][c]==0)
                                        { cost=d[i][c]; vi[i][c]=1;  temp=i; }
                                }

                        }
                        c=temp;
                        co=co+cost;
                        cost=-1;
                        v.add(c+1);

                }

                System.out.print("\nCost is : "+co);
                System.out.println("\nPath is: ");
                System.out.print(s+1+"  ");
                                Enumeration e=v.elements();
                                while(e.hasMoreElements())
                                System.out.print(e.nextElement()+"  ");
                                System.out.print("");
        }

}
/*
Cost is : 9
Path is:
1  2  5  6
*/