28
PRACTICAL COMPUTING CSC305MC3 ASSIGNMENT-01 R LOGARAJAH 2005/SP/30

PRACTICAL COMPUTING

Embed Size (px)

DESCRIPTION

matlab And java coding

Citation preview

Page 1: PRACTICAL COMPUTING

PRACTICAL COMPUTING

CSC305MC3

ASSIGNMENT-01

R LOGARAJAH2005/SP/30

DEPARTMENT OF COMPUTER SCIENCE

Page 2: PRACTICAL COMPUTING

Matlab Coding

Filter

1).Maximum filter:-

S

function Img=MaximumFilter(Img)[row,col]=size(Img);copyImg=Img;filter=[1 1 1;1 1 1;1 1 1];filter1=zeros(3,3);for x=2:row-1 for y=2:col-1 for i=-1:1 for j=-1:1 p=copyImg(x+i,y+j); c=filter(j+2,i+2); filter1(j+2,i+2)=c*p; end end q=max(max(filter1)); Img(x,y)=q; endendI=imread('123.tif');b=MaximumFilter(I);subplot(2,2,1); imshow(I);subplot(2,2,2); imshow(b);Output:-

Page 3: PRACTICAL COMPUTING

2) Minimum filter

function Img=mininumfilter(Img)[row,col]=size(Img);copyImg=Img;filter=[1 1 1;1 1 1;1 1 1];filter1=zeros(3,3);for x=2:row-1 for y=2:col-1 for i=-1:1 for j=-1:1 c=copyImg(x+i,y+j); p=filter(j+2,i+2); filter1(j+2,i+2)=c*p; end end q=min(min(filter1)); Img(x,y)=q; endend

>>I=imread('45.tif'); >> m=mininumfilter(I); >> subplot(2,2,1);imshow(I);//h>> subplot(2,2,2);imshow(m); Output:-

Page 4: PRACTICAL COMPUTING

3) Average Filter

function Img=AverageFilter(Img)[row,col]=size(Img);copyImg=Img;for x=2:row-1 for y=2:col-1 sum=0; for i=-1:1 for j=-1:1 p=copyImg(x+i,y+j); sum=sum+p; end end q=sum/2; Img(x,y)=q; endend

>> I=imread('11.tif');>> m=AverageFilter(I); >>subplot(2,2,1);imshow(I);>> subplot(2,2,2);imshow(m); Output:-

Page 5: PRACTICAL COMPUTING

4) Smoothing Filter

function Img=Smoothing(Img)[row,col]=size(Img);copyImg=Img;filter=[0.075 0.125 0.175;0.125 0.200 0.125;0.075 0.125 0.175];for x=2:row-1 for y=2:col-1 sum=0; for i=-1:1 for j=-1:1 p=copyImg(x+i,y+j); c=filter(i+2,j+2); sum=sum+c*p; end end q=sum; Img(x,y)=q; endend>> I=imread('45.tif');>> m=Smoothing(I);>>subplot(2,2,1);imshow(I);>> subplot(2,2,2);imshow(m);Output:-

5) Median filter

function Img=MedianFilter(Img)[row,col]=size(Img);k=4;

Page 6: PRACTICAL COMPUTING

p=zeros(2*k+1);copyImg=Img;for x=2:row-1 for y=2:col-1 m=1; for i=-1:1 for j=-1:1 p(m)=copyImg(x+i,y+j); m=m+1; end end sort(p); Img(x,y)=p(k); endend

>>I=imread('11.tif');>> m=MedianFilter(I); >> subplot(2,2,1);imshow(I);>> subplot(2,2,2);imshow(m);Output:-

6) Difference Filterfunction Img=DifferenceFilter(Img)[row,column]=size(Img);CopyImg=Img;filter=[0 0 -1 0 0; 0 -1 2 -1 0; -1 2 16 2 -1;0 -1 2 -1 0;0 0 -1 0 0];for x=2:row-1 for y=2:column-1 sum=0; for i=-1:1 for j=-1:1 p=CopyImg(x+i,y+j);

Page 7: PRACTICAL COMPUTING

c=filter(i+2,j+2); sum=sum+c*p; end end q=sum; Img(x,y)=q; endend>> I=imread('22.tif');>>Im=DifferenceFilter(I);>>subplot(1,2,1),imshow(I);>>subplot(1,2,2),imshow(Im);

Output:-

7) Prewitt Filter

function Img=PrewittFilter(Img)CopyImg=Img;Hx=[-1 0 1 ; -1 0 1; -1 0 1];Hy=[-1 -1 -1; 0 0 0 ; 1 1 1];Dx=conv2(CopyImg ,Hx);Dy=conv2(CopyImg ,Hy);Img=round(sqrt(Dx.*Dx+Dy.*Dy));

