SlideShare a Scribd company logo
1 of 25
PRACTICAL COMPUTING
          CSC305MC3
        ASSIGNMENT-01




         R LOGARAJAH
          2005/SP/30
DEPARTMENT OF COMPUTER SCIENCE
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;
   end
end
I=imread('123.tif');
b=MaximumFilter(I);
subplot(2,2,1); imshow(I);
subplot(2,2,2); imshow(b);
Output:-
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;
   end
end

>>I=imread('45.tif');
>> m=mininumfilter(I);
>> subplot(2,2,1);imshow(I);//h
>> subplot(2,2,2);imshow(m);
Output:-
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;
  end
end


>> I=imread('11.tif');
>> m=AverageFilter(I);
>>subplot(2,2,1);imshow(I);
>> subplot(2,2,2);imshow(m);
Output:-
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;
   end
end
>> 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;
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);
  end
end

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




6) Difference Filter
function 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);
         c=filter(i+2,j+2);
         sum=sum+c*p;
       end
     end
     q=sum;
     Img(x,y)=q;
  end
end
>> 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);
>>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;
       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.
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;
   end
end
I=imread('122.tif');

Im=Gussianfilter(I);

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

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

output:-
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++)
 {
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 )
            {
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 QuickSort
How many elements :7
array[0] :22
array [1] :11
array [2] :55
array [3] :12
array[4] :99
array[5] :77
array[6] :55
The Array Elements are: 22 11 55 12 99 77 55
The Array Elements are: 11 12 22 55 55 77 99
Quick sort:-
Partition procedure
Step1:- p- startindex.
                r- lastidex.
Step2:-If startindex < lastidex then
step3:-q      Partition(array,p,r)
                         Quick Sort (array, p, q)
                         Quick Sort (array, q + r, r)
Step4:-pivot          array[p]
            i        p
            j       r+1
step5:-while do
Repeat j       j-1
        Until array[j]โ‰ค pivot
        Repeat i       i+1
        until array[i] โ‰ฅ pivot
        If i < j
    then swap(array,i,j)
        else return j
step:-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)
{
        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:UserslograjahDesktop>java PerfectNumber
Enter any number =6
PerfectNumber are:
1
2
3
number is a perfect number
C:UserslograjahDesktop>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)
                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:UserslograjahDesktop>java GCD
Enter the first number: 12
Enter the second number: 6
The GCD is: 6
C:UserslograjahDesktop>java GCD
Enter the first number: 56
Enter the second number: 48
The 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));
                  }
   }
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");
      else
               System.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 palindrome
Enter the string :-radar
Given string after ignore special characters is: RADAR
Given word or phrase is palindrome
I:Assignment>java palindrome
Enter the string :-won't lovers revolt now?
Given string after ignore special characters is: WONTLOVERSREVOLTNOW
Given 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('nn')

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

fprintf('nn');

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
    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[][]=new
    int[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;
      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;

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 shortpath
Enter the value for n & m 1st matrix
4
4
Enter the elements for 1st matrix(0 or 1)
1
2
3
6
5
4
7
8
9
4
5
6
7
8
9
4
Enter the position of source(x,y)
0
0
Enter the position of Destination(x,y)
3
3
(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)
   {
        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());
        }
else System.out.println("another Type");
   }}
Output:-I:Assignment>javac CustomerDirectory.java
               I:Assignment>java CustomerDirectory
               Enter the Name:
               loga
               Enter the Car type(petrolcar=1 or dieselcar=0):
               1
Dear loga, Please find enclosed a complimentary voucher for the amount of Rs.5000

More Related Content

What's hot

Java ะฒะตัะฝะฐ 2013 ะปะตะบั†ะธั 2
Java ะฒะตัะฝะฐ 2013 ะปะตะบั†ะธั 2Java ะฒะตัะฝะฐ 2013 ะปะตะบั†ะธั 2
Java ะฒะตัะฝะฐ 2013 ะปะตะบั†ะธั 2
Technopark
ย 
&Y tgs P kii for
&Y tgs P kii for&Y tgs P kii for
&Y tgs P kii for
Oktavian Dani
ย 
C Prog - Array
C Prog - ArrayC Prog - Array
C Prog - Array
vinay arora
ย 

What's hot (20)