end >>I=imread('44.tif');>>Im=PrewittFilter(I);>>subplot(1,2,1),imshow(I);

Page 8: PRACTICAL COMPUTING

>>subplot(1,2,2),imshow(Im)

Output:-

8) Sobel filter

function Img=SobelFilter(Img)CopyImg=Img;Hx=[-1 0 1 ; -2 0 2; -1 0 1];Hy=[-1 -2 -1; 0 0 0 ; 1 2 1];Dx=conv2(CopyImg ,Hx);Dy=conv2(CopyImg ,Hy);Img=round(sqrt(Dx.*Dx+Dy.*Dy));

end >>I=imread('44.tif');>>Im=SobelFilter(I);>>subplot(1,2,1),imshow(I);>>subplot(1,2,2),imshow(Im)

Output:-

9) UnsharpMask

function Img=UnsharpMask(Img,a)CopyImg=Img;

Page 9: PRACTICAL COMPUTING

D1=Smoothing(CopyImg);I=(1+a).*CopyImg;J=a.*D1;Img=I-J;

end I=imread('55.tif');Im=UnsharpMask(I,0.5);subplot(1,2,1),imshow(I);subplot(1,2,2),imshow(Im)

10) Robert filter

function Img=RobertFilter(Img)CopyImg=Img;H0=[0 1;-1 0];H1=[-1 0;0 1]; D0=conv2(CopyImg ,H0);D1=conv2(CopyImg ,H1);Img=round(sqrt(D0.*D0+D1.*D1));

end I=imread('7.tif');Im=RobertFilter(Im);subplot(1,2,1),imshow(I);subplot(1,2,2),imshow(Im)output:-

11) Gaussian filter

Gaussian kernel .

Page 10: PRACTICAL COMPUTING

function kernal=makeGaussian(sigma)center=3*sigma;kernal=zeros(2*center+1);sigma2=sigma*sigma;for i=1:size(kernal)-1 r=center-i; kernal(i)=exp(-0.5*(r*r)/sigma2);end

===================================function Img=Gussianfilter(Img)[row,col]=size(Img);copyImg=Img;filter=makeGaussian(5);for x=2:row-1 for y=2:col-1 sum=0; for i=-1:1 for j=-1:1 c=copyImg(x+i,y+j); p=filter(j+2,i+2); sum=sum+c*p; end end q=sum; Img(x,y)=q; endendI=imread('122.tif');

Im=Gussianfilter(I);

subplot(1,2,1),imshow(I);

subplot(1,2,2),imshow(Im)

output:-

Page 11: PRACTICAL COMPUTING

Programming in Java

1) Quick Sort in the array elements

import java.util.*;public class QuickSort{static int n;static int[] array;Scanner s;public QuickSort(){s=new Scanner(System.in);System.out.print("How many elements :");n=s.nextInt();array=new int[n];}public void readArray(){

for (int i=0;i<array.length;i++){

System.out.print("array["+i+"] :");array[i]=s.nextInt();

}}public void printArray(){System.out.print("The Array Elements are :");for (int i=0;i<array.length;i++){

Page 12: PRACTICAL COMPUTING

System.out.print(array[i]+" ");}System.out.println();}public void quickSort(int[] array,int startIndex,int endIndex){ if(startIndex<endIndex){

int j=partition(startIndex,endIndex);quickSort(array,startIndex,j-1);quickSort(array,j+1,endIndex);

}} public int partition(int startIndex,int endIndex){ int i=startIndex;

int j=endIndex+1;int pivot=array[startIndex];

do{

do{

i=i+1;}while(array[i]<pivot&&i<endIndex);do{

j=j-1;}while(array[j]>pivot&&j>startIndex);if(i<j){

swap(array,i,j);}

}while(i<j);swap(array,startIndex,j);return j;

} public void swap(int[] array, int index1, int index2 ) {

Page 13: PRACTICAL COMPUTING

int tmp = array[index1]; array[index1]=array[index2]; array[index2]=tmp; }

public static void main(String args[]){

QuickSort q=new QuickSort();q.readArray();q.printArray();q.quickSort(array,0,array.length-1);q.printArray();

}}

Output:-F:\Java>java QuickSortHow many elements :7array[0] :22array [1] :11array [2] :55array [3] :12array[4] :99array[5] :77array[6] :55The Array Elements are: 22 11 55 12 99 77 55The Array Elements are: 11 12 22 55 55 77 99Quick sort:-Partition procedureStep1:-p- startindex.