The Ring programming language version 1.4 book - Part 9 of 30
The Ring programming language version 1.4 book - Part 9 of 30The Ring programming language version 1.4 book - Part 9 of 30
The Ring programming language version 1.4 book - Part 9 of 30
ย 
Java practical
Java practicalJava practical
Java practical
ย 
Chapter 7 functions (c)
Chapter 7 functions (c)Chapter 7 functions (c)
Chapter 7 functions (c)
ย 
The Ring programming language version 1.5.3 book - Part 10 of 184
The Ring programming language version 1.5.3 book - Part 10 of 184The Ring programming language version 1.5.3 book - Part 10 of 184
The Ring programming language version 1.5.3 book - Part 10 of 184
ย 
Java ะฒะตัะฝะฐ 2013 ะปะตะบั†ะธั 2
Java ะฒะตัะฝะฐ 2013 ะปะตะบั†ะธั 2Java ะฒะตัะฝะฐ 2013 ะปะตะบั†ะธั 2
Java ะฒะตัะฝะฐ 2013 ะปะตะบั†ะธั 2
ย 
&Y tgs P kii for
&Y tgs P kii for&Y tgs P kii for
&Y tgs P kii for
ย 
The Ring programming language version 1.5.4 book - Part 10 of 185
The Ring programming language version 1.5.4 book - Part 10 of 185The Ring programming language version 1.5.4 book - Part 10 of 185
The Ring programming language version 1.5.4 book - Part 10 of 185
ย 
JVM Mechanics
JVM MechanicsJVM Mechanics
JVM Mechanics
ย 
Monadic Comprehensions and Functional Composition with Query Expressions
Monadic Comprehensions and Functional Composition with Query ExpressionsMonadic Comprehensions and Functional Composition with Query Expressions
Monadic Comprehensions and Functional Composition with Query Expressions
ย 
The Ring programming language version 1.3 book - Part 24 of 88
The Ring programming language version 1.3 book - Part 24 of 88The Ring programming language version 1.3 book - Part 24 of 88
The Ring programming language version 1.3 book - Part 24 of 88
ย 
C Prog - Array
C Prog - ArrayC Prog - Array
C Prog - Array
ย 
Developer Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duoDeveloper Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duo
ย 
Awt
AwtAwt
Awt
ย 
Computer java programs
Computer java programsComputer java programs
Computer java programs
ย 
JVM Mechanics: Understanding the JIT's Tricks
JVM Mechanics: Understanding the JIT's TricksJVM Mechanics: Understanding the JIT's Tricks
JVM Mechanics: Understanding the JIT's Tricks
ย 
Let the type system be your friend
Let the type system be your friendLet the type system be your friend
Let the type system be your friend
ย 
Microsoft Word Hw#1
Microsoft Word   Hw#1Microsoft Word   Hw#1
Microsoft Word Hw#1
ย 
design and analysis of algorithm Lab files
design and analysis of algorithm Lab filesdesign and analysis of algorithm Lab files
design and analysis of algorithm Lab files
ย 
From NumPy to PyTorch
From NumPy to PyTorchFrom NumPy to PyTorch
From NumPy to PyTorch
ย 
The Ring programming language version 1.7 book - Part 91 of 196
The Ring programming language version 1.7 book - Part 91 of 196The Ring programming language version 1.7 book - Part 91 of 196
The Ring programming language version 1.7 book - Part 91 of 196
ย 

Similar to PRACTICAL COMPUTING

Please help with this JAVA Assignment and show output if you can ple.pdf
Please help with this JAVA Assignment and show output if you can ple.pdfPlease help with this JAVA Assignment and show output if you can ple.pdf
Please help with this JAVA Assignment and show output if you can ple.pdf
aroramobiles1
ย 
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
Java AssignmentWrite a program using sortingsorting bubble,sele.pdfJava AssignmentWrite a program using sortingsorting bubble,sele.pdf
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
eyewatchsystems
ย 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdf
Rahul04August
ย 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
Er Ritu Aggarwal
ย 
Sorting programs
Sorting programsSorting programs
Sorting programs
Varun Garg
ย 
Hello, I need some assistance in writing a java program THAT MUST US.pdf
Hello, I need some assistance in writing a java program THAT MUST US.pdfHello, I need some assistance in writing a java program THAT MUST US.pdf
Hello, I need some assistance in writing a java program THAT MUST US.pdf
FashionColZone
ย 
Array
ArrayArray
Array
Radha Rani
ย 
81818088 isc-class-xii-computer-science-project-java-programs
81818088 isc-class-xii-computer-science-project-java-programs81818088 isc-class-xii-computer-science-project-java-programs
81818088 isc-class-xii-computer-science-project-java-programs
Abhishek Jena
ย 
FileName EX06_1java Programmer import ja.pdf
FileName EX06_1java Programmer  import ja.pdfFileName EX06_1java Programmer  import ja.pdf
FileName EX06_1java Programmer import ja.pdf
actocomputer
ย 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Feb-2021_L6-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Feb-2021_L6-...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Feb-2021_L6-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Feb-2021_L6-...
MaruMengesha
ย 

Similar to PRACTICAL COMPUTING (20)

Please help with this JAVA Assignment and show output if you can ple.pdf
Please help with this JAVA Assignment and show output if you can ple.pdfPlease help with this JAVA Assignment and show output if you can ple.pdf
Please help with this JAVA Assignment and show output if you can ple.pdf
ย 
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
Java AssignmentWrite a program using sortingsorting bubble,sele.pdfJava AssignmentWrite a program using sortingsorting bubble,sele.pdf
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
ย 
programs on arrays.pdf
programs on arrays.pdfprograms on arrays.pdf
programs on arrays.pdf
ย 
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple Programs
ย 
Vcs16
Vcs16Vcs16
Vcs16
ย 
Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"
ย 
Cpds lab
Cpds labCpds lab
Cpds lab
ย 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdf
ย 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
ย 
QA Auotmation Java programs,theory
QA Auotmation Java programs,theory QA Auotmation Java programs,theory
QA Auotmation Java programs,theory
ย 
Sorting programs
Sorting programsSorting programs
Sorting programs
ย 
Hello, I need some assistance in writing a java program THAT MUST US.pdf
Hello, I need some assistance in writing a java program THAT MUST US.pdfHello, I need some assistance in writing a java program THAT MUST US.pdf
Hello, I need some assistance in writing a java program THAT MUST US.pdf
ย 
Array
ArrayArray
Array
ย 
Why Learn Python?
Why Learn Python?Why Learn Python?
Why Learn Python?
ย 
ISCP internal.pdf
ISCP internal.pdfISCP internal.pdf
ISCP internal.pdf
ย 
81818088 isc-class-xii-computer-science-project-java-programs
81818088 isc-class-xii-computer-science-project-java-programs81818088 isc-class-xii-computer-science-project-java-programs
81818088 isc-class-xii-computer-science-project-java-programs
ย 
FileName EX06_1java Programmer import ja.pdf
FileName EX06_1java Programmer  import ja.pdfFileName EX06_1java Programmer  import ja.pdf
FileName EX06_1java Programmer import ja.pdf
ย 
Pnno
PnnoPnno
Pnno
ย 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Feb-2021_L6-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Feb-2021_L6-...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Feb-2021_L6-...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Feb-2021_L6-...
ย 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
ย 

More from Ramachendran Logarajah (7)

A/L ICT syllabus
A/L ICT syllabusA/L ICT syllabus
A/L ICT syllabus
ย 
computer network
computer networkcomputer network
computer network
ย 
Algorithm
Algorithm Algorithm
Algorithm
ย 
Lesson22
Lesson22Lesson22
Lesson22
ย 
Lesson11
Lesson11Lesson11
Lesson11
ย 
BLOOD BANK SOFTWARE PRESENTATION
BLOOD BANK SOFTWARE PRESENTATIONBLOOD BANK SOFTWARE PRESENTATION
BLOOD BANK SOFTWARE PRESENTATION
ย 
point operations in image processing
point operations in image processingpoint operations in image processing
point operations in image processing
ย 

Recently uploaded

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
ย 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
ย 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
ย 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
ย 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
ย 

Recently uploaded (20)

psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
ย 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
ย 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
ย 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
ย 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
ย 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
ย 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
ย 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
ย 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
ย 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
ย 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
ย 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
ย 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
ย 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
ย 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ย 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
ย 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ย 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
ย 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
ย 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
ย 