r- lastidex.Step2:-If startindex < lastidex thenstep3:-q Partition(array,p,r)

Quick Sort (array, p, q)Quick Sort (array, q + r, r)

Step4:-pivot array[p]i pj r+1

step5:-while do

Page 14: PRACTICAL COMPUTING

Repeat j j-1 Until array[j]≤ pivot

   Repeat i i+1 until array[i] ≥ pivotIf i < j

then swap(array,i,j) else return jstep:-stop.

Best Case

Produces two regions of size n/2

T (n)=T(n/2)+T(N/2)+Ѳ(n)

T (n) = Ѳ (n lg n)

Worst case

T(n)= Ѳ (n2)

Average case

T (n) = Ѳ (n lg (n))Conclusion:-Compare to quick sort running time, worst case >average case>best case

2)

Import java .util.*;

public class PerfectNumber

{

static int n;

static int pNo = 0;

public static void main(String []args)

Page 15: PRACTICAL COMPUTING

{

System.out.print("Enter any number =");

Scanner s = new Scanner(System.in);

n= s.nextInt();

System.out.println("PerfectNumber are:");

for (int i = 1; i < n; i++)

{

if (n % i == 0)

{ pNo += i;

System.out.println(i);

}

}

if (pNo == n)

{

System.out.println("number is a perfect number");

}

else

{

System.out.println ("number is not a perfect number");

}

}

}

Compiler and output

C:\Users\lograjah\Desktop>java PerfectNumber

Enter any number =6

PerfectNumber are:

1

2

3

Page 16: PRACTICAL COMPUTING

number is a perfect number

C:\Users\lograjah\Desktop>java PerfectNumber

Enter any number =100

PerfectNumber are:

1

2

4

5

10

20

25

50

number is not a perfect number

4)

The greatest common divisor (GCD)

Import java.uti1l.*;Public class GCD{

Static int a,b;Public GCD(){

Scanner s=new Scanner(System.in);System.out.print("Enter the first number: ");a=s.nextInt();System.out.print("Enter the second number: ");b=s.nextInt();

}Public int gcd(int a,int b){

if(b==0)return a;

else if(a<b)

Page 17: PRACTICAL COMPUTING

return gcd(b,a);else

return gcd(b,a%b);}public static void main(String args[]){

GCD g=new GCD();System.out.println("The GCD is: "+g.gcd(a,b));

}}Output:-

C:\Users\lograjah\Desktop>java GCDEnter the first number: 12Enter the second number: 6The GCD is: 6C:\Users\lograjah\Desktop>java GCDEnter the first number: 56Enter the second number: 48The GCD is: 8

5) A palindrome is a number or a text phrase that reads the same back and forth.

Import java.util.*;public class palindrome{

String word;String copyword="";public Stack s=new Stack();public palindrome(){

Scanner input=new Scanner(System.in);System.out.print("Enter the string :-");word=input.nextLine();word=word.toUpperCase();filter();System.out.println("Given string after ignore special characters is: "+copyword);

for(int i=0;i<copyword.length();i++){

s.push((Object)copyword.charAt(i));

Page 18: PRACTICAL COMPUTING

}}public void filter(){

for(int i=0;i<word.length();i++){

char ch=word.charAt(i);if(ch>=48 && ch<=57 || ch>=65 && ch<=90)

copyword+=ch;}

}public void checkpalin(){

for(int i=0;i<copyword.length();i++){

if(s.peek()==(Object)copyword.charAt(i))s.pop();

else break;

}}public void getResult(){

if(s.empty())System.out.println("Given word or phrase is palindrome");

elseSystem.out.println("Given word or phrase is not palindrome");

}public static void main(String args[]){

palindrome p=new palindrome();p.checkpalin();p.getResult();

}}Output:-I:\Assignment>java palindromeEnter the string :-radar

Page 19: PRACTICAL COMPUTING

Given string after ignore special characters is: RADARGiven word or phrase is palindrome

I:\Assignment>java palindromeEnter the string :-won't lovers revolt now?Given string after ignore special characters is: WONTLOVERSREVOLTNOWGiven word or phrase is palindrome

6) Tower of Hanoi

function[] = towers(n,source,destination,tmp) if (n == 1) fprintf('\t move disk 1 source %c destination %c \n',source,destination); else towers(n-1,source,tmp,destination); fprintf('\t move disk %d source %c destination %c \n',n,source,destination); towers(n-1,tmp,destination,source); end

>> n= input('Towers of Hanoi: How many disks?\n');

fprintf('\n\n')

towers(n,'A','B','C');

fprintf('\n\n');

Towers of Hanoi: How many disks?3

move disk 1 source A destination B move disk 2 source A destination C move disk 1 source B destination C move disk 3 source A destination B

Page 20: PRACTICAL COMPUTING

move disk 1 source C destination A move disk 2 source C destination B move disk 1 source A destination B

7) Give area may be found shortest path

import java.util.*;class shortpath{

public static int x1,x2,y1,y2,n,m;public static int a[][]=new int[10][10],parent[][]=newint[10][10],dist[][]=new int[10][10];

public static void main(String s[]){

Scanner s1=new Scanner(System.in);int x,y,num;LinkedList l=new LinkedList();LinkedList path=new LinkedList();System.out.println("Enter the value for n & m 1st matrix");n=s1.nextInt();m=s1.nextInt();System.out.println("Enter the elements for 1st matrix(0 or 1)");for(int i=0;i<n;i++)

for(int j=0;j<m;j++){

a[i][j]=s1.nextInt();parent[i][j]=-1;dist[i][j]=n*m;

}System.out.println("Enter the position of source(x,y)");x1=s1.nextInt();y1=s1.nextInt();System.out.println("Enter the position of Destination(x,y)");x2=s1.nextInt();y2=s1.nextInt();l.add(getnum(x1,y1));dist[x1][y1]=0;

Page 21: PRACTICAL COMPUTING

while(l.size()!=0){

num=Integer.parseInt(l.removeLast().toString());x=num/m;y=num%m;if(x>0&&a[x-1][y]!=1&&(dist[x-1][y]>(dist[x][y])+1)){

l.add(getnum(x-1,y));dist[x-1][y]=dist[x][y]+1;parent[x-1][y]=num;

}if(y>0&&a[x][y-1]!=1&&(dist[x][y-1]>(dist[x][y])+1)){

l.add(getnum(x,y-1));dist[x][y-1]=dist[x][y]+1;parent[x][y-1]=num;

}if(x<n-1&&a[x+1][y]!=1&&(dist[x+1][y]>(dist[x][y])+1)){

l.add(getnum(x+1,y));dist[x+1][y]=dist[x][y]+1;parent[x+1][y]=num;

}if(y<m-1&&a[x][y+1]!=1&&(dist[x][y+1]>(dist[x][y])+1)){

l.add(getnum(x,y+1));dist[x][y+1]=dist[x][y]+1;parent[x][y+1]=num;

}}

path.add("("+x2+","+y2+")");x=x2;y=y2;do{num=parent[x][y];x=num/m;y=num%m;

Page 22: PRACTICAL COMPUTING

path.add("("+x+","+y+")");}while(!(x==x1&&y==y1));while(path.size()!=0)

System.out.println(path.removeLast());}public static Integer getnum(int x,int y){

return new Integer(x*m+y);}

}

Output:-

I:\Assignment>java shortpathEnter the value for n & m 1st matrix44Enter the elements for 1st matrix(0 or 1)1236547894567894Enter the position of source(x,y)00Enter the position of Destination(x,y)

Page 23: PRACTICAL COMPUTING

33(0,0)(0,1)(0,2)(0,3)(1,3)(2,3)(3,3)

10) A car dealer, who sells petrol and diesel car, need to print personalized letters for customers according to the type of car they bough.

Diesel Car class

public class DieselCar extends Car{

public DieselCar(String name){

super(name);}public String toString(){

return String.format("%s","many thanks for your custom");}

}

Car class

public class Car{

String Customername;public Car(String name){

Page 24: PRACTICAL COMPUTING

Customername=name;setName(name);

}public void setName(String name){

Customername=name;}public String getName(){

return Customername;}public String toString1(){

return String.format("%s %s, ","Dear",Customername);}

}

Customer Directory class(main)

import java.util.*;public class CustomerDirectory{

static Scanner s=new Scanner(System.in);static String n;static int t;public static void main(String args[]){

System.out.println("Enter the Name: ");n=s.nextLine();System.out.println("Enter the Car type(petrolcar=1 or dieselcar=0):");t=s.nextInt();if(t==1){PetrolCar pc=new PetrolCar(n);

System.out.println(pc.toString1()+pc.toString());}else if(t==0){

DieselCar dc=new DieselCar(n);System.out.println(dc.toString1()+dc.toString());

Page 25: PRACTICAL COMPUTING

}else System.out.println("another Type");

}}Output:-I:\Assignment>javac CustomerDirectory.java

I:\Assignment>java CustomerDirectoryEnter the Name:logaEnter the Car type(petrolcar=1 or dieselcar=0):1

Dear loga, Please find enclosed a complimentary voucher for the amount of Rs.5000