PRACTICAL COMPUTING

  • 1. PRACTICAL COMPUTING CSC305MC3 ASSIGNMENT-01 R LOGARAJAH 2005/SP/30 DEPARTMENT OF COMPUTER SCIENCE
  • 2. 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; end end I=imread('123.tif'); b=MaximumFilter(I); subplot(2,2,1); imshow(I); subplot(2,2,2); imshow(b); Output:-
  • 3. 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; end end >>I=imread('45.tif'); >> m=mininumfilter(I); >> subplot(2,2,1);imshow(I);//h >> subplot(2,2,2);imshow(m); Output:-
  • 4. 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; end end >> I=imread('11.tif'); >> m=AverageFilter(I); >>subplot(2,2,1);imshow(I); >> subplot(2,2,2);imshow(m); Output:-
  • 5. 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; end end >> 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);
  • 6. k=4; 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); end end >>I=imread('11.tif'); >> m=MedianFilter(I); >> subplot(2,2,1);imshow(I); >> subplot(2,2,2);imshow(m); Output:- 6) Difference Filter function 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
  • 7. p=CopyImg(x+i,y+j); c=filter(i+2,j+2); sum=sum+c*p; end end q=sum; Img(x,y)=q; end end >> 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);
  • 8. >>subplot(1,2,1),imshow(I); >>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
  • 9. function Img=UnsharpMask(Img,a) CopyImg=Img; 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
  • 10. ๏ƒ˜ Gaussian kernel. 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; end end I=imread('122.tif'); Im=Gussianfilter(I); subplot(1,2,1),imshow(I); subplot(1,2,2),imshow(Im) output:-
  • 11. 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++) {
  • 12. 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 ) {
  • 13. 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 QuickSort How many elements :7 array[0] :22 array [1] :11 array [2] :55 array [3] :12 array[4] :99 array[5] :77 array[6] :55 The Array Elements are: 22 11 55 12 99 77 55 The Array Elements are: 11 12 22 55 55 77 99 Quick sort:- Partition procedure Step1:- p- startindex. r- lastidex. Step2:-If startindex < lastidex then step3:-q Partition(array,p,r) Quick Sort (array, p, q) Quick Sort (array, q + r, r) Step4:-pivot array[p] i p j r+1 step5:-while do
  • 14. Repeat j j-1 Until array[j]โ‰ค pivot Repeat i i+1 until array[i] โ‰ฅ pivot If i < j then swap(array,i,j) else return j step:-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)
  • 15. { 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:UserslograjahDesktop>java PerfectNumber Enter any number =6 PerfectNumber are: 1 2 3 number is a perfect number
  • 16. C:UserslograjahDesktop>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) return gcd(b,a); else
  • 17. 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:UserslograjahDesktop>java GCD Enter the first number: 12 Enter the second number: 6 The GCD is: 6 C:UserslograjahDesktop>java GCD Enter the first number: 56 Enter the second number: 48 The 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)); } }
  • 18. 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"); else System.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 palindrome Enter the string :-radar Given string after ignore special characters is: RADAR Given word or phrase is palindrome
  • 19. I:Assignment>java palindrome Enter the string :-won't lovers revolt now? Given string after ignore special characters is: WONTLOVERSREVOLTNOW Given 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('nn') towers(n,'A','B','C'); fprintf('nn'); 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 move disk 1 source C destination A move disk 2 source C destination B
  • 20. 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[][]=new int[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; while(l.size()!=0)
  • 21. { 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; path.add("("+x+","+y+")");
  • 22. }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 shortpath Enter the value for n & m 1st matrix 4 4 Enter the elements for 1st matrix(0 or 1) 1 2 3 6 5 4 7 8 9 4 5 6 7 8 9 4 Enter the position of source(x,y) 0 0 Enter the position of Destination(x,y) 3
  • 23. 3 (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) { Customername=name;
  • 24. 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()); }
  • 25. else System.out.println("another Type"); }} Output:-I:Assignment>javac CustomerDirectory.java I:Assignment>java CustomerDirectory Enter the Name: loga Enter the Car type(petrolcar=1 or dieselcar=0): 1 Dear loga, Please find enclosed a complimentary voucher for the amount of Rs.5